Skip to main content

Semantic Kernel–OpenTelemetry integration in C#

I already showed in a previous post how you could integrate Semantic Kernel with the .NET Core LoggerFactory to see what is going on while interacting with your OpenAI backend.

Here is the link in case you missed it: Debugging Semantic Kernel in C# (bartwullems.blogspot.com).

An even better solution is to use the OpenTelemetry integration. Therefore we need to create a LoggerFactory instance that uses OpenTelemetry as a logging provider:

using var loggerFactory = LoggerFactory.Create(builder =>
{
// Add OpenTelemetry as a logging provider
builder.AddOpenTelemetry(options =>
{
options.AddOtlpExporter();
// Format log messages. This defaults to false.
options.IncludeFormattedMessage = true;
options.IncludeScopes = true;
});
builder.SetMinimumLevel(LogLevel.Trace);
});
view raw Program.cs hosted with ❤ by GitHub

Now we need to register this LoggerFactory as a service of the Semantic Kernel builder:

var semanticKernelBuilder = Kernel.CreateBuilder()
.AddOpenAIChatCompletion( // We use Semantic Kernel OpenAI API
modelId: "phi3",
apiKey: null,
endpoint: new Uri("http://localhost:11434"),
httpClient: client);// With Ollama OpenAI API endpoint
semanticKernelBuilder.Services.AddSingleton(loggerFactory);
view raw Program.cs hosted with ❤ by GitHub

If we now take a look at our Aspire Dashboard, we could see the logged messages appear:

It is also possible to collect any related metrics and traces. Therefore add the following code to your Program.cs:

using var traceProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("Microsoft.SemanticKernel*")
.AddOtlpExporter()
.Build();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("Microsoft.SemanticKernel*")
.AddOtlpExporter()
.Build();
view raw Program.cs hosted with ❤ by GitHub

If we now take a look at the Aspire Dashboard, we can see both the metrics and the end-2-end trace:




Popular posts from this blog

Kubernetes–Limit your environmental impact

Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

.NET 9 - Goodbye sln!

Although the csproj file evolved and simplified a lot over time, the Visual Studio solution file (.sln) remained an ugly file format full of magic GUIDs. With the latest .NET 9 SDK(9.0.200), we finally got an alternative; a new XML-based solution file(.slnx) got introduced in preview. So say goodbye to this ugly sln file: And meet his better looking slnx brother instead: To use this feature we first have to enable it: Go to Tools -> Options -> Environment -> Preview Features Check the checkbox next to Use Solution File Persistence Model Now we can migrate an existing sln file to slnx using the following command: dotnet sln migrate AICalculator.sln .slnx file D:\Projects\Test\AICalculator\AICalculator.slnx generated. Or create a new Visual Studio solution using the slnx format: dotnet new sln --format slnx The template "Solution File" was created successfully. The new format is not yet recognized by VSCode but it does work in Jetbr...