-
Notifications
You must be signed in to change notification settings - Fork 893
Add aspnet metric example #3033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
8138941
36c7e6b
794ab14
215fac5
e78d815
7a13bd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HttpClient for .NET FW don't produce metrics as of today, so its best to remove this line to avoid confusion. We can add it when httpclient starts having some metrics. |
||
|
|
||
| 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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given #2982 is merged, we can simplify the code here?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI @alanwest, IIRC you plan to remove the MetricReaderType entirely? |
||
| 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(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.