Add Langfuse diagnostics and GiteeAI plugin
User description
Introduced OpenTelemetry-based model diagnostics with Langfuse integration, including new helper classes and activity tracing for agent and function execution. Added BotSharp.Plugin.GiteeAI with chat and embedding providers, and updated solution/project files to register the new plugin. Enhanced tracing in routing, executor, and controller logic for improved observability.
PR Type
Enhancement, Documentation
Description
-
Implemented OpenTelemetry-based model diagnostics with Langfuse integration for improved observability
-
Added GiteeAI plugin with chat completion and text embedding providers
-
Enhanced tracing in routing, executor, and controller logic for agent and function execution
-
Integrated activity tracking with semantic conventions for GenAI operations
Diagram Walkthrough
flowchart LR
A["OpenTelemetry Setup"] --> B["ModelDiagnostics Helper"]
B --> C["Activity Tracing"]
C --> D["Chat Providers"]
C --> E["Function Executors"]
C --> F["Routing Service"]
G["GiteeAI Plugin"] --> D
H["Langfuse Config"] --> A
File Walkthrough
| Relevant files | |||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Configuration changes | 6 files
| ||||||||||||||||||||||||||||||||||
| Enhancement | 17 files
| ||||||||||||||||||||||||||||||||||
| Documentation | 1 files
| ||||||||||||||||||||||||||||||||||
| Formatting | 1 files
|
PR Compliance Guide 🔍
Below is a summary of compliance checks for this PR:
| Security Compliance | |||||||||||
| ⚪ | Sensitive data in tracesDescription: Diagnostic switches enable sensitive events which may capture and export user Referred Code
| ||||||||||
Insecure auth transportDescription: Constructs Basic Auth header for OTLP exporter to Langfuse and sends it over HTTP Referred Code
| |||||||||||
| Ticket Compliance | |||||||||||
| ⚪ | 🎫 No ticket provided
Codebase Duplication Compliance | ⚪ | Codebase context is not definedFollow the guide to enable codebase context checks. Custom Compliance | ⚪ | No custom compliance providedFollow the guide to enable custom compliance check.
|
| |||||
Compliance status legend
🟢 - Fully Compliant🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label
PR Code Suggestions ✨
Explore these optional code suggestions:
| Category | Suggestion | Impact |
| High-level |
Refactor providers to reduce duplicationThe Examples:src/Plugins/BotSharp.Plugin.GiteeAI/Providers/Chat/ChatCompletionProvider.cs [15-100]
src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs [35-119]
Solution Walkthrough:Before:
After:
Suggestion importance[1-10]: 9__ Why: The suggestion correctly identifies significant code duplication across the new | High |
| Possible issue |
Fix incorrect null checkReplace the incorrect null check on src/BotSharp.ServiceDefaults/Extensions.cs [129-130]
Suggestion importance[1-10]: 8__ Why: The suggestion correctly identifies that | Medium |
Validate Langfuse configuration valuesAdd validation to ensure src/BotSharp.ServiceDefaults/Extensions.cs [139-143]
Suggestion importance[1-10]: 7__ Why: The suggestion correctly points out that missing configuration values will lead to a | Medium | |
| General |
✅
| Low |
✅
| Low | |
| ||
This is the result of integrating OpenTelemetry with Langfuse.
The integration enables real-time tracing and streaming collection of inputs, outputs, execution states, and exceptions for each node in complex workflows, enhancing observability for debugging and performance optimization
This is the result of integrating OpenTelemetry with Langfuse. The integration enables real-time tracing and streaming collection of inputs, outputs, execution states, and exceptions for each node in complex workflows, enhancing observability for debugging and performance optimization
Is there any setting to turn on/off the OpenTelemetry with Langfuse?
This is the result of integrating OpenTelemetry with Langfuse. The integration enables real-time tracing and streaming collection of inputs, outputs, execution states, and exceptions for each node in complex workflows, enhancing observability for debugging and performance optimization
Is there any setting to turn on/off the OpenTelemetry with Langfuse? I will refactor this part of the code.
High-Level Objectives
- Introduce generalized OpenTelemetry (OTel) model diagnostics with semantic conventions for GenAI operations.
- Integrate Langfuse tracing (refactoring away from the prior dedicated BotSharp.Diagnostics.Langfuse project into a unified ModelDiagnostics approach).
- Add a new plugin: BotSharp.Plugin.GiteeAI (chat completion + text embedding providers).
- Extend tracing coverage across:
- ConversationController (incoming requests)
- RoutingService (agent selection and function dispatch)
- FunctionCallbackExecutor and MCPToolExecutor (tool/function execution)
- Model providers (Azure OpenAI, OpenAI, and newly added GiteeAI) with activity spans for prompt/response lifecycles.
- Enhance configuration (appsettings.json) to include Langfuse and GiteeAI model entries.
- Provide initial plugin documentation in README.md.
- Prepare architecture diagrams and walkthrough to clarify the tracing and provider orchestration flow.
Architectural Evolution
Previously, Langfuse integration appears to have been isolated (e.g., via a specialized diagnostics project). This PR consolidates diagnostic responsibilities into:
- ModelDiagnostics.cs: A single, extensible instrumentation layer
- ActivityExtensions.cs: Shared helpers for span enrichment (e.g., tags for model name, input length, output length, latency, tool invocation context) The removal/refactoring of BotSharp.Diagnostics.Langfuse (as indicated in your request) results in a cleaner, provider-agnostic tracing path.
Core Added / Modified Components
Diagnostics & Tracing Foundation
- ActivityExtensions.cs (≈119 additions): Adds extension methods to attach standardized attributes (e.g., genai.system, genai.model, usage tokens, error info).
- AppContextSwitchHelper.cs: Utility for reading runtime feature toggles tied to diagnostics.
- Extensions.cs: Registers OpenTelemetry, sets up exporters (Langfuse), and configures ActivitySource(s) to emit spans.
Routing & Execution Instrumentation
- RoutingService (partial modifications in RoutingService.cs, InvokeAgent.cs, InvokeFunction.cs): Injects spans around agent resolution, function decision logic, and fallback/branching behaviors.
- FunctionCallbackExecutor.cs / MCPToolExecutor.cs: Wrap function/micro-tool execution inside activities, capturing input/output payload summaries and potential exception traces.
Controller Layer
- ConversationController.cs: Starts top-level request activities (entry points), enabling correlation IDs and trace continuity across chained model/tool operations.
Provider Enhancements
Instrumentation added (or refactored) to:
- ChatCompletionProvider.cs (multiple instances for different providers; diffs show added tracing blocks wrapping send/receive phases)
- GiteeAI ChatCompletionProvider.cs: Full new implementation (≈496 lines) with prompt assembly, response parsing, diagnostic events.
- TextEmbeddingProvider.cs (GiteeAI): Embedding generation with trace spans for vectorization operations.
- ProviderHelper.cs: Centralizes client instantiation (e.g., constructing a configured HTTP client for GiteeAI with retries or headers).
GiteeAI Plugin Infrastructure
- BotSharp.Plugin.GiteeAI.csproj: New project definition.
- GiteeAiPlugin.cs: Dependency injection (DI) registration of chat and embedding providers.
- Using.cs: Global using directives for plugin namespace cohesion.
- BotSharp.sln / WebStarter.csproj: Solution and startup integration of the plugin.
Configuration & Settings
- appsettings.json: Adds Langfuse credentials/config blocks and GiteeAI model definitions (plus adjustments for existing OpenAI/Azure OpenAI entries). Expansions suggest enabling multi-provider side-by-side tracing comparisons.
Documentation
- README.md: Introduces usage/registration details for the GiteeAI plugin (likely minimal initial documentation, flagged for future expansion).
Formatting / Minor Adjustments
- Program.cs: Minor cleanup (reordered usings, commented MCP service block temporarily).
Mermaid Diagram Walkthrough
1. High-Level Data & Trace Flow
flowchart LR
Request["HTTP Request /conversation"] --> CC["ConversationController"]
CC --> RT["RoutingService"]
RT --> AGT["Agent Selection"]
AGT --> DEC{"Dispatch"}
DEC --> CHAT["ChatCompletion Provider (Azure/OpenAI/GiteeAI)"]
DEC --> FUNC["FunctionCallbackExecutor"]
DEC --> MCP["MCPToolExecutor"]
CHAT --> DIAG["ModelDiagnostics"]
FUNC --> DIAG
MCP --> DIAG
DIAG --> OTEL["OpenTelemetry ActivitySource"]
OTEL --> LANG["Langfuse Exporter"]
OTEL --> OTHER["Other OTEL Exporters (e.g., Console, OTLP)"]
2. Detailed Sequence (Conversation -> Agent -> Provider -> Trace)
sequenceDiagram
participant User
participant Controller as ConversationController
participant Router as RoutingService
participant Agent as Agent/Policy
participant Provider as Model Provider (Azure/OpenAI/GiteeAI)
participant Exec as Function/MCP Executor
participant Diag as ModelDiagnostics
participant OTel as OpenTelemetry
participant Lang as Langfuse
User->>Controller: Chat/Task request
Controller->>Diag: Start root Activity (request span)
Controller->>Router: Route(message, context)
Router->>Agent: Evaluate agent(s)
Agent-->>Router: Selected agent + strategy
Router->>Diag: Start agent invocation span
Router->>Provider: Generate response (chat or embedding)
Provider->>Diag: Start model operation span
Provider-->>Diag: Emit attributes (model, tokens, latency, etc.)
Diag->>OTel: Activity emission
OTel->>Lang: Export spans (Langfuse sink)
alt Needs tool/function
Router->>Exec: Invoke function/MCP tool
Exec->>Diag: Start tool execution span
Exec-->>Diag: Output / error metadata
end
Provider-->>Router: Response
Router-->>Controller: Aggregated result
Controller-->>User: Final structured reply
File Walkthrough (Categorized)
Configuration & Bootstrapping
- Extensions.cs: Central OTel + Langfuse registration logic
- appsettings.json: Adds Langfuse settings block and multi-model provider configuration (Azure OpenAI, OpenAI, GiteeAI)
- Program.cs: Minor startup adjustments (commented MCP service portion)
- BotSharp.sln / WebStarter.csproj: Solution integration of GiteeAI plugin project
Diagnostics Core
- ModelDiagnostics.cs: Heart of instrumentation (chat, embedding, function, agent spans)
- ActivityExtensions.cs: Tagging helpers (model name, message counts, timings, error classification)
- AppContextSwitchHelper.cs: Toggle-based feature enabling (e.g., conditional tracing intensity)
Routing & Execution
- RoutingService.cs / InvokeAgent.cs / InvokeFunction.cs: Introduces or expands span boundaries around decision phases
- FunctionCallbackExecutor.cs / MCPToolExecutor.cs: Wraps function/micro-tool invocation with structured activities
Providers
- ChatCompletionProvider.cs (multiple provider contexts including Azure/OpenAI): Inject span start/stop, response metrics
- GiteeAI:
- ChatCompletionProvider.cs: New provider with full instrumentation
- TextEmbeddingProvider.cs: Embedding generation + trace capture
- ProviderHelper.cs: Client construction utilities (e.g., auth/config composition)
- Using.cs & GiteeAiPlugin.cs: Plugin-level DI and global usings
Documentation
- README.md: Adds initial GiteeAI plugin usage or description
Miscellaneous
- Minor formatting (Program.cs) and import adjustments (System.Diagnostics added to several files)
Observability & Semantic Conventions
The tracing layer appears to align with emerging GenAI semantic conventions:
- genai.model / genai.system: Identify provider & runtime
- Usage metrics: token counts, input/output sizes
- Latency: operation timing
- Error classification: enabling downstream analytics This positions the system for cross-provider performance and reliability comparison, while Langfuse augments prompt/result auditing.
Commit-Level Summary (Conceptual)
While individual commit messages were not visible in the retrieved data, the 26 commits likely progressed through:
- Scaffolding diagnostics infrastructure (ActivitySources, settings).
- Introducing LangfuseSettings and configuration wiring.
- Instrumenting existing providers (Azure/OpenAI).
- Adding GiteeAI plugin project + chat provider + embedding provider.
- Enhancing routing and execution spans.
- Refactoring/removing prior Langfuse-specific project (unifying under ModelDiagnostics).
- Expanding appsettings.json for multi-provider support.
- Adding README plugin documentation.
- Final polish: minor formatting and controller tracing refinements.
Key Benefits
- Unified tracing across heterogeneous model and tool operations.
- Pluggable multi-model architecture (Azure OpenAI, OpenAI, GiteeAI) with consistent diagnostics.
- Improved debuggability (span granularity at agent, function, model levels).
- Extensibility: Future plugins can hook into ModelDiagnostics without bespoke exporter code.
- Langfuse integration supports prompt/response observability and post-hoc analysis.
