diff --git a/examples/AspNet/Global.asax.cs b/examples/AspNet/Global.asax.cs index ca4ad645aa5..b8e3759ac03 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,37 @@ protected void Application_Start() this.tracerProvider = builder.Build(); + // Metrics + // Note: Tracerprovider is needed for metrics to work + // https://github.com/open-telemetry/opentelemetry-dotnet/issues/2994 + + 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 +108,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..bd4f12f8b14 100644 --- a/examples/AspNet/Web.config +++ b/examples/AspNet/Web.config @@ -6,6 +6,7 @@ +