dotnet/docs/TELEMETRY.md
Telemetry in Semantic Kernel (SK) .NET implementation includes logging, metering and tracing. The code is instrumented using native .NET instrumentation tools, which means that it's possible to use different monitoring platforms (e.g. Application Insights, Aspire dashboard, Prometheus, Grafana etc.).
Code example using Application Insights can be found here.
The logging mechanism in this project relies on the ILogger interface from the Microsoft.Extensions.Logging namespace. Recent updates have introduced enhancements to the logger creation process. Instead of directly using the ILogger interface, instances of ILogger are now recommended to be created through an ILoggerFactory configured through a ServiceCollection.
By employing the ILoggerFactory approach, logger instances are generated with precise type information, facilitating more accurate logging and streamlined control over log filtering across various classes.
Log levels used in SK:
Enable logging for Kernel instance:
IKernelBuilder builder = Kernel.CreateBuilder();
// Assuming loggerFactory is already defined.
builder.Services.AddSingleton(loggerFactory);
...
var kernel = builder.Build();
All kernel functions and planners will be instrumented. It includes logs, metering and tracing.
Log filtering configuration has been refined to strike a balance between visibility and relevance:
using var loggerFactory = LoggerFactory.Create(builder =>
{
// Add OpenTelemetry as a logging provider
builder.AddOpenTelemetry(options =>
{
// Assuming connectionString is already defined.
options.AddAzureMonitorLogExporter(options => options.ConnectionString = connectionString);
// Format log messages. This is default to false.
options.IncludeFormattedMessage = true;
});
builder.AddFilter("Microsoft", LogLevel.Warning);
builder.AddFilter("Microsoft.SemanticKernel", LogLevel.Information);
}
Read more at: https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/logs/customizing-the-sdk/README.md
Metering is implemented with Meter class from System.Diagnostics.Metrics namespace.
Available meters:
semantic_kernel.planning.create_plan.duration (Histogram) - execution time of plan creation (in seconds)semantic_kernel.planning.invoke_plan.duration (Histogram) - execution time of plan execution (in seconds)KernelFunction. List of metrics:
semantic_kernel.function.invocation.duration (Histogram) - function execution time (in seconds)semantic_kernel.function.streaming.duration (Histogram) - function streaming execution time (in seconds)semantic_kernel.function.invocation.token_usage.prompt (Histogram) - number of prompt token usage (only for KernelFunctionFromPrompt)semantic_kernel.function.invocation.token_usage.completion (Histogram) - number of completion token usage (only for KernelFunctionFromPrompt)semantic_kernel.connectors.openai.tokens.prompt (Counter) - number of prompt tokens used.semantic_kernel.connectors.openai.tokens.completion (Counter) - number of completion tokens used.semantic_kernel.connectors.openai.tokens.total (Counter) - total number of tokens used.Measurements will be associated with tags that will allow data to be categorized for analysis:
TagList tags = new() { { "semantic_kernel.function.name", this.Name } };
s_invocationDuration.Record(duration.TotalSeconds, in tags);
Depending on monitoring tool, there are different ways how to subscribe to available meters. Following example shows how to subscribe to available meters and export metrics to Application Insights using OpenTelemetry.Sdk:
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("Microsoft.SemanticKernel*")
.AddAzureMonitorMetricExporter(options => options.ConnectionString = connectionString)
.Build();
Read more at: https://learn.microsoft.com/en-us/azure/azure-monitor/app/opentelemetry-enable?tabs=net
Read more at: https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/metrics/customizing-the-sdk/README.md
Tracing is implemented with Activity class from System.Diagnostics namespace.
Available activity sources:
KernelFunction as well as requests to models.Subscribe to available activity sources using OpenTelemetry.Sdk:
using var traceProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("Microsoft.SemanticKernel*")
.AddAzureMonitorTraceExporter(options => options.ConnectionString = connectionString)
.Build();
Read more at: https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/trace/customizing-the-sdk/README.md