-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenTelemetryExtensions.cs
More file actions
86 lines (71 loc) · 3.33 KB
/
Copy pathOpenTelemetryExtensions.cs
File metadata and controls
86 lines (71 loc) · 3.33 KB
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
using NServiceBus.Configuration.AdvancedExtensibility;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
namespace CommonConfigurations;
static class OpenTelemetryExtensions
{
public static void EnableOpenTelemetryMetrics(this EndpointConfiguration endpointConfiguration)
{
var endpointName = endpointConfiguration.GetSettings().EndpointName();
var attributes = new Dictionary<string, object>
{
["service.name"] = endpointName,
["service.instance.id"] = Guid.NewGuid().ToString(),
};
var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(attributes);
var meterProviderBuilder = Sdk.CreateMeterProviderBuilder()
.SetResourceBuilder(resourceBuilder)
.AddMeter("NServiceBus.Core.Pipeline.Incoming")
.AddMeter("LoanBroker")
.AddOtlpExporter(cfg =>
{
var url = Environment.GetEnvironmentVariable(OtlpMetricsUrlEnvVar) ?? OtlpMetricsDefaultUrl;
cfg.Endpoint = new Uri(url);
cfg.Protocol = OtlpExportProtocol.HttpProtobuf;
});
// When orchestrated by Aspire, also export to the dashboard's OTLP endpoint so traces and
// metrics show up in the Aspire dashboard.
if (HasAspireOtlpEndpoint())
{
meterProviderBuilder.AddOtlpExporter();
}
meterProviderBuilder.Build();
}
public static void EnableOpenTelemetryTracing(this EndpointConfiguration endpointConfiguration)
{
var endpointName = endpointConfiguration.GetSettings().EndpointName();
var attributes = new Dictionary<string, object>
{
["service.name"] = endpointName,
["service.instance.id"] = Guid.NewGuid().ToString(),
};
var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(attributes);
var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(resourceBuilder)
.AddSource("NServiceBus.Core")
.AddOtlpExporter(cfg =>
{
var url = Environment.GetEnvironmentVariable(OtlpTracesUrlEnvVar) ?? OtlpTracesDefaultUrl;
cfg.Endpoint = new Uri(url);
cfg.Protocol = OtlpExportProtocol.HttpProtobuf;
});
// Also feed the Aspire dashboard when running under the AppHost (see metrics for details).
if (HasAspireOtlpEndpoint())
{
tracerProviderBuilder.AddOtlpExporter();
}
tracerProviderBuilder.Build();
}
// Aspire injects OTEL_EXPORTER_OTLP_ENDPOINT (plus protocol/headers) into project resources it
// orchestrates. Its absence means we are not running under Aspire (e.g. plain Docker Compose),
// so the dashboard exporter is skipped and only the collector pipeline is used.
static bool HasAspireOtlpEndpoint() =>
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT"));
const string OtlpMetricsDefaultUrl = "http://localhost:5318/v1/metrics";
const string OtlpTracesDefaultUrl = "http://localhost:5318/v1/traces";
const string OtlpMetricsUrlEnvVar = "OTLP_METRICS_URL";
const string OtlpTracesUrlEnvVar = "OTLP_TRACING_URL";
}