|
11 | 11 | // OpenTelemetry Configuration |
12 | 12 | // See https://opentelemetry.io/docs/instrumentation/net/getting-started/ |
13 | 13 | builder.Services.AddOpenTelemetry() |
| 14 | + // This block configures OpenTelemetry to send traces to Sentry |
| 15 | + .WithTracing(tracerProviderBuilder => |
| 16 | + tracerProviderBuilder |
| 17 | + // Here we add a custom source we've created, which sends telemetry in the `LookupUser` method below |
| 18 | + .AddSampleInstrumentation() |
| 19 | + // Here we can optionally configure resource attributes that get sent with every trace |
| 20 | + .ConfigureResource(resource => resource.AddService(SampleTelemetry.ServiceName)) |
| 21 | + // The two lines below take care of configuring sources for ASP.NET Core and HttpClient |
| 22 | + .AddAspNetCoreInstrumentation() |
| 23 | + .AddHttpClientInstrumentation() |
| 24 | + // Finally we configure OpenTelemetry to send traces to Sentry |
| 25 | + .AddSentry() |
| 26 | + ) |
| 27 | + // This block configures OpenTelemetry metrics that we care about... later we'll configure Sentry to capture these |
14 | 28 | .WithMetrics(metrics => |
15 | 29 | { |
16 | 30 | metrics |
|
20 | 34 | "Microsoft.AspNetCore.Hosting", |
21 | 35 | "Microsoft.AspNetCore.Server.Kestrel", |
22 | 36 | "System.Net.Http"); |
23 | | - }) |
24 | | - .WithTracing(tracerProviderBuilder => |
25 | | - tracerProviderBuilder |
26 | | - .AddSource(Telemetry.ActivitySource.Name) |
27 | | - .ConfigureResource(resource => resource.AddService(Telemetry.ServiceName)) |
28 | | - .AddAspNetCoreInstrumentation() |
29 | | - .AddHttpClientInstrumentation() |
30 | | - .AddSentry() // <-- Configure OpenTelemetry to send traces to Sentry |
31 | | - ); |
| 37 | + }); |
32 | 38 |
|
33 | 39 | builder.WebHost.UseSentry(options => |
34 | 40 | { |
35 | 41 | // options.Dsn = "...Your DSN..."; |
36 | 42 | options.Debug = builder.Environment.IsDevelopment(); |
37 | 43 | options.SendDefaultPii = true; |
38 | 44 | options.TracesSampleRate = 1.0; |
| 45 | + options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information |
39 | 46 | // This shows experimental support for capturing OpenTelemetry metrics with Sentry |
40 | 47 | options.ExperimentalMetrics = new ExperimentalMetricsOptions() |
41 | 48 | { |
| 49 | + // Here we're telling Sentry to capture all built-in metrics. This includes all the metrics we configured |
| 50 | + // OpenTelemetry to emit when we called `builder.Services.AddOpenTelemetry()` above: |
| 51 | + // - "OpenTelemetry.Instrumentation.Runtime" |
| 52 | + // - "Microsoft.AspNetCore.Hosting", |
| 53 | + // - "Microsoft.AspNetCore.Server.Kestrel", |
| 54 | + // - "System.Net.Http" |
42 | 55 | CaptureSystemDiagnosticsMeters = BuiltInSystemDiagnosticsMeters.All |
43 | 56 | }; |
44 | | - options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information |
45 | 57 | }); |
46 | 58 |
|
47 | 59 | builder.Services |
|
85 | 97 |
|
86 | 98 | static string LookupUser(HttpRequest request) |
87 | 99 | { |
88 | | - using var _ = Telemetry.ActivitySource.StartActivity(nameof(LookupUser)); |
| 100 | + using var _ = SampleTelemetry.ActivitySource.StartActivity(nameof(LookupUser)); |
89 | 101 | Thread.Sleep(100); // Simulate some work |
90 | 102 | return (request.Query.TryGetValue("name", out var name)) |
91 | 103 | ? name.ToString() |
|
0 commit comments