[MEAI] Add OpenTelemetry GenAI semantic convention selection - #7710
Closed
rogerbarreto wants to merge 1 commit into
Closed
[MEAI] Add OpenTelemetry GenAI semantic convention selection#7710rogerbarreto wants to merge 1 commit into
rogerbarreto wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds GenAI semantic convention mode selection to Microsoft.Extensions.AI OpenTelemetry instrumentation, allowing consumers to choose between a v1.36 compatibility representation and the latest experimental representation (with precedence: explicit API > OTEL_SEMCONV_STABILITY_OPT_IN > latest experimental default).
Changes:
- Introduces
OpenTelemetryGenAISemanticConvention(experimental) and plumbs it through chat, embeddings, and function-invocation telemetry. - Adds v1.36-compatible event-based message emission and mode-specific attribute/metric differences.
- Expands test coverage for environment-variable parsing, mode stability, and v1.36-specific outputs.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/Libraries/Microsoft.Extensions.AI.Tests/Embeddings/OpenTelemetryEmbeddingGeneratorTests.cs | Adds v1.36 compatibility assertions for embedding span tags and metrics. |
| test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryEnvironmentVariableTests.cs | Adds tests for OTEL_SEMCONV_STABILITY_OPT_IN parsing and precedence/stability behavior. |
| test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryChatClientTests.cs | Adds v1.36 compatibility tests validating event bodies and absence of latest-only fields. |
| test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs | Extends function-invocation tests to validate both semantic convention modes and propagation. |
| src/Shared/DiagnosticIds/DiagnosticIds.cs | Adds experiment diagnostic ID for the new semantic convention selection API. |
| src/Libraries/Microsoft.Extensions.AI/TelemetryHelpers.cs | Adds semantic-convention environment parsing and provider-attribute-name selection helpers. |
| src/Libraries/Microsoft.Extensions.AI/OpenTelemetryGenAISemanticConvention.cs | Introduces new experimental enum representing v1.36 vs latest experimental emission modes. |
| src/Libraries/Microsoft.Extensions.AI/OpenTelemetryConsts.cs | Adds constants for stability opt-in env var/token and v1.36 event names/keys. |
| src/Libraries/Microsoft.Extensions.AI/Microsoft.Extensions.AI.json | Updates public API metadata for the new enum and properties (experimental). |
| src/Libraries/Microsoft.Extensions.AI/Embeddings/OpenTelemetryEmbeddingGeneratorBuilderExtensions.cs | Documents env-var behavior and override precedence for embeddings instrumentation. |
| src/Libraries/Microsoft.Extensions.AI/Embeddings/OpenTelemetryEmbeddingGenerator.cs | Adds SemanticConvention property and applies mode-specific tag/metric differences. |
| src/Libraries/Microsoft.Extensions.AI/Common/OtelV136.cs | Adds v1.36 event body serialization models. |
| src/Libraries/Microsoft.Extensions.AI/Common/OtelMessageSerializer.cs | Adjusts “latest” serialization model references (namespaced under OtelLatest). |
| src/Libraries/Microsoft.Extensions.AI/Common/OtelLatest.cs | Adds latest-experimental serialization models previously embedded in chat client. |
| src/Libraries/Microsoft.Extensions.AI/Common/OtelContext.cs | Updates source-gen serialization context to include both latest and v1.36 models. |
| src/Libraries/Microsoft.Extensions.AI/Common/FunctionInvocationProcessor.cs | Adds semantic-convention awareness to execute_tool span tags and sensitive fields. |
| src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClientBuilderExtensions.cs | Documents env-var behavior and override precedence for chat instrumentation. |
| src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs | Adds SemanticConvention selection; emits v1.36 message/choice events vs latest tags. |
| src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs | Plumbs semantic convention from agent activity, telemetry client, or environment default. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
49
to
53
| _activitySource = activitySource; | ||
| _invokeFunction = invokeFunction; | ||
| _isSensitiveDataEnabled = isSensitiveDataEnabled ?? (_ => false); | ||
| _getSemanticConvention = getSemanticConvention ?? (_ => OpenTelemetryGenAISemanticConvention.LatestExperimental); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and context
Microsoft.Extensions.AIcurrently emits only the latest GenAI semantic conventions supported by the package. It does not honorOTEL_SEMCONV_STABILITY_OPT_IN, so applications cannot select the v1.36 compatibility representation.This change adds both the v1.36 compatibility representation and the latest experimental representation. It keeps the existing default when the environment variable is absent, avoiding a breaking telemetry change for current users. Applications can select v1.36 by defining
OTEL_SEMCONV_STABILITY_OPT_INwithoutgen_ai_latest_experimental.This support also unblocks consistent .NET observability in Microsoft Agent Framework, which uses
OpenTelemetryChatClientfor agent, chat, and tool execution telemetry.Fixes #7709
Description
Semantic convention selection
The resolved mode follows this precedence:
OTEL_SEMCONV_STABILITY_OPT_INgen_ai_latest_experimentalThe environment value is parsed as a comma-separated list. The resolved mode is fixed for the lifetime of each instrumentation instance and is exposed for wrappers that need to emit related spans consistently.
v1.36 compatibility representation
The compatibility mode restores the telemetry contract that
Microsoft.Extensions.AIemitted before #6767:gen_ai.systeminstead ofgen_ai.provider.name.Latest experimental representation
The latest experimental mode preserves the current behavior:
gen_ai.provider.name.gen_ai.input.messagesandgen_ai.output.messages.gen_ai.system_instructions.Sensitive content
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENTcontinues to control message bodies, system instructions, function arguments, and function results in both modes.The change does not introduce the Agent Framework specific
ENABLE_MESSAGE_EVENTSsetting.Scope
This pull request updates:
OpenTelemetryChatClientFunctionInvokingChatClientandFunctionInvocationProcessorOpenTelemetryEmbeddingGeneratorCompatibility
The recommended behavior preserves the telemetry emitted when no new configuration is provided.
Defining
OTEL_SEMCONV_STABILITY_OPT_INwithoutgen_ai_latest_experimentalintentionally changes that instrumentation instance to the v1.36 compatibility representation. Consumers should not expect provider fields or message representations from both modes in the same operation.The v1.36 GenAI conventions have
Developmentstatus. This pull request describes them as a compatibility representation rather than a stable representation.API
The new API is experimental pending owner review:
Testing
The implementation is covered by:
invoke_agentandinvoke_workflowmode propagationThe filtered AI solution builds and tests successfully. The final full build reports existing
IDE0055warnings in unchanged lines ofFunctionInvokingChatClient.cs.Checklist
Developmentand compatibility terminology correctly.