Add Application Insights telemetry example (#1778)

This commit is contained in:
Loi Nguyen 2026-08-05 06:25:19 +07:00 • committed by GitHub
commit f5d54b5c29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,3 +1,4 @@
using Azure.Monitor.OpenTelemetry.AspNetCore;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
@ -34,15 +35,27 @@ builder.Services.AddMcpServer()
.WithTools<WeatherTools>()
.WithResources<SimpleResourceType>();
builder.Services.AddOpenTelemetry()
var telemetry = builder.Services.AddOpenTelemetry()
.WithTracing(b => b.AddSource("*")
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation())
.WithMetrics(b => b.AddMeter("*")
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation())
.WithLogging()
.UseOtlpExporter();
.WithLogging();
string? applicationInsightsConnectionString =
builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
if (string.IsNullOrWhiteSpace(applicationInsightsConnectionString))
{
telemetry.UseOtlpExporter();
}
else
{
telemetry.UseAzureMonitor(options =>
options.ConnectionString = applicationInsightsConnectionString);
}
// Configure HttpClientFactory for weather.gov API
builder.Services.AddHttpClient("WeatherApi", client =>