Skip to content

Adds logging LLM usage information using OpenTelemetry. Closes #1067#1167

Merged
garrytrinder merged 2 commits intodotnet:mainfrom
waldekmastykarz:otel-ai
May 13, 2025
Merged

Adds logging LLM usage information using OpenTelemetry. Closes #1067#1167
garrytrinder merged 2 commits intodotnet:mainfrom
waldekmastykarz:otel-ai

Conversation

@waldekmastykarz
Copy link
Copy Markdown
Collaborator

@waldekmastykarz waldekmastykarz commented May 8, 2025

Adds logging LLM usage information using OpenTelemetry. Closes #1067

To test

devproxyrc.json:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.27.0/rc.schema.json",
  "plugins": [
    {
      "name": "OpenAITelemetryPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
    }
  ],
  "urlsToWatch": [
    "http://localhost:11434/*"
  ],
  "logLevel": "information",
  "newVersionNotification": "stable",
  "showSkipMessages": true,
  "showTimestamps": true,
  "validateSchemas": true
}

Visualize using Aspire dashboard:

docker run --rm -it -p 18888:18888 -p 4317:18889 -p 4318:18890 --name aspire-dashboard mcr.microsoft.com/dotnet/aspire-dashboard:latest

Call Ollama using its OpenAI-compatible API:

curl -ikx http://127.0.0.1:8000 -X POST -H Content-Type: application/json -d '{"model":"llama3.2","messages":[{"role":"user","content":"What is the capital of France?"}],"stream":false}' http://localhost:11434/v1/chat/completions

In the Aspire dashboard, you'll see tracing and metrics (it takes a moment for metrics to show up).

To test cost estimation

devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.27.0/rc.schema.json",
  "plugins": [
    {
      "name": "OpenAITelemetryPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "openAITelemetryPlugin"
    }
  ],
  "urlsToWatch": [
    "http://localhost:11434/*"
  ],
  "openAITelemetryPlugin": {
    "includeCosts": true,
    "pricesFile": "llm-prices.json"
  },
  "logLevel": "information",
  "newVersionNotification": "stable",
  "showSkipMessages": true,
  "showTimestamps": true,
  "validateSchemas": true
}

llm-prices.json:

{
  "prices": {
    "llama3.2": {
      "input": 10.0,
      "output": 30.0
    }
  }
}

Call Ollama again, check out the new span with cost information and wait for additional metrics to show up in the dashboard.

@waldekmastykarz waldekmastykarz requested a review from a team as a code owner May 8, 2025 09:34
@garrytrinder
Copy link
Copy Markdown
Contributor

In the first code snippet the plugin is not enabled, is this correct? When I enable the plugin I get a warning when executing Dev Proxy from the build files.

OpenAITelemetryPlugin: File /Users/garrytrinder/repos/dev-proxy/dev-proxy/bin/Debug/net9.0 not found. No data will be provided

@waldekmastykarz
Copy link
Copy Markdown
Collaborator Author

Duh! Of course the plugin needs to be enabled. Sorry for that. Let me check why there's a warning.

@waldekmastykarz waldekmastykarz marked this pull request as draft May 12, 2025 11:03
@waldekmastykarz
Copy link
Copy Markdown
Collaborator Author

I can't repro the warning you're getting. I did notice, that the sample prices file was missing a bracket, so I updated the sample. Could you please try again and see if that might've been it?

@waldekmastykarz waldekmastykarz marked this pull request as ready for review May 12, 2025 11:20
@garrytrinder
Copy link
Copy Markdown
Contributor

Same issue unfortunately using the updated config.

image

@garrytrinder garrytrinder merged commit 3ce9cb1 into dotnet:main May 13, 2025
4 checks passed
@waldekmastykarz waldekmastykarz deleted the otel-ai branch May 13, 2025 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Count LLM tokens in Azure AI Foundry models

2 participants