Add aspnet metric example (#3033)

This commit is contained in:
Vishwesh Bankwar 2022-03-12 06:37:03 -08:00 committed by GitHub
parent 3af5a3b0b4
commit 98a4c99350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -22,6 +22,7 @@ using System.Web.Mvc;
using System.Web.Routing;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
namespace Examples.AspNet
@ -31,6 +32,7 @@ namespace Examples.AspNet
#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 @@ namespace Examples.AspNet
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 @@ namespace Examples.AspNet
protected void Application_End()
{
this.tracerProvider?.Dispose();
this.meterProvider?.Dispose();
}
}
}

View File

@ -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"/>