Last active
February 1, 2023 17:09
-
-
Save DanielMPries/3390727b818f3a08cbaa448f9a0420bc to your computer and use it in GitHub Desktop.
Open Telemetry OLTP Collector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFramework>net6.0</TargetFramework> | |
<Nullable>enable</Nullable> | |
<ImplicitUsings>enable</ImplicitUsings> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.4.0-rc.2" /> | |
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.4.0-rc.2" /> | |
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs" Version="1.4.0-rc.2" /> | |
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.4.0-rc.2" /> | |
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.11" /> | |
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.11" /> | |
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./otel/config.yaml | |
receivers: | |
otlp: | |
protocols: | |
grpc: | |
endpoint: 0.0.0.0:4317 | |
http: | |
endpoint: 0.0.0.0:4318 | |
processors: | |
memory_limiter: | |
check_interval: 1s | |
limit_mib: 2000 | |
batch: | |
timeout: 10s | |
exporters: | |
logging: | |
logLevel: debug #warn | |
otlp/elastic: | |
endpoint: "apm-server:8200" | |
tls: | |
insecure: true | |
# ^ Don't do this in a real env | |
#headers: | |
# Authorization: "Bearer SomeAPIToken" | |
service: | |
pipelines: | |
traces: | |
receivers: [otlp] | |
exporters: [logging, otlp/elastic] | |
metrics: | |
receivers: [otlp] | |
exporters: [logging, otlp/elastic] | |
logs: | |
receivers: [otlp] | |
exporters: [logging, otlp/elastic] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.8" | |
otel-collector: | |
image: otel/opentelemetry-collector | |
command: ["--config=/etc/config.yaml"] | |
volumes: | |
- ./otel/config.yml:/etc/config.yaml | |
ports: | |
- "8888:8888" # Prometheus metrics exposed by the collector | |
- "8889:8889" # Prometheus exporter metrics | |
- "13133:13133" # health_check extension | |
- "4317:4317" # OTLP gRPC receiver | |
- "4318:4318" # OTLP HTTP receivr | |
networks: | |
- elk | |
networks: | |
elk: | |
name: docker-elk_elk | |
external: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global using FastEndpoints; | |
using System.Reflection; | |
using FastEndpoints.Swagger; | |
using FluentMigrator.Runner; | |
using OpenTelemetry.Instrumentation.AspNetCore; | |
using Microsoft.Extensions.Caching.Distributed; | |
using Microsoft.Extensions.Caching.StackExchangeRedis; | |
using QuickI18N.API.Caching; | |
using QuickI18N.API.Migrations; | |
using OpenTelemetry.Logs; | |
using OpenTelemetry.Trace; | |
using OpenTelemetry.Resources; | |
using OpenTelemetry.Metrics; | |
using System.Diagnostics; | |
using OpenTelemetry.Exporter; | |
using System.Diagnostics.Metrics; | |
var serviceVersion = "1.0.0"; | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Logging.With( _ => { | |
_.ClearProviders(); | |
_.AddOpenTelemetry(options => { | |
options.AddOtlpExporter(opts => { | |
opts.Endpoint = new Uri("http://localhost:4317"); | |
}); | |
}); | |
}); | |
builder.Services.With( _ => { | |
_.AddControllers(); | |
_.AddOpenTelemetryMetrics((meterBuilder) => { | |
meterBuilder | |
.AddOtlpExporter(opt => { | |
opt.Protocol = OtlpExportProtocol.Grpc; | |
opt.Endpoint = new Uri("http://localhost:4317"); | |
}) | |
.AddInstrumentation<Program>() | |
.AddAspNetCoreInstrumentation() | |
.AddHttpClientInstrumentation(); | |
}); | |
_.AddOpenTelemetryTracing((traceBuilder) => { | |
traceBuilder | |
.AddOtlpExporter(opt => { | |
opt.Protocol = OtlpExportProtocol.Grpc; | |
opt.Endpoint = new Uri("http://localhost:4317"); | |
}) | |
.AddSource(builder.Environment.ApplicationName) | |
.SetResourceBuilder( | |
ResourceBuilder.CreateDefault() | |
.AddService( | |
serviceName: builder.Environment.ApplicationName, | |
serviceVersion: serviceVersion)) | |
.AddInstrumentation<Program>() | |
.AddHttpClientInstrumentation() | |
.AddAspNetCoreInstrumentation(options => { | |
//options. = Enrich; | |
options.RecordException = true; | |
}); | |
}); | |
_.AddFastEndpoints(); | |
_.AddSwaggerDoc(); | |
_.AddMemoryCache(); | |
_.AddStackExchangeRedisCache(options => { | |
options.Configuration = builder.Configuration.GetSection("Redis")["ConnectionString"]; | |
}); | |
_.AddSingleton<IDistributedCache, RedisCache>(); | |
_.AddSingleton<ITwoLevelCache, TwoLevelCache>(); | |
_.AddFluentMigratorCore() | |
.ConfigureRunner(rb => rb | |
.AddSqlServer() | |
.WithGlobalConnectionString("DefaultConnection") | |
// Define the assembly containing the migrations | |
.ScanIn(Assembly.GetExecutingAssembly()) | |
.For.Migrations()); | |
}); | |
var app = builder.Build(); | |
app.UseFastEndpoints(); | |
if (app.Environment.IsDevelopment()) | |
{ | |
app.UseOpenApi(); | |
app.UseSwaggerUi3(s => s.ConfigureDefaults()); | |
} | |
app.Migrate(); | |
app.UseHttpsRedirection(); | |
app.UseAuthorization(); | |
app.Run(); | |
static void Enrich(Activity activity, string eventName, object obj) | |
{ | |
if (obj is HttpRequest request) | |
{ | |
var context = request.HttpContext; | |
activity.AddTag("http.client_ip", context.Connection.RemoteIpAddress); | |
activity.AddTag("http.request_content_length", request.ContentLength); | |
activity.AddTag("http.request_content_type", request.ContentType); | |
foreach(var header in request.Headers) { | |
activity.AddTag($"http.request_header.{header.Key}", header.Value); | |
} | |
} | |
else if (obj is HttpResponse response) | |
{ | |
activity.AddTag("http.response_content_length", response.ContentLength); | |
activity.AddTag("http.response_content_type", response.ContentType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment