-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathProgram.cs
More file actions
55 lines (46 loc) · 2 KB
/
Program.cs
File metadata and controls
55 lines (46 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* This sample demonstrates how to initialize and enable Open Telemetry with Sentry
* in a console application.
* For using Open Telemetry and Sentry in ASP.NET, see Sentry.Samples.OpenTelemetry.AspNet.
* For using Open Telemetry and Sentry in ASP.NET Core, see Sentry.Samples.OpenTelemetry.AspNetCore.
*/
using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Trace;
using Sentry.OpenTelemetry;
var activitySource = new ActivitySource("Sentry.Samples.OpenTelemetry.Console");
SentrySdk.Init(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif
options.Debug = true;
options.TracesSampleRate = 1.0;
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
});
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(activitySource.Name)
.AddHttpClientInstrumentation()
.AddSentry() // <-- Configure OpenTelemetry to send traces to Sentry
.Build();
Console.WriteLine("Hello World!");
// Finally we can use OpenTelemetry to instrument our code. This activity will be captured as a Sentry transaction.
using (var activity = activitySource.StartActivity("Main"))
{
// This creates a span called "Task 1" within the transaction
using (var task = activitySource.StartActivity("Task 1"))
{
task?.SetTag("Answer", 42);
Thread.Sleep(100); // simulate some work
Console.WriteLine("Task 1 completed");
task?.SetStatus(ActivityStatusCode.Ok);
}
// Since we use `AddHttpClientInstrumentation` when initializing OpenTelemetry, the following Http request will also
// be captured as a Sentry span
var httpClient = new HttpClient();
var html = await httpClient.GetStringAsync("https://example.com/");
Console.WriteLine(html);
}
Console.WriteLine("Goodbye cruel world...");