-
Notifications
You must be signed in to change notification settings - Fork 429
Enable OTLP exporting as part of development build #2006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -248,9 +248,13 @@ public static IServiceCollection AddAzureMcpServer(this IServiceCollection servi | |
| /// Using <see cref="IConfiguration"/> configures <see cref="McpServerConfiguration"/>. | ||
| /// </summary> | ||
| /// <param name="services">Service Collection to add configuration logic to.</param> | ||
| public static void InitializeConfigurationAndOptions(this IServiceCollection services) | ||
| public static void InitializeConfigurationOptionsAndOpenTelemetry(this IServiceCollection services) | ||
| { | ||
| #if DEBUG | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason for having DOTNET_ENVIRONMENT is so we don't have to recompile to make test changes. We can rerun the application with different settings. We shouldn't have to if/def things based on the build type.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was finding the DOTNET_ENVIRONMENT was blank when running debug builds, so we fell back to |
||
| var environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Development"; | ||
| #else | ||
| var environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"; | ||
| #endif | ||
| var configuration = new ConfigurationBuilder() | ||
| .AddJsonFile("appsettings.json", optional: false) | ||
| .AddJsonFile($"appsettings.{environment}.json", optional: true) | ||
|
|
@@ -272,11 +276,8 @@ public static void InitializeConfigurationAndOptions(this IServiceCollection ser | |
|
|
||
| // Assembly.GetEntryAssembly is used to retrieve the version of the server application as that is | ||
| // the assembly that will run the tool calls. | ||
| var entryAssembly = Assembly.GetEntryAssembly(); | ||
| if (entryAssembly == null) | ||
| { | ||
| throw new InvalidOperationException("Entry assembly must be a managed assembly."); | ||
| } | ||
| var entryAssembly = Assembly.GetEntryAssembly() | ||
| ?? throw new InvalidOperationException("Entry assembly must be a managed assembly."); | ||
|
|
||
| options.Version = AssemblyHelper.GetAssemblyVersion(entryAssembly); | ||
|
|
||
|
|
@@ -293,5 +294,7 @@ public static void InitializeConfigurationAndOptions(this IServiceCollection ser | |
| // over any other settings. | ||
| options.IsTelemetryEnabled = rootConfiguration.GetValue("AZURE_MCP_COLLECT_TELEMETRY", true); | ||
| }); | ||
|
|
||
| services.ConfigureOpenTelemetry(configuration); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using System.Runtime.InteropServices; | ||
| using Azure.Monitor.OpenTelemetry.Exporter; | ||
| using Microsoft.Extensions.Azure; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
@@ -25,7 +26,7 @@ public static class OpenTelemetryExtensions | |
| /// </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) | ||
| public static void ConfigureOpenTelemetry(this IServiceCollection services, IConfiguration configuration) | ||
| { | ||
| services.AddSingleton<ITelemetryService, TelemetryService>(); | ||
|
|
||
|
|
@@ -46,10 +47,10 @@ public static void ConfigureOpenTelemetry(this IServiceCollection services) | |
| services.AddSingleton<IMachineInformationProvider, DefaultMachineInformationProvider>(); | ||
| } | ||
|
|
||
| EnableAzureMonitor(services); | ||
| EnableAzureMonitor(services, configuration); | ||
| } | ||
|
|
||
| private static void EnableAzureMonitor(this IServiceCollection services) | ||
| private static void EnableAzureMonitor(this IServiceCollection services, IConfiguration configuration) | ||
| { | ||
| #if DEBUG | ||
| services.AddSingleton(sp => | ||
|
|
@@ -80,7 +81,7 @@ private static void EnableAzureMonitor(this IServiceCollection services) | |
| .AddTelemetrySdk(); | ||
| }); | ||
|
|
||
| var userProvidedAppInsightsConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"); | ||
| var userProvidedAppInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]; | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(userProvidedAppInsightsConnectionString)) | ||
| { | ||
|
|
@@ -92,7 +93,7 @@ private static void EnableAzureMonitor(this IServiceCollection services) | |
| #if RELEASE | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Frankly, I think we could have avoided this by using default appsettings.json = false and used the running environment to set it or not. Because now if we want to test this in a debug environment, it's impossible. |
||
| // 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"); | ||
| var microsoftTelemetry = configuration["AZURE_MCP_COLLECT_TELEMETRY_MICROSOFT"]; | ||
|
|
||
| bool shouldCollectMicrosoftTelemetry = string.IsNullOrWhiteSpace(microsoftTelemetry) || (bool.TryParse(microsoftTelemetry, out var shouldCollect) && shouldCollect); | ||
|
|
||
|
|
@@ -102,7 +103,7 @@ private static void EnableAzureMonitor(this IServiceCollection services) | |
| } | ||
| #endif | ||
|
|
||
| var enableOtlp = Environment.GetEnvironmentVariable("AZURE_MCP_ENABLE_OTLP_EXPORTER"); | ||
| var enableOtlp = configuration["AZURE_MCP_ENABLE_OTLP_EXPORTER"]; | ||
| if (!string.IsNullOrEmpty(enableOtlp) && bool.TryParse(enableOtlp, out var shouldEnable) && shouldEnable) | ||
| { | ||
| otelBuilder.WithTracing(tracing => tracing.AddOtlpExporter()) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we have to configure open telemetry in this method as well? This method now has multiple responsibilities rather than just setting options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find a cleaner way to get the IConfiguration singleton bound here when we called
OpenTelemetryExtensions.ConfigureOpenTelemetry. If you know of a way to cleanly pull that out of theIServiceCollectionI'd be more than happy to revert this change as I'm not a huge fan of it either.