diff --git a/samples/Sentry.Samples.Console.Basic/Program.cs b/samples/Sentry.Samples.Console.Basic/Program.cs index 889dcde450..9a82d0e633 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,22 @@ 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); + + // 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, SentryUnits.Duration.Second, + [new KeyValuePair("http.request.method", HttpMethod.Get.Method), new KeyValuePair("http.response.status_code", (int)HttpStatusCode.OK)]); } async Task SecondFunction()