-
Notifications
You must be signed in to change notification settings - Fork 290
/
Global.asax.cs
89 lines (76 loc) · 2.89 KB
/
Global.asax.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Configuration;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
namespace Examples.AspNet;
#pragma warning disable SA1649 // File name should match first type name
public class WebApiApplication : HttpApplication
#pragma warning restore SA1649 // File name should match first type name
{
private TracerProvider? tracerProvider;
private MeterProvider? meterProvider;
protected void Application_Start()
{
var builder = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation();
switch (ConfigurationManager.AppSettings["UseExporter"].ToUpperInvariant())
{
case "ZIPKIN":
builder.AddZipkinExporter(zipkinOptions =>
{
zipkinOptions.Endpoint = new Uri(ConfigurationManager.AppSettings["ZipkinEndpoint"]);
});
break;
case "OTLP":
builder.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(ConfigurationManager.AppSettings["OtlpEndpoint"]);
});
break;
default:
builder.AddConsoleExporter(options => options.Targets = ConsoleExporterOutputTargets.Debug);
break;
}
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();
switch (ConfigurationManager.AppSettings["UseMetricsExporter"].ToUpperInvariant())
{
case "OTLP":
meterBuilder.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(ConfigurationManager.AppSettings["OtlpEndpoint"]);
});
break;
case "PROMETHEUS":
meterBuilder.AddPrometheusHttpListener();
break;
default:
meterBuilder.AddConsoleExporter((exporterOptions, metricReaderOptions) =>
{
exporterOptions.Targets = ConsoleExporterOutputTargets.Debug;
});
break;
}
this.meterProvider = meterBuilder.Build();
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
protected void Application_End()
{
this.tracerProvider?.Dispose();
this.meterProvider?.Dispose();
}
}