semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: New Feature: Feature Request: Add OpenTelemetry GenAI instrumentation sample & docs

Open dineshkumarkummara opened this issue 1 year ago • 5 comments


name: Feature request about: Suggest an idea for this project


Summary

otel-genai-bridges provides a .NET library (SkOtel) that adds both ASP.NET Core middleware and a DelegatingHandler to emit OpenTelemetry Generative AI telemetry for Semantic Kernel apps. Surfacing this as a community sample in the SK docs would give teams a ready-made path to end-to-end GenAI observability.

Key features

  • AddSemanticKernelTelemetry extension registers options, middleware, and handler
  • Captures prompts/completions/tool invocations, tokens, latency, cost, and RAG retrieval latency
  • Bundled sample (dotnet/samples/sk-chat) plus Dockerized Collector → Tempo/Prometheus → Grafana stack

Snippet

builder.Services.AddSemanticKernelTelemetry(options =>
{
    options.System = "azure.ai.openai";
    options.Model = "gpt-4o";
    options.CapturePrompts = true;
    options.CaptureCompletions = true;
});

builder.Services.AddOpenTelemetry()
    .ConfigureResource(r => r.AddService("sk-chat"))
    .WithTracing(t => t
        .AddSource("otel-genai-bridges/semantic-kernel")
        .AddAspNetCoreInstrumentation()
        .AddOtlpExporter());

Grafana token throughput panel

Proposal

Add a docs/sample entry referencing this instrumentation so developers can adopt OTEL GenAI semantics today. Repository: https://github.com/dineshkumarkummara/otel-genai-bridges

dineshkumarkummara avatar Oct 08 '25 21:10 dineshkumarkummara

@dineshkumarkummara Thanks for creating this issue. Would you be interested in creating a sample that demonstrates this functionality?

markwallace-microsoft avatar Oct 17 '25 14:10 markwallace-microsoft

Hi @markwallace-microsoft 👋 — per your ask I fleshed out a complete Semantic Kernel sample that emits the OpenTelemetry GenAI semantics end to end.

Sample + docs

  • Code lives in dotnet/samples/sk-chat with middleware/delegating-handler wiring from dotnet/libs/sk-otel/*.
  • New README: https://github.com/dineshkumarkummara/otel-genai-bridges/blob/docs/add-otel-genai-guide/dotnet/samples/sk-chat/README.md walks through prerequisites, launch commands, and what telemetry to expect.
  • PR tracking it: https://github.com/dineshkumarkummara/otel-genai-bridges/pull/1

How to run it

  1. git clone https://github.com/dineshkumarkummara/otel-genai-bridges.git
  2. cd otel-genai-bridges
  3. (Ensure Docker Desktop is running, since the stack brings up Tempo/Prometheus/Grafana.)
  4. ./scripts/run_all.sh
    • Builds the Java RAG sample + SK .NET sample, spins up the OTLP collector, and serves the chat API at http://localhost:7080.
    • Curl helper: curl -s http://localhost:7080/chat -H "Content-Type: application/json" -d '{"question":"How do GenAI semantic conventions work?"}' | jq
    • Grafana dashboard auto-opens at http://localhost:3000/d/genai-overview so you can watch spans/metrics/tokens in real time.

If you just need the .NET side you can run dotnet run --project dotnet/samples/sk-chat/SkChat.csproj --urls http://localhost:7080; it defaults OTEL_EXPORTER_OTLP_ENDPOINT to http://localhost:4317 so pointing at any collector is straightforward.

Verification done on my end

  • git pull origin main
  • dotnet build dotnet/libs/sk-otel/SkOtel.csproj
  • dotnet build dotnet/samples/sk-chat/SkChat.csproj
  • dotnet test dotnet/libs/sk-otel.Tests/SkOtel.Tests.csproj
  • ./mvnw -f java/pom.xml package -DskipTests
  • ./scripts/run_all.sh (completes the build steps; container startup requires Docker Desktop running)

Let me know if you’d like the sample mirrored directly under the microsoft/semantic-kernel repo as well — happy to open a PR there next.

dineshkumarkummara avatar Oct 21 '25 14:10 dineshkumarkummara

Quick follow-up: reran ./scripts/run_all.sh with Docker Desktop up and verified the integrated stack end-to-end. The helper curls return:

/chat -> {"answer":"Telemetry friendly response for: How do GenAI semantic conventions work?","tokens":{"input":6,"output":10}}
/rag  -> {"answer":"Telemetry friendly response for: Use retrieval augmented generation to answer: Explain OTEL GenAI telemetry"}

Containers spin up cleanly (Tempo, Prometheus, Grafana, Collector, Java RAG service, SK chat API) and tear down when the script exits. Grafana auto-loads the genai-overview dashboard for tracing/metrics inspection.

dineshkumarkummara avatar Oct 21 '25 14:10 dineshkumarkummara

This issue is stale because it has been open for 90 days with no activity.

github-actions[bot] avatar Jan 20 '26 02:01 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Feb 06 '26 02:02 github-actions[bot]