-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
93 lines (86 loc) · 4.02 KB
/
Startup.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
90
91
92
93
namespace DemoOpenTelemetry
{
using System.Diagnostics;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
/// <summary>
/// Startup class contains method for App Startiup
/// </summary>
public static class Startup
{
public static void SetCustomerInstanceConfigurations(WebApplicationBuilder builder)
{
builder.Configuration.AddEnvironmentVariables(prefix: "CustomerInstanceConfiguration_");
builder.Services.Configure<CustomerInstanceConfigurationOptions>(builder.Configuration.GetSection("CustomerInstanceConfigurationOptions"));
}
/// <summary>
/// Method to inject services in to the DI Container
/// </summary>
/// <param name="services">Service Collection</param>
public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
//Configure Options
services.ConfigureOptions(configuration);
}
public static void ConfigureOpenTelemetry(WebApplicationBuilder builder)
{
var appInsightsConnection = builder.Configuration.GetSection("ApplicationInsights")["ConnectionString"];
var customerInstanceConfig = builder.Configuration.GetSection("CustomerInstanceConfiguration").Get<CustomerInstanceConfigurationOptions>();
builder.Services.AddOpenTelemetry()
.WithTracing(builder => builder
.AddAspNetCoreInstrumentation(o =>
{
o.EnrichWithHttpRequest = (activity, httpRequest) =>
{
activity.SetTag("requestProtocol", httpRequest.Protocol);
};
o.EnrichWithHttpResponse = (activity, httpResponse) =>
{
activity.SetTag("responseLength", httpResponse.ContentLength);
};
o.EnrichWithException = (activity, exception) =>
{
activity.SetTag("exceptionType", exception.GetType().ToString());
};
})
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.AddAzureMonitorTraceExporter(o =>
{
o.ConnectionString = appInsightsConnection;
o.Diagnostics.IsDistributedTracingEnabled = true;
}))
.WithMetrics(builder => builder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.AddAzureMonitorMetricExporter(o =>
{
o.ConnectionString = appInsightsConnection;
})
.ConfigureResource(resource =>
{
resource.AddService(DiagnosticsConfig.ServiceName);
resource.AddAttributes(customerInstanceConfig.Values);
}))
.ConfigureResource(resource =>
{
resource.AddService(DiagnosticsConfig.ServiceName);
resource.AddAttributes(customerInstanceConfig.Values);
}
);
}
}
public static class DiagnosticsConfig
{
public const string ServiceName = "DemoOpenTelemetry";
public static ActivitySource ActivitySource = new ActivitySource(ServiceName);
}
}