-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathOpenTelemetryExtensions.cs
More file actions
175 lines (153 loc) · 6.95 KB
/
OpenTelemetryExtensions.cs
File metadata and controls
175 lines (153 loc) · 6.95 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Reflection;
using System.Runtime.InteropServices;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Mcp.Core.Commands;
using Microsoft.Mcp.Core.Configuration;
using Microsoft.Mcp.Core.Services.Telemetry;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
namespace Azure.Mcp.Core.Extensions;
public static class OpenTelemetryExtensions
{
/// <summary>
/// The App Insights connection string to send telemetry to Microsoft.
/// </summary>
private const string MicrosoftOwnedAppInsightsConnectionString = "InstrumentationKey=21e003c0-efee-4d3f-8a98-1868515aa2c9;IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/;ApplicationId=f14f6a2d-6405-4f88-bd58-056f25fe274f";
public static void ConfigureOpenTelemetry(this IServiceCollection services)
{
services.AddSingleton<ITelemetryService, TelemetryService>();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
services.AddSingleton<IMachineInformationProvider, WindowsMachineInformationProvider>();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
services.AddSingleton<IMachineInformationProvider, MacOSXMachineInformationProvider>();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
services.AddSingleton<IMachineInformationProvider, LinuxMachineInformationProvider>();
}
else
{
services.AddSingleton<IMachineInformationProvider, DefaultMachineInformationProvider>();
}
EnableAzureMonitor(services);
}
private static void EnableAzureMonitor(this IServiceCollection services)
{
#if DEBUG
services.AddSingleton(sp =>
{
var forwarder = new AzureEventSourceLogForwarder(sp.GetRequiredService<ILoggerFactory>());
forwarder.Start();
return forwarder;
});
#endif
services.ConfigureOpenTelemetryTracerProvider((sp, builder) =>
{
var serverConfig = sp.GetRequiredService<IOptions<McpServerConfiguration>>();
if (!serverConfig.Value.IsTelemetryEnabled)
{
return;
}
builder.AddSource(serverConfig.Value.Name);
});
var otelBuilder = services.AddOpenTelemetry()
.ConfigureResource(r =>
{
var version = Assembly.GetExecutingAssembly()?.GetName()?.Version?.ToString();
r.AddService("azmcp", version)
.AddTelemetrySdk();
});
var userProvidedAppInsightsConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING");
if (!string.IsNullOrWhiteSpace(userProvidedAppInsightsConnectionString))
{
// Configure telemetry to be sent to user-provided Application Insights instance regardless of build configuration.
ConfigureUserProvidedAzureMonitorExporter(otelBuilder, userProvidedAppInsightsConnectionString);
}
// Configure Microsoft-owned telemetry only in RELEASE builds to avoid polluting telemetry during development.
#if RELEASE
// This environment variable can be used to disable Microsoft telemetry collection.
// By default, Microsoft telemetry is enabled.
var microsoftTelemetry = Environment.GetEnvironmentVariable("AZURE_MCP_COLLECT_TELEMETRY_MICROSOFT");
bool shouldCollectMicrosoftTelemetry = string.IsNullOrWhiteSpace(microsoftTelemetry) || (bool.TryParse(microsoftTelemetry, out var shouldCollect) && shouldCollect);
if (shouldCollectMicrosoftTelemetry)
{
ConfigureMicrosoftAzureMonitorExporter(otelBuilder, MicrosoftOwnedAppInsightsConnectionString);
}
#endif
var enableOtlp = Environment.GetEnvironmentVariable("AZURE_MCP_ENABLE_OTLP_EXPORTER");
if (true)
{
otelBuilder.WithTracing(tracing => tracing.AddOtlpExporter())
.WithMetrics(metrics => metrics.AddOtlpExporter())
.WithLogging(logging => logging.AddOtlpExporter());
}
}
/// <summary>
/// Configures OpenTelemetry to use Azure Monitor exporters with Microsoft's Application Insights instance.
/// </summary>
/// <param name="otelBuilder">The OpenTelemetry builder to configure.</param>
/// <param name="appInsightsConnectionString">The Application Insights connection string for Microsoft's telemetry instance.</param>
private static void ConfigureMicrosoftAzureMonitorExporter(OpenTelemetry.OpenTelemetryBuilder otelBuilder, string appInsightsConnectionString)
{
// We don't configure logging for Microsoft telemetry to avoid sending potentially sensitive log data to Microsoft.
otelBuilder.WithMetrics(metrics =>
{
metrics.AddAzureMonitorMetricExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
},
name: AppInsightsInstanceType.Microsoft);
});
otelBuilder.WithTracing(tracing =>
{
tracing.AddAzureMonitorTraceExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
},
name: AppInsightsInstanceType.Microsoft);
});
}
/// <summary>
/// Configures OpenTelemetry to use Azure Monitor exporters with a user-provided Application Insights connection string.
/// </summary>
/// <param name="otelBuilder">The OpenTelemetry builder to configure.</param>
/// <param name="appInsightsConnectionString">The Application Insights connection string provided by the user.</param>
private static void ConfigureUserProvidedAzureMonitorExporter(OpenTelemetry.OpenTelemetryBuilder otelBuilder, string appInsightsConnectionString)
{
otelBuilder.WithLogging(logging =>
{
logging.AddAzureMonitorLogExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
},
name: AppInsightsInstanceType.UserProvided);
});
otelBuilder.WithMetrics(metrics =>
{
metrics.AddAzureMonitorMetricExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
},
name: AppInsightsInstanceType.UserProvided);
});
otelBuilder.WithTracing(tracing =>
{
tracing.AddAzureMonitorTraceExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
},
name: AppInsightsInstanceType.UserProvided);
});
}
}