Skip to content

Conversation

@RKest
Copy link

@RKest RKest commented Nov 20, 2025

Description

Introduces ability to add additional attributes to generate_content {model.name} spans to Google GenAI instrumentation.

Motivation behind this change is to add (non-semconv) gcp.vertex.agent.event_id attribute, to generate_content {model.name} spans, in order to correlate events with spans in google/adk-python.

Broader motivation is:

  • Currently in ADK we trace inference in both generate_content and legacy call_llm spans.
  • We want to migrate fully to generate_content spans and deprecate call_llm spans.
  • However call_llm spans contain gcp.vertex.agent.event_id attribute, and we still want to correlate events with spans.

Note: Event in ADK is not synonymous with event in OTEL. In ADK, Event == LLM Request/Response or Tool Call/Result, in OTEL IIUC Event == Log.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Tested using:

  • Automated unit tests.
  • Using opentelemetry-instrumentor-google-genai off my fork in ADK, and successfully adding custom attributes to target span
image

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@RKest RKest requested a review from a team as a code owner November 20, 2025 13:55
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 20, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: RKest / name: Max Ind (d8cc094)

Copy link
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, it could probably be generalized more to other GenAI instrumentations if this proves useful. We can also use this to set the gen_ai.conversation.id from ADK 🙂

# Constant used for the value of 'gen_ai.operation.name".
_GENERATE_CONTENT_OP_NAME = "generate_content"

GENERATE_CONTENT_EXTRA_ATTRIBUTES = contextvars.ContextVar[Mapping[str, AttributeValue]]("GENERATE_CONTENT_EXTRA_ATTRIBUTES", default={})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of using OTel context api instead of contextvars directly? It uses contextvars under the hood but it would then also work with code that does explicit context passing

def foo(context: Context):
  with tracer.start_as_current_span("foo", context=context):
    ...

(Admittedly it's a little clunkier/indirect)

# Constant used for the value of 'gen_ai.operation.name".
_GENERATE_CONTENT_OP_NAME = "generate_content"

GENERATE_CONTENT_EXTRA_ATTRIBUTES = contextvars.ContextVar[Mapping[str, AttributeValue]]("GENERATE_CONTENT_EXTRA_ATTRIBUTES", default={})
Copy link
Member

@aabmass aabmass Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think contextvars are usually named with snake case since the values aren't constants. Also, consider exposing it in __init__.py explicitly to be clear it's in the public API.

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.

6 participants