From 813894130e60adc85b614ea37276195bb547da8e Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Fri, 11 Mar 2022 14:10:22 -0800 Subject: [PATCH 1/3] add aspnet metric example --- examples/AspNet/Global.asax.cs | 33 +++++++++++++++++++++++++++++++++ examples/AspNet/Web.config | 1 + 2 files changed, 34 insertions(+) diff --git a/examples/AspNet/Global.asax.cs b/examples/AspNet/Global.asax.cs index ca4ad645aa5..c81ded79e7d 100644 --- a/examples/AspNet/Global.asax.cs +++ b/examples/AspNet/Global.asax.cs @@ -22,6 +22,7 @@ using System.Web.Routing; using OpenTelemetry; using OpenTelemetry.Exporter; +using OpenTelemetry.Metrics; using OpenTelemetry.Trace; namespace Examples.AspNet @@ -31,6 +32,7 @@ public class WebApiApplication : HttpApplication #pragma warning restore SA1649 // File name should match first type name { private IDisposable tracerProvider; + private IDisposable meterProvider; protected void Application_Start() { @@ -66,6 +68,36 @@ protected void Application_Start() this.tracerProvider = builder.Build(); + // Metrics + // Note: Tracerprovider is needed for metrics to work + + var meterBuilder = Sdk.CreateMeterProviderBuilder() + .AddAspNetInstrumentation() + .AddHttpClientInstrumentation(); + + switch (ConfigurationManager.AppSettings["UseMetricsExporter"].ToLowerInvariant()) + { + case "otlp": + meterBuilder.AddOtlpExporter(otlpOptions => + { + otlpOptions.Endpoint = new Uri(ConfigurationManager.AppSettings["OtlpEndpoint"]); + }); + break; + default: + meterBuilder.AddConsoleExporter((exporterOptions, metricReaderOptions) => + { + exporterOptions.Targets = ConsoleExporterOutputTargets.Debug; + + // The ConsoleMetricExporter defaults to a manual collect cycle. + // This configuration causes metrics to be exported to stdout on a 10s interval. + metricReaderOptions.MetricReaderType = MetricReaderType.Periodic; + metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 10000; + }); + break; + } + + this.meterProvider = meterBuilder.Build(); + GlobalConfiguration.Configure(WebApiConfig.Register); AreaRegistration.RegisterAllAreas(); @@ -75,6 +107,7 @@ protected void Application_Start() protected void Application_End() { this.tracerProvider?.Dispose(); + this.meterProvider?.Dispose(); } } } diff --git a/examples/AspNet/Web.config b/examples/AspNet/Web.config index c81722ba16a..d38c23bbfc6 100644 --- a/examples/AspNet/Web.config +++ b/examples/AspNet/Web.config @@ -6,6 +6,7 @@ + From 794ab1496182498a81730fe02dd98dbfb060aea2 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Fri, 11 Mar 2022 14:28:23 -0800 Subject: [PATCH 2/3] indent --- examples/AspNet/Web.config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/AspNet/Web.config b/examples/AspNet/Web.config index d38c23bbfc6..bd4f12f8b14 100644 --- a/examples/AspNet/Web.config +++ b/examples/AspNet/Web.config @@ -6,7 +6,7 @@ - + From e78d815304819a1c5b2987a16a9e789dce9f717b Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Fri, 11 Mar 2022 14:29:54 -0800 Subject: [PATCH 3/3] link to issue --- examples/AspNet/Global.asax.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/AspNet/Global.asax.cs b/examples/AspNet/Global.asax.cs index c81ded79e7d..b8e3759ac03 100644 --- a/examples/AspNet/Global.asax.cs +++ b/examples/AspNet/Global.asax.cs @@ -70,6 +70,7 @@ protected void Application_Start() // Metrics // Note: Tracerprovider is needed for metrics to work + // https://github.com/open-telemetry/opentelemetry-dotnet/issues/2994 var meterBuilder = Sdk.CreateMeterProviderBuilder() .AddAspNetInstrumentation()