-
Notifications
You must be signed in to change notification settings - Fork 5
/
ObservabilityRegistration.cs
116 lines (100 loc) · 4.14 KB
/
ObservabilityRegistration.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
using BuildingBlocks.Observability.Options;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Serilog;
using Serilog.Settings.Configuration;
using Serilog.Sinks.OpenTelemetry;
namespace BuildingBlocks.Observability;
public static class ObservabilityRegistration
{
public static WebApplicationBuilder AddObservability(this WebApplicationBuilder builder)
{
var configuration = builder.Configuration;
ObservabilityOptions observabilityOptions = new();
configuration
.GetRequiredSection(nameof(ObservabilityOptions))
.Bind(observabilityOptions);
builder.AddSerilog(observabilityOptions);
builder.Services
.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(observabilityOptions.ServiceName))
.AddMetrics(observabilityOptions)
.AddTracing(observabilityOptions);
return builder;
}
private static OpenTelemetryBuilder AddTracing(this OpenTelemetryBuilder builder, ObservabilityOptions observabilityOptions)
{
if (!observabilityOptions.EnabledTracing) return builder;
builder.WithTracing(tracing =>
{
tracing
.SetErrorStatusOnException()
.SetSampler(new AlwaysOnSampler())
.AddAspNetCoreInstrumentation(options =>
{
options.RecordException = true;
});
tracing
.AddOtlpExporter(_ =>
{
_.Endpoint = observabilityOptions.CollectorUri;
_.ExportProcessorType = ExportProcessorType.Batch;
_.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
});
});
return builder;
}
private static OpenTelemetryBuilder AddMetrics(this OpenTelemetryBuilder builder, ObservabilityOptions observabilityOptions)
{
builder.WithMetrics(metrics =>
{
metrics
.AddAspNetCoreInstrumentation();
metrics
.AddOtlpExporter(_ =>
{
_.Endpoint = observabilityOptions.CollectorUri;
_.ExportProcessorType = ExportProcessorType.Batch;
_.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
});
});
return builder;
}
private static WebApplicationBuilder AddSerilog(this WebApplicationBuilder builder, ObservabilityOptions observabilityOptions)
{
var services = builder.Services;
var configuration = builder.Configuration;
services.AddSerilog((sp, serilog) =>
{
serilog
.ReadFrom.Configuration(configuration, new ConfigurationReaderOptions
{
SectionName = $"{nameof(ObservabilityOptions)}:{nameof(Serilog)}"
})
.ReadFrom.Services(sp)
.Enrich.FromLogContext()
.Enrich.WithProperty("ApplicationName", observabilityOptions.ServiceName)
.WriteTo.Console();
serilog
.WriteTo.OpenTelemetry(c =>
{
c.Endpoint = observabilityOptions.CollectorUrl;
c.Protocol = OtlpProtocol.Grpc;
c.IncludedData = IncludedData.TraceIdField | IncludedData.SpanIdField | IncludedData.SourceContextAttribute;
c.ResourceAttributes = new Dictionary<string, object>
{
{"service.name", observabilityOptions.ServiceName},
{"index", 10},
{"flag", true},
{"value", 3.14}
};
});
});
return builder;
}
}