From f1fd5e1ed60fdf228947b4a15c0910c1b5ae7660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20P=C3=B6lz?= <38893694+Flash0ver@users.noreply.github.com> Date: Thu, 15 Jan 2026 17:54:21 +0100 Subject: [PATCH 1/4] feat(metrics): Trace-connected Metrics (Samples) --- .../Sentry.Samples.Console.Basic/Program.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/samples/Sentry.Samples.Console.Basic/Program.cs b/samples/Sentry.Samples.Console.Basic/Program.cs index 889dcde450..6730069362 100644 --- a/samples/Sentry.Samples.Console.Basic/Program.cs +++ b/samples/Sentry.Samples.Console.Basic/Program.cs @@ -9,6 +9,8 @@ * For more advanced features of the SDK, see Sentry.Samples.Console.Customized. */ +using System.Diagnostics; +using System.Net; using System.Net.Http; using static System.Console; @@ -50,6 +52,17 @@ // Drop logs with level Info return log.Level is SentryLogLevel.Info ? null : log; }); + + // Sentry (trace-connected) Metrics via SentrySdk.Experimental.Metrics are enabled by default. + options.Experimental.SetBeforeSendMetric(static metric => + { + // A demonstration of how you can modify the metric object before sending it to Sentry + metric.SetAttribute("operating_system.platform", Environment.OSVersion.Platform.ToString()); + metric.SetAttribute("operating_system.version", Environment.OSVersion.Version.ToString()); + + // Return null to drop the metric + return metric; + }); }); // This starts a new transaction and attaches it to the scope. @@ -71,9 +84,16 @@ async Task FirstFunction() // This is an example of making an HttpRequest. A trace us automatically captured by Sentry for this. var messageHandler = new SentryHttpMessageHandler(); var httpClient = new HttpClient(messageHandler, true); + + var stopwatch = Stopwatch.StartNew(); var html = await httpClient.GetStringAsync("https://example.com/"); + stopwatch.Stop(); + WriteLine(html); SentrySdk.Logger.LogInfo("HTTP Request completed."); + SentrySdk.Experimental.Metrics.EmitCounter("sentry.samples.console.basic.http_requests_completed", 1); + SentrySdk.Experimental.Metrics.EmitDistribution("sentry.samples.console.basic.http_request_duration", stopwatch.Elapsed.TotalSeconds, "second", + [new KeyValuePair("http.request.method", HttpMethod.Get.Method), new KeyValuePair("http.response.status_code", (int)HttpStatusCode.OK)]); } async Task SecondFunction() From 15e2d2270368126b68c8408a42ef1347f85d7cfc Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Thu, 15 Jan 2026 17:21:47 +0000 Subject: [PATCH 2/4] Format code --- samples/Sentry.Samples.Console.Basic/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Sentry.Samples.Console.Basic/Program.cs b/samples/Sentry.Samples.Console.Basic/Program.cs index 6730069362..71d505e623 100644 --- a/samples/Sentry.Samples.Console.Basic/Program.cs +++ b/samples/Sentry.Samples.Console.Basic/Program.cs @@ -93,7 +93,7 @@ async Task FirstFunction() SentrySdk.Logger.LogInfo("HTTP Request completed."); SentrySdk.Experimental.Metrics.EmitCounter("sentry.samples.console.basic.http_requests_completed", 1); SentrySdk.Experimental.Metrics.EmitDistribution("sentry.samples.console.basic.http_request_duration", stopwatch.Elapsed.TotalSeconds, "second", - [new KeyValuePair("http.request.method", HttpMethod.Get.Method), new KeyValuePair("http.response.status_code", (int)HttpStatusCode.OK)]); + [new KeyValuePair("http.request.method", HttpMethod.Get.Method), new KeyValuePair("http.response.status_code", (int)HttpStatusCode.OK)]); } async Task SecondFunction() From 01ecd9c98f257b0a6cf4ad7028395ee26964cc6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20P=C3=B6lz?= <38893694+Flash0ver@users.noreply.github.com> Date: Thu, 15 Jan 2026 19:45:43 +0100 Subject: [PATCH 3/4] add more comments --- samples/Sentry.Samples.Console.Basic/Program.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/samples/Sentry.Samples.Console.Basic/Program.cs b/samples/Sentry.Samples.Console.Basic/Program.cs index 71d505e623..5e34bafd35 100644 --- a/samples/Sentry.Samples.Console.Basic/Program.cs +++ b/samples/Sentry.Samples.Console.Basic/Program.cs @@ -90,8 +90,14 @@ async Task FirstFunction() stopwatch.Stop(); WriteLine(html); + + // Info-Log filtered via "BeforeSendLog" callback SentrySdk.Logger.LogInfo("HTTP Request completed."); + + // Metric modified via "BeforeSendMetric" callback for type "int" before sending it to Sentry SentrySdk.Experimental.Metrics.EmitCounter("sentry.samples.console.basic.http_requests_completed", 1); + + // Metric sent as is because no "BeforeSendMetric" is set for type "double" SentrySdk.Experimental.Metrics.EmitDistribution("sentry.samples.console.basic.http_request_duration", stopwatch.Elapsed.TotalSeconds, "second", [new KeyValuePair("http.request.method", HttpMethod.Get.Method), new KeyValuePair("http.response.status_code", (int)HttpStatusCode.OK)]); } From bdeff84c38a6de0f2acae9e0851002d49659e545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20P=C3=B6lz?= <38893694+Flash0ver@users.noreply.github.com> Date: Fri, 16 Jan 2026 15:02:01 +0100 Subject: [PATCH 4/4] update sample to use units-helper --- samples/Sentry.Samples.Console.Basic/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Sentry.Samples.Console.Basic/Program.cs b/samples/Sentry.Samples.Console.Basic/Program.cs index 5e34bafd35..9a82d0e633 100644 --- a/samples/Sentry.Samples.Console.Basic/Program.cs +++ b/samples/Sentry.Samples.Console.Basic/Program.cs @@ -98,7 +98,7 @@ async Task FirstFunction() SentrySdk.Experimental.Metrics.EmitCounter("sentry.samples.console.basic.http_requests_completed", 1); // Metric sent as is because no "BeforeSendMetric" is set for type "double" - SentrySdk.Experimental.Metrics.EmitDistribution("sentry.samples.console.basic.http_request_duration", stopwatch.Elapsed.TotalSeconds, "second", + SentrySdk.Experimental.Metrics.EmitDistribution("sentry.samples.console.basic.http_request_duration", stopwatch.Elapsed.TotalSeconds, SentryUnits.Duration.Second, [new KeyValuePair("http.request.method", HttpMethod.Get.Method), new KeyValuePair("http.response.status_code", (int)HttpStatusCode.OK)]); }