Skip to content
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

Add aspnet metric example #3033

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/AspNet/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Web.Routing;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Examples.AspNet
Expand All @@ -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()
{
Expand Down Expand Up @@ -66,6 +68,37 @@ protected void Application_Start()

this.tracerProvider = builder.Build();

// Metrics
// Note: Tracerprovider is needed for metrics to work
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/2994

var meterBuilder = Sdk.CreateMeterProviderBuilder()
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation();
Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given #2982 is merged, we can simplify the code here?

Copy link
Member

Choose a reason for hiding this comment

The 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();
Expand All @@ -75,6 +108,7 @@ protected void Application_Start()
protected void Application_End()
{
this.tracerProvider?.Dispose();
this.meterProvider?.Dispose();
}
}
}
1 change: 1 addition & 0 deletions examples/AspNet/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="UseExporter" value="console"/>
<add key="UseMetricsExporter" value="console"/>
<add key="JaegerHost" value="localhost"/>
<add key="JaegerPort" value="6831"/>
<add key="ZipkinEndpoint" value="http://localhost:9411/api/v2/spans"/>
Expand Down