-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
150 lines (124 loc) · 5.15 KB
/
Program.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using OpenTelemetry;
using OpenTelemetry.Trace;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using OpenTelemetry.Context.Propagation;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.HttpOverrides;
ILoggerFactory loggerFactoryOT;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
var MyInstrumentation = new Instrumentation();
builder.Services.AddSingleton<Instrumentation>(MyInstrumentation);
ActivitySource MyActivitySource = MyInstrumentation.ActivitySource;
var configBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args);
IConfiguration configuration = configBuilder.Build();
// Add services to the container.
services.AddControllersWithViews();
// Load Dynatrace Metadata
List<KeyValuePair<string, object>> metadata = new List<KeyValuePair<string, object>>();
foreach (string name in configuration.GetSection("Otlp:MetadataFiles").Get<string[]>())
{
try
{
foreach (string line in System.IO.File.ReadAllLines(name.StartsWith("/var") ? name : System.IO.File.ReadAllText(name)))
{
var keyvalue = line.Split("=");
metadata.Add(new KeyValuePair<string, object>(keyvalue[0], keyvalue[1]));
}
}
catch { }
}
Action<ResourceBuilder> configureResource = r => r
.AddService(serviceName: MyActivitySource.Name)
.AddAttributes(metadata);
// Add OTEL services
services.AddOpenTelemetry()
.ConfigureResource(configureResource)
.WithTracing(builder => {
builder.SetSampler(new AlwaysOnSampler());
builder.AddSource(MyActivitySource.Name);
builder.AddAspNetCoreInstrumentation();
if (configuration.GetValue<string>("Otlp:TracesEndpoint") != "") {
builder.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(configuration.GetValue<string>("Otlp:TracesEndpoint"));
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
options.Headers = configuration.GetValue<string>("Otlp:Headers");
options.ExportProcessorType = OpenTelemetry.ExportProcessorType.Simple;
});
}
})
.WithMetrics(builder => {
builder.AddMeter(RequestMeter.MeterName);
if (configuration.GetValue<string>("Otlp:MetricsEndpoint") != "") {
builder.AddOtlpExporter((OtlpExporterOptions exporterOptions, MetricReaderOptions readerOptions) =>
{
exporterOptions.Endpoint = new Uri(configuration.GetValue<string>("Otlp:MetricsEndpoint"));
exporterOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
exporterOptions.Headers = configuration.GetValue<string>("Otlp:Headers");
readerOptions.TemporalityPreference = MetricReaderTemporalityPreference.Delta;
});
}
});
var resourceBuilder = ResourceBuilder.CreateDefault();
configureResource!(resourceBuilder);
Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddSource(MyActivitySource.Name)
.ConfigureResource(configureResource);
services.AddSingleton<RequestMeter>();
// Configure OTEL logger
loggerFactoryOT = LoggerFactory.Create(builder =>
{
builder
.AddOpenTelemetry(builder =>
{
builder.SetResourceBuilder(resourceBuilder);
if (configuration.GetValue<string>("Otlp:LogsEndpoint") != ""){
builder.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(configuration.GetValue<string>("Otlp:LogsEndpoint"));
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
options.Headers = configuration.GetValue<string>("Otlp:Headers");
options.ExportProcessorType = OpenTelemetry.ExportProcessorType.Batch;
});
}
})
.AddConsole();
});
var logger = loggerFactoryOT.CreateLogger<Program>();
services.AddSingleton<ILoggerFactory>(loggerFactoryOT);
// services.AddSingleton(logger);
// Test logger
logger.LogInformation(eventId: 123, "OtelTest");
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseForwardedHeaders();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();