Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.12.0" />
<PackageReference Include="Microsoft.Graph" Version="5.79.0" />
<PackageReference Include="OpenTelemetry" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Azure.Identity" Version="1.14.2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.IO;

namespace Azure.Sdk.Tools.Cli.Configuration;

public static class Constants
Expand All @@ -18,4 +16,6 @@ public static class Constants
public const string AZURE_SDK_TOOLS_PATH = "azure-sdk-tools";
public const string AZURE_COMMON_LABELS_PATH = "tools/github/data/common-labels.csv";
public const string AZURE_CODEOWNERS_PATH = ".github/CODEOWNERS";

public const string TOOLS_ACTIVITY_SOURCE = "azsdk.tools";
}
26 changes: 26 additions & 0 deletions tools/azsdk-cli/Azure.Sdk.Tools.Cli/Core/TelemetryProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using OpenTelemetry;
using System.Diagnostics;

namespace Azure.Sdk.Tools.Cli.Core;

public sealed class TelemetryProcessor : BaseProcessor<Activity>
{
public override void OnStart(Activity activity)
{
}

public override void OnEnd(Activity activity)
{
// TODO: Add progress/logging for MCP clients so we can see the spans in debug mode
// without it being treated as a parse failure when using AddConsoleExporter()

// Do any post-processing work here
/*
var toolName = activity.GetTagItem("mcp.tool.name") as string;
if (!string.IsNullOrEmpty(toolName))
{
activity.SetTag("CustomToolProperty", toolName);
}
*/
Comment thread
benbp marked this conversation as resolved.
}
}
18 changes: 15 additions & 3 deletions tools/azsdk-cli/Azure.Sdk.Tools.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;
using OpenTelemetry;
using OpenTelemetry.Trace;
using Azure.Sdk.Tools.Cli.Commands;
using Azure.Sdk.Tools.Cli.Core;
using Azure.Sdk.Tools.Cli.Helpers;
using Azure.Sdk.Tools.Cli.Models;
using Azure.Sdk.Tools.Cli.Services;
using Azure.Sdk.Tools.Cli.Configuration;

namespace Azure.Sdk.Tools.Cli;

Expand All @@ -33,13 +37,23 @@ public static async Task<int> Main(string[] args)
public static WebApplicationBuilder CreateAppBuilder(string[] args)
{
var isCLI = IsCLI(args);
var (outputFormat, debug) = SharedOptions.GetGlobalOptionValues(args);
var logLevel = debug ? LogLevel.Debug : LogLevel.Information;

// Any args that ASP.NET doesn't recognize will be _ignored_ by the CreateBuilder, so we don't need to ONLY
// pass unmatched ASP.NET config values like --ASPNET_URLS to the builder. It'll just quietly ignore everything
// it doesn't recognize.
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

var (outputFormat, debug) = SharedOptions.GetGlobalOptionValues(args);
builder.Services.AddOpenTelemetry()
.WithTracing(b => {
b.AddSource(Constants.TOOLS_ACTIVITY_SOURCE)
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddProcessor(new TelemetryProcessor());
if (debug) { b.AddConsoleExporter(); }
})
.UseOtlpExporter();

// Log everything to stderr in mcp mode so the client doesn't try to interpret stdout messages that aren't json rpc
var logErrorThreshold = isCLI ? LogLevel.Error : LogLevel.Debug;
Expand All @@ -62,8 +76,6 @@ public static WebApplicationBuilder CreateAppBuilder(string[] args)
});

// add the console logger
var logLevel = debug ? LogLevel.Debug : LogLevel.Information;

builder.Services.AddLogging(l =>
{
l.AddConsole();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public async Task<ExampleServiceResponse> DemonstrateAzureService(string? tenant
var credential = azureService.GetCredential(tenantId);

// Get token for demonstration (but don't log the actual token)
var tokenResult = await credential.GetTokenAsync(new Core.TokenRequestContext(["https://management.azure.com/.default"]), ct);
var tokenResult = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(["https://management.azure.com/.default"]), ct);

var details = new Dictionary<string, string>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using Azure.Sdk.Tools.Cli.Services;
using Azure.Sdk.Tools.Cli.Configuration;
using System.Text.Json;

namespace Azure.Sdk.Tools.Cli.Tools;

public class InstrumentedTool(ILogger logger, McpServerTool innerTool, string toolName) : DelegatingMcpServerTool(innerTool)
{
private static readonly ActivitySource source = new(Constants.TOOLS_ACTIVITY_SOURCE);

private readonly JsonSerializerOptions serializerOptions = new()
{
WriteIndented = false,
};

public override async ValueTask<CallToolResult> InvokeAsync(RequestContext<CallToolRequestParams> request, CancellationToken ct = default)
{
using var activity = source.StartActivity("tool.invoke", ActivityKind.Internal);
if (activity == null)
{
logger.LogError("Null activity created for tool {ToolName}", toolName);
Comment thread
benbp marked this conversation as resolved.
}

try
{
TelemetryService.InstrumentationBefore(logger, toolName, request.Params?.Arguments, ct);
activity?.SetTag("name", toolName);
var args = JsonSerializer.Serialize(request.Params?.Arguments, serializerOptions);
activity?.SetTag("args", args);

var result = await base.InvokeAsync(request, ct);
TelemetryService.InstrumentationAfter(logger, toolName, result, ct);

var content = JsonSerializer.Serialize(result.Content);
activity?.SetTag("result", content);
activity?.SetStatus(ActivityStatusCode.Ok);

return result;
}
catch (Exception ex)
{
TelemetryService.InstrumentationError(logger, toolName, ex, ct);
activity?.AddException(ex);
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}
}
Expand Down
Loading