mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> This PR contains changes to improve observability for Semantic Kernel users, including: - Logging: using already existing `ILogger` interface with some improvements described below. - Metering: implemented using `Meter` and `MeterListener` classes from `System.Diagnostics.Metrics` namespace. - Tracing: implemented using `Activity`, `ActivitySource` and `ActivityListener` classes from `System.Diagnostics` namespace. Added telemetry uses native .NET methods, which means that it's not dependent on specific telemetry tool. Current PR contains console application using Application Insights as example to show telemetry capabilities in Kernel. Changes include instrumentation for `SequentialPlanner` as a starting point and later there will be new PRs for other types of planners and more improvements for kernel telemetry. Also, there are updates for `LogLevel` handling across codebase to align with described log level purpose: https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loglevel?view=dotnet-plat-ext-7.0 Particularly, there are changes for `LogLevel.Trace` to be used for sensitive data and most detailed messages, which should not be enabled in prodution environments. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Changes: - Marked `.WithLogger` method in KernelBuilder as obsolete and added new method `.WithLogging`. - Added new methods `.WithMetering` and `.WithTracing` in KernelBuilder. - Improved `LogLevel` handling across codebase to cover cases of logging sensitive data. - Implemented `InstrumentedSequentialPlanner` for instrumentation using decorator pattern. - Extended `InvokeAsync` method in `Plan` for instrumentation. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) - [x] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with `dotnet format` - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Shawn Callegari <[email protected]>
193 lines
7.4 KiB
C#
193 lines
7.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Diagnostics;
|
|
using System.Diagnostics.Metrics;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.ApplicationInsights;
|
|
using Microsoft.ApplicationInsights.DataContracts;
|
|
using Microsoft.ApplicationInsights.Extensibility;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.ApplicationInsights;
|
|
using Microsoft.SemanticKernel;
|
|
using Microsoft.SemanticKernel.Planning;
|
|
using Microsoft.SemanticKernel.Planning.Sequential;
|
|
|
|
/// <summary>
|
|
/// Example of telemetry in Semantic Kernel using Application Insights within console application.
|
|
/// </summary>
|
|
public sealed class Program
|
|
{
|
|
/// <summary>
|
|
/// Log level to be used by <see cref="ILogger"/>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <see cref="LogLevel.Information"/> is set by default. <para />
|
|
/// <see cref="LogLevel.Trace"/> will enable logging with more detailed information, including sensitive data. Should not be used in production. <para />
|
|
/// </remarks>
|
|
private static LogLevel LogLevel = LogLevel.Information;
|
|
|
|
public static async Task Main()
|
|
{
|
|
var serviceProvider = GetServiceProvider();
|
|
|
|
var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();
|
|
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
|
|
|
|
using var meterListener = new MeterListener();
|
|
using var activityListener = new ActivityListener();
|
|
|
|
ConfigureMetering(meterListener, telemetryClient);
|
|
ConfigureTracing(activityListener, telemetryClient);
|
|
|
|
var kernel = GetKernel(logger);
|
|
var planner = GetPlanner(kernel, logger);
|
|
|
|
try
|
|
{
|
|
using var operation = telemetryClient.StartOperation<DependencyTelemetry>("ApplicationInsights.Example");
|
|
|
|
Console.WriteLine("Operation/Trace ID:");
|
|
Console.WriteLine(Activity.Current?.TraceId);
|
|
|
|
var plan = await planner.CreatePlanAsync("Write a poem about John Doe, then translate it into Italian.");
|
|
|
|
Console.WriteLine("Original plan:");
|
|
Console.WriteLine(plan.ToPlanString());
|
|
|
|
var result = await kernel.RunAsync(plan);
|
|
|
|
Console.WriteLine("Result:");
|
|
Console.WriteLine(result.Result);
|
|
}
|
|
finally
|
|
{
|
|
// Explicitly call Flush() followed by sleep is required in console apps.
|
|
// This is to ensure that even if application terminates, telemetry is sent to the back-end.
|
|
telemetryClient.Flush();
|
|
await Task.Delay(5000);
|
|
}
|
|
}
|
|
|
|
private static ServiceProvider GetServiceProvider()
|
|
{
|
|
var services = new ServiceCollection();
|
|
|
|
ConfigureApplicationInsightsTelemetry(services);
|
|
|
|
return services.BuildServiceProvider();
|
|
}
|
|
|
|
private static void ConfigureApplicationInsightsTelemetry(ServiceCollection services)
|
|
{
|
|
string instrumentationKey = Env.Var("ApplicationInsights__InstrumentationKey");
|
|
|
|
services.AddLogging(loggingBuilder =>
|
|
{
|
|
loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>(typeof(Program).FullName, LogLevel);
|
|
loggingBuilder.SetMinimumLevel(LogLevel);
|
|
});
|
|
|
|
services.AddApplicationInsightsTelemetryWorkerService(options =>
|
|
{
|
|
options.ConnectionString = $"InstrumentationKey={instrumentationKey}";
|
|
});
|
|
}
|
|
|
|
private static IKernel GetKernel(ILogger logger)
|
|
{
|
|
string folder = RepoFiles.SampleSkillsPath();
|
|
|
|
var kernel = new KernelBuilder()
|
|
.WithLogger(logger)
|
|
.WithAzureChatCompletionService(
|
|
Env.Var("AzureOpenAI__ChatDeploymentName"),
|
|
Env.Var("AzureOpenAI__Endpoint"),
|
|
Env.Var("AzureOpenAI__ApiKey"))
|
|
.Build();
|
|
|
|
kernel.ImportSemanticSkillFromDirectory(folder, "SummarizeSkill", "WriterSkill");
|
|
|
|
return kernel;
|
|
}
|
|
|
|
private static ISequentialPlanner GetPlanner(
|
|
IKernel kernel,
|
|
ILogger logger,
|
|
int maxTokens = 1024)
|
|
{
|
|
var plannerConfig = new SequentialPlannerConfig { MaxTokens = maxTokens };
|
|
|
|
return new SequentialPlanner(kernel, plannerConfig).WithInstrumentation(logger);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Example of metering configuration in Application Insights
|
|
/// using <see cref="MeterListener"/> to attach for <see cref="Meter"/> recordings.
|
|
/// </summary>
|
|
/// <param name="meterListener">Instance of <see cref="MeterListener"/> for metering configuration.</param>
|
|
/// <param name="telemetryClient">Instance of Application Insights <see cref="TelemetryClient"/>.</param>
|
|
private static void ConfigureMetering(MeterListener meterListener, TelemetryClient telemetryClient)
|
|
{
|
|
meterListener.InstrumentPublished = (instrument, listener) =>
|
|
{
|
|
// Subscribe to all metrics in Semantic Kernel
|
|
if (instrument.Meter.Name.StartsWith("Microsoft.SemanticKernel", StringComparison.Ordinal))
|
|
{
|
|
listener.EnableMeasurementEvents(instrument);
|
|
}
|
|
};
|
|
|
|
MeasurementCallback<double> measurementCallback = (instrument, measurement, tags, state) =>
|
|
{
|
|
telemetryClient.GetMetric(instrument.Name).TrackValue(measurement);
|
|
};
|
|
|
|
meterListener.SetMeasurementEventCallback(measurementCallback);
|
|
|
|
meterListener.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Example of advanced distributed tracing configuration in Application Insights
|
|
/// using <see cref="ActivityListener"/> to attach for <see cref="Activity"/> events.
|
|
/// </summary>
|
|
/// <param name="activityListener">Instance of <see cref="ActivityListener"/> for tracing configuration.</param>
|
|
/// <param name="telemetryClient">Instance of Application Insights <see cref="TelemetryClient"/>.</param>
|
|
private static void ConfigureTracing(ActivityListener activityListener, TelemetryClient telemetryClient)
|
|
{
|
|
var operations = new ConcurrentDictionary<string, IOperationHolder<DependencyTelemetry>>();
|
|
|
|
// For more detailed tracing we need to attach Activity entity to Application Insights operation manually.
|
|
Action<Activity> activityStarted = activity =>
|
|
{
|
|
var operation = telemetryClient.StartOperation<DependencyTelemetry>(activity);
|
|
operation.Telemetry.Type = activity.Kind.ToString();
|
|
|
|
operations.TryAdd(activity.TraceId.ToString(), operation);
|
|
};
|
|
|
|
// We also need to manually stop Application Insights operation when Activity entity is stopped.
|
|
Action<Activity> activityStopped = activity =>
|
|
{
|
|
if (operations.TryRemove(activity.TraceId.ToString(), out var operation))
|
|
{
|
|
telemetryClient.StopOperation(operation);
|
|
}
|
|
};
|
|
|
|
// Subscribe to all traces in Semantic Kernel
|
|
activityListener.ShouldListenTo =
|
|
activitySource => activitySource.Name.StartsWith("Microsoft.SemanticKernel", StringComparison.Ordinal);
|
|
|
|
activityListener.Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData;
|
|
activityListener.SampleUsingParentId = (ref ActivityCreationOptions<string> _) => ActivitySamplingResult.AllData;
|
|
activityListener.ActivityStarted = activityStarted;
|
|
activityListener.ActivityStopped = activityStopped;
|
|
|
|
ActivitySource.AddActivityListener(activityListener);
|
|
}
|
|
}
|