-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSemanticKernelTelemetryExtensions.cs
More file actions
43 lines (38 loc) · 1.55 KB
/
SemanticKernelTelemetryExtensions.cs
File metadata and controls
43 lines (38 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
namespace ChatApp.WebApi
{
// Adds OpenTelemetry support for the Semantic Kernel.
// As the semantic conventions for gen_ai telemetry are still experimental,
// it needs to be enabled with an App Context switch.
// Sensitive mode will add the chat messages to the telemetry, which is useful
// for debugging but should not be used in production.
// For more details see https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Demos/TelemetryWithAppInsights/README.md
public static class SemanticKernelTelemetryExtensions
{
public static IHostApplicationBuilder AddSemanticKernelTelemetry(this IHostApplicationBuilder builder)
{
if (builder.Environment.IsDevelopment())
{
AppContext.SetSwitch("Microsoft.SemanticKernel.Experimental.GenAI.EnableOTelDiagnosticsSensitive", true);
}
else
{
AppContext.SetSwitch("Microsoft.SemanticKernel.Experimental.GenAI.EnableOTelDiagnostics", true);
}
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddMeter("Microsoft.SemanticKernel*")
.AddMeter("Azure.*");
})
.WithTracing(tracing =>
{
tracing.AddSource("Microsoft.SemanticKernel.*")
.AddSource("Azure.*")
.AddSource("Microsoft.ML.*");
});
return builder;
}
}
}