-
Notifications
You must be signed in to change notification settings - Fork 253
Add telemetry for AzSdk MCP server tool calls #12063
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
Merged
praveenkuttappan
merged 4 commits into
Azure:main
from
praveenkuttappan:azsdk_mcp_telemetry
Sep 13, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tools/azsdk-cli/Azure.Sdk.Tools.Cli/Configuration/AzSdkToolsMcpServerConfiguration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| namespace Azure.Sdk.Tools.Cli.Configuration | ||
| { | ||
| public class AzSdkToolsMcpServerConfiguration | ||
| { | ||
| public const string DefaultName = Constants.TOOLS_ACTIVITY_SOURCE; | ||
|
|
||
| public string Name { get; set; } = DefaultName; | ||
|
|
||
| public string Version { get; set; } = "1.0.0-dev"; | ||
|
|
||
| public bool IsTelemetryEnabled { get; set; } = true; | ||
| } | ||
| } |
118 changes: 118 additions & 0 deletions
118
tools/azsdk-cli/Azure.Sdk.Tools.Cli/Extensions/OpenTelemetryExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using Azure.Sdk.Tools.Cli.Configuration; | ||
| using Azure.Sdk.Tools.Cli.Telemetry; | ||
| using Azure.Sdk.Tools.Cli.Telemetry.InformationProvider; | ||
| using Azure.Monitor.OpenTelemetry.Exporter; | ||
| using Microsoft.Extensions.Azure; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
| using OpenTelemetry.Resources; | ||
| using OpenTelemetry.Trace; | ||
|
|
||
| namespace Azure.Sdk.Tools.Cli.Extensions; | ||
|
|
||
| public static class OpenTelemetryExtensions | ||
| { | ||
| private const string DefaultAppInsights = "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.AddOptions<AzSdkToolsMcpServerConfiguration>() | ||
| .Configure(options => | ||
| { | ||
| var entryAssembly = Assembly.GetEntryAssembly(); | ||
| var assemblyName = entryAssembly?.GetName() ?? new AssemblyName(); | ||
| if (assemblyName?.Version != null) | ||
| { | ||
| options.Version = assemblyName.Version.ToString(); | ||
| } | ||
|
|
||
| var collectTelemetry = Environment.GetEnvironmentVariable("AZSDKTOOLS_MCP_COLLECT_TELEMETRY"); | ||
|
|
||
| options.IsTelemetryEnabled = string.IsNullOrEmpty(collectTelemetry) | ||
| || (bool.TryParse(collectTelemetry, out var shouldCollect) && shouldCollect); | ||
| }); | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| public static void ConfigureOpenTelemetryLogger(this ILoggingBuilder builder) | ||
| { | ||
| builder.AddOpenTelemetry(logger => | ||
| { | ||
| logger.AddProcessor(new TelemetryLogRecordEraser()); | ||
| }); | ||
| } | ||
|
|
||
| private static void EnableAzureMonitor(this IServiceCollection services) | ||
| { | ||
| #if DEBUG | ||
| services.AddSingleton(sp => | ||
| { | ||
| var forwarder = new AzureEventSourceLogForwarder(sp.GetRequiredService<ILoggerFactory>()); | ||
| forwarder.Start(); | ||
| return forwarder; | ||
| }); | ||
| #endif | ||
|
|
||
| var appInsightsConnectionString = Environment.GetEnvironmentVariable("AZSDKTOOLS_MCP_APPLICATIONINSIGHTS_CONNECTION_STRING"); | ||
|
|
||
| if (string.IsNullOrEmpty(appInsightsConnectionString)) | ||
| { | ||
| appInsightsConnectionString = DefaultAppInsights; | ||
| } | ||
|
|
||
| services.ConfigureOpenTelemetryTracerProvider((sp, builder) => | ||
| { | ||
| var serverConfig = sp.GetRequiredService<IOptions<AzSdkToolsMcpServerConfiguration>>(); | ||
| if (!serverConfig.Value.IsTelemetryEnabled) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| builder.AddSource(serverConfig.Value.Name); | ||
| }); | ||
|
|
||
| services.AddOpenTelemetry() | ||
| .ConfigureResource(r => | ||
| { | ||
| var version = Assembly.GetExecutingAssembly()?.GetName()?.Version?.ToString(); | ||
|
|
||
| r.AddService(Constants.TOOLS_ACTIVITY_SOURCE, version) | ||
| .AddTelemetrySdk(); | ||
| }) | ||
| .UseAzureMonitorExporter(options => | ||
| { | ||
| #if DEBUG | ||
| options.EnableLiveMetrics = true; | ||
| options.Diagnostics.IsLoggingEnabled = true; | ||
| options.Diagnostics.IsLoggingContentEnabled = true; | ||
| #endif | ||
| options.ConnectionString = appInsightsConnectionString; | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tools/azsdk-cli/Azure.Sdk.Tools.Cli/Telemetry/ITelemetryService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics; | ||
| using ModelContextProtocol.Protocol; | ||
|
|
||
| namespace Azure.Sdk.Tools.Cli.Telemetry; | ||
|
|
||
| public interface ITelemetryService : IDisposable | ||
| { | ||
| ValueTask<Activity?> StartActivity(string activityName); | ||
|
|
||
| ValueTask<Activity?> StartActivity(string activityName, Implementation? clientInfo); | ||
| } |
19 changes: 19 additions & 0 deletions
19
...li/Azure.Sdk.Tools.Cli/Telemetry/InformationProvider/DefaultMachineInformationProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Azure.Sdk.Tools.Cli.Telemetry.InformationProvider; | ||
|
|
||
| /// <summary> | ||
| /// Default information provider not tied to any platform specification for DevDeviceId. | ||
| /// </summary> | ||
| internal class DefaultMachineInformationProvider(ILogger<MachineInformationProviderBase> logger) | ||
| : MachineInformationProviderBase(logger) | ||
| { | ||
| /// <summary> | ||
| /// Returns null. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public override Task<string?> GetOrCreateDeviceId() => Task.FromResult<string?>(null); | ||
| } |
18 changes: 18 additions & 0 deletions
18
...zsdk-cli/Azure.Sdk.Tools.Cli/Telemetry/InformationProvider/IMachineInformationProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Azure.Sdk.Tools.Cli.Telemetry.InformationProvider; | ||
|
|
||
| internal interface IMachineInformationProvider | ||
| { | ||
| /// <summary> | ||
| /// Gets existing or creates the device id. In case the cached id cannot be retrieved, or the | ||
| /// newly generated id cannot be cached, a value of null is returned. | ||
| /// </summary> | ||
| Task<string?> GetOrCreateDeviceId(); | ||
|
|
||
| /// <summary> | ||
| /// Gets a hash of the machine's MAC address. | ||
| /// </summary> | ||
| Task<string> GetMacAddressHash(); | ||
| } |
41 changes: 41 additions & 0 deletions
41
...-cli/Azure.Sdk.Tools.Cli/Telemetry/InformationProvider/LinuxMachineInformationProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Runtime.Versioning; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Azure.Sdk.Tools.Cli.Telemetry.InformationProvider; | ||
|
|
||
| [SupportedOSPlatform("linux")] | ||
|
|
||
| internal class LinuxMachineInformationProvider(ILogger<LinuxMachineInformationProvider> logger) : UnixMachineInformationProvider(logger) | ||
| { | ||
| private const string PrimaryPathEnvVar = "XDG_CACHE_HOME"; | ||
| private const string SecondaryPathSubDirectory = ".cache"; | ||
|
|
||
| /// <summary> | ||
| /// Gets the base folder for the cache to be stored. | ||
| /// The final path should be $HOME\.cache\Microsoft\DeveloperTools. | ||
| /// </summary> | ||
| public override string GetStoragePath() | ||
| { | ||
| var userDir = Environment.GetEnvironmentVariable(PrimaryPathEnvVar); | ||
|
|
||
| // If this comes back as null or empty/whitespace, try environment variable. | ||
| if (string.IsNullOrWhiteSpace(userDir)) | ||
| { | ||
| var rootPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
|
|
||
| // If the secondary path is still null/empty/whitespace, then throw as it will lead | ||
| // to us caching the data in the wrong directory otherwise. | ||
| if (string.IsNullOrWhiteSpace(rootPath)) | ||
| { | ||
| throw new InvalidOperationException("linux: Unable to get UserProfile or $HOME folder."); | ||
| } | ||
|
|
||
| userDir = Path.Combine(rootPath, SecondaryPathSubDirectory); | ||
| } | ||
|
|
||
| return userDir; | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
...cli/Azure.Sdk.Tools.Cli/Telemetry/InformationProvider/MacOSXMachineInformationProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Runtime.Versioning; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Azure.Sdk.Tools.Cli.Telemetry.InformationProvider; | ||
|
|
||
| [SupportedOSPlatform("osx")] | ||
| internal class MacOSXMachineInformationProvider(ILogger<MacOSXMachineInformationProvider> logger) | ||
| : UnixMachineInformationProvider(logger) | ||
| { | ||
| /// <summary> | ||
| /// Retrieves the storage path for application data on macOS. | ||
| /// </summary> | ||
| /// <remarks>This method determines the appropriate storage directory by first attempting to retrieve the | ||
| /// user's profile directory using <see cref="Environment.SpecialFolder.UserProfile"/>. If that is unavailable, it | ||
| /// falls back to the "HOME" environment variable. If neither is available, an <see | ||
| /// cref="InvalidOperationException"/> is thrown. The returned path is typically located under the | ||
| /// "Library/Application Support" directory within the user's home folder.</remarks> | ||
| /// <returns>A string representing the full path to the "Library/Application Support" directory in the user's home folder. | ||
| /// </returns> | ||
| /// <exception cref="InvalidOperationException">Thrown if the user's profile directory and the "HOME" | ||
| /// environment variable are both unavailable or invalid.</exception> | ||
| public override string GetStoragePath() | ||
| { | ||
| var userDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
|
|
||
| // If this comes back as null or empty/whitespace, try environment variable. | ||
| if (string.IsNullOrWhiteSpace(userDir)) | ||
| { | ||
| userDir = Environment.GetEnvironmentVariable("HOME"); | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(userDir)) | ||
| { | ||
| throw new InvalidOperationException("macOS: Unable to get UserProfile or $HOME folder."); | ||
| } | ||
|
|
||
| var subdirectoryPath = Path.Combine(userDir, "Library", "Application Support"); | ||
| return subdirectoryPath; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.