agent-framework icon indicating copy to clipboard operation
agent-framework copied to clipboard

.NET: [Bug]: Microsoft.Agents.AI.OpenTelemetryConsts is internal

Open viul-sc opened this issue 5 months ago • 1 comments

Description

I would like to reuse OTel Semantic conventions defined in Microsoft.Agents.AI.OpenTelemetryConsts class. Unfortunately not possible now because the class is marked as internal. Is it possible to make it public?

Code Sample


Error Messages / Stack Traces


Package Versions

main branch

.NET Version

any

Additional Context

No response

viul-sc avatar Jun 10 '26 13:06 viul-sc

The Ask is to MakeMicrosoft.Agents.AI.OpenTelemetryConstsconsumable from outside the assembly

Problem: Microsoft.Agents.AI.OpenTelemetryConsts is marked internal, so consumers can't reference the OpenTelemetry conventions it defines. Two distinct things are locked away:

  1. DefaultSourceName ("Experimental.Microsoft.Agents.AI")the ActivitySource name used by OpenTelemetryAgent and compaction tracing.
  2. The GenAI.* attribute/span keys gen_ai.operation.name, gen_ai.agent.id, gen_ai.agent.name, invoke_agent, etc.

Why this is a concrete gap, not just a nice-to-have the usage documented in ADR 0003 (https://github.com/microsoft/agent-framework/blob/main/docs/decisions/0003-agent-opentelemetry-instrumentation.md) tells users to wire up tracing like this:

  using var tracerProvider = Sdk.CreateTracerProviderBuilder()
      .AddSource(AgentOpenTelemetryConsts.DefaultSourceName)
      .AddConsoleExporter()
      .Build();

This doesn't compile against the shipped package:

  • The class is internal, so DefaultSourceName isn't reachable.
  • The type name in the doc (AgentOpenTelemetryConsts) no longer exists it was renamed to OpenTelemetryConsts in #356.

As a result, the only way to subscribe to the framework's agent spans today is to hardcode the literal Experimental.Microsoft.Agents.AI", which is brittle and breaks silently if the source name ever changes.

Design constraint to consider This telemetry layer was intentionally built to mirror Microsoft.Extensions.AI's OpenTelemetryChatClient ADR 0003 (https://github.com/microsoft/agent-framework/blob/main/docs/decisions/0003-agent-opentelemetry-instrumentation.md) lists "Microsoft.Extensions.AI Alignment" as one of its stated design goals and MEAI keeps its equivalent type internal (internal static class OpenTelemetryConsts in src/Libraries/Microsoft.Extensions.AI/OpenTelemetryConsts.cs). So fully publicizing this class diverges from that precedent and commits the team to the full gen_ai.* set as supported API surface worth a deliberate call rather than a blanket flip.

Proposed options

  • Option A expose only what's needed to subscribe (recommended). Surface just the source name as public API either public const string OpenTelemetryConsts.DefaultSourceName, or a public ActivitySource / source-name member on OpenTelemetryAgent. Solves the actual .AddSource(...) gap with minimal surface and keeps the gen_ai.* keys internal. Fix the stale ADR example in the same change.

  • Option B make the class public. Mark OpenTelemetryConsts (and members) public, gated with [Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)] to match the project's convention for unstable API. Maximizes reuse but is the largest API commitment and breaks MEAI symmetry.

  • Option C documentation only. Note that the gen_ai.* keys come straight from the OpenTelemetry GenAI semantic conventions (https://opentelemetry.io/docs/specs/semconv/gen-ai/) and can be referenced directly; still needs a supported way to obtain the source name, so this likely pairs with A.

Which direction do you prefer? Happy to send a PR leaning toward Option A plus the ADR doc fix unless you'd rather expose the whole class.

challaravinath avatar Jun 12 '26 10:06 challaravinath

Could you please help us understand how having these constants as public would help your use-case? Thanks.

moonbox3 avatar Jun 16 '26 23:06 moonbox3

Hi @challaravinath, @moonbox3, thank you for looking into this.

Due to #3723, we cannot use OpenTelemetry provided by the MAF. We will need to use our own implementation. For that, we will need constants representing OTel semantic conventions. The official OTel open-telemetry/semantic-conventions-genai repo does not provide C# implementation of the conventions.

Defining own constants in our codebase will be a small effort. But we prefer to rely on constants supplied by OTel or Microsoft libraries if that is possible.

viul-sc avatar Jun 17 '26 19:06 viul-sc

@moonbox3 - to add a concrete data point on the "how would public constants help" question: there are two separable asks in this thread, and one of them is a plain documentation bug.

ADR 0003 tells users to wire up tracing like this:

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddSource(AgentOpenTelemetryConsts.DefaultSourceName)
    .AddConsoleExporter()
    .Build();

That snippet cannot compile against the shipped package. AgentOpenTelemetryConsts was renamed to OpenTelemetryConsts in #356, and the type is internal. So the only way to subscribe to invoke_agent spans today is to hardcode "Experimental.Microsoft.Agents.AI", which breaks silently if the source name ever changes. The repo's own tests hardcode that literal as well, in OpenTelemetryAgentTests.Ctor_NullOrWhitespaceSourceName_AutoWiredChatClientUsesDefaultSource_Async.

I have Option A from the breakdown above implemented, and would like to take it if nobody else is mid-flight:

  • public const string OpenTelemetryAgent.DefaultSourceName, forwarding to the existing internal const, so the constant lives on the type that emits the spans.
  • OpenTelemetryConsts and the gen_ai.* keys stay internal, preserving the Microsoft.Extensions.AI symmetry that ADR 0003 lists as a design goal.
  • The ADR 0003 snippet corrected to the constant that actually exists.

Release build is clean across all five target frameworks with Package Validation passing (the change is purely additive, no CP diagnostics), and OpenTelemetryAgentTests is green on net10.0 and net48.

Two things I would like direction on before this is more than a draft:

  1. @viul-sc, your requirement was the gen_ai.* attribute keys rather than the source name, because #3723 pushed you onto your own OpenTelemetry implementation. #3723 is now closed - does that change the picture for you? If you still need the attribute keys, that is Option B, a larger API commitment that seems like a deliberate maintainer decision rather than something to fold into this change.
  2. If you would prefer the constant live somewhere other than OpenTelemetryAgent, that is a one-line move.

Happy to open a draft PR so there is something concrete to look at, or to hold if you would rather settle the direction first.

YashvantHange avatar Aug 22 '26 07:08 YashvantHange

Hi @YashvantHange , thank you for the followup!

I will rely on the implementation provided by #3723, hence I do not need the workaround and the constants anymore. On a separate thought - the constants represent widely accepted semantic conventions, so keeping those public seems reasonable.

veton avatar Aug 22 '26 12:08 veton