Skip to content

feat(log-enrichment): add custom session ID attribute for enriched spans#181

Merged
krisztianfekete merged 5 commits into
agentevals-dev:mainfrom
AaronProbha18:feat/174-stop-stash-session-id
Jul 27, 2026
Merged

feat(log-enrichment): add custom session ID attribute for enriched spans#181
krisztianfekete merged 5 commits into
agentevals-dev:mainfrom
AaronProbha18:feat/174-stop-stash-session-id

Conversation

@AaronProbha18

Copy link
Copy Markdown
Contributor

Summary

This PR stops storing session IDs in the OpenTelemetry semantic convention attribute gen_ai.agent.name and instead records them in a repository-specific custom attribute, agentevals.session.id.

Previously, the log enrichment pipeline wrote the session ID into gen_ai.agent.name, overwriting legitimate agent names from SDK and OTLP telemetry. This caused downstream metadata extraction to incorrectly report the session ID as the agent name.

Changes

  • Added the new custom telemetry attribute agentevals.session.id.
  • Replaced all session ID writes in the log enrichment pipeline to use agentevals.session.id.
  • Factored the duplicated session ID injection logic into a shared helper used by both enrichment paths.
  • Preserved gen_ai.agent.name for genuine agent names supplied by telemetry.
  • Updated log enrichment tests to verify that session IDs are written to the new attribute.

Comment thread src/agentevals/trace_attrs.py Outdated
}

# agentevals custom attributes (repository-specific, outside OTel semconv)
AGENTEVALS_SESSION_ID = "agentevals.session.id"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

While it's not great, let's follow the existing conventions in this codebase and go with agentevals.session_id. We can clean it up to follow the OTel pattern in a follow-up PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree, sounds like a great plan
I have renamed all references of agentevals.session.id to agentevals.session_id

return input_messages, output_messages


def _append_session_id(attrs: list[dict], session_id: str | None) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One q: what reads agentevals.session.id downstream? I only see it written, never read. There might be benefits for OTLP export consumers, just want to confirm it's intentional and not dead now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is write-only inside runtime code. I found no downstream internal reader in the repo beyond being appended during enrichment. So this looks intentional as exported metadata for external OTLP consumers rather than an in-repo dependency today.

Comment thread tests/test_log_enrichment.py Outdated

agent_name = _get_injected_attr(result[0], "gen_ai.agent.name", parse_json=False)
assert agent_name == "my-session"
session_attr = _get_injected_attr(result[0], "agentevals.session.id", parse_json=False)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we add a test for the actual bug this fixes? i.e. span comes in with a real gen_ai.agent.name, enrich with a session_id, assert the real name survives AND session id lands in the new attr.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added test_agent_name_preserved_with_session_id

attrs=[{"key": "gen_ai.agent.name", "value": {"stringValue": "roll_die_agent"}}],
)
logs = [_make_log("gen_ai.user.message", {"content": "hi"}, span_id="s1")]
result = enrich_spans_with_logs([span], logs, session_id="sess-123")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure this catches the regression. Let's do it via flatten_otlp_attributes(result[0]["attributes"])["gen_ai.agent.name"] (or assert there's exactly one gen_ai.agent.name attr). The agentevals.session_id assertion is fine!

@krisztianfekete krisztianfekete left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@krisztianfekete
krisztianfekete merged commit 3ff2936 into agentevals-dev:main Jul 27, 2026
5 checks passed
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.

2 participants