Skip to content

Add Foundry-hosted agent ID override for A365 exporter#102

Open
singankit wants to merge 1 commit intomainfrom
anksing/foundry-agent-id-override
Open

Add Foundry-hosted agent ID override for A365 exporter#102
singankit wants to merge 1 commit intomainfrom
anksing/foundry-agent-id-override

Conversation

@singankit
Copy link
Copy Markdown
Member

Summary

When running in a Foundry-hosted environment (\FOUNDRY_HOSTING_ENVIRONMENT=1), override \gen_ai.agent.id\ on spans sent to the A365 exporter with the value from \FOUNDRY_AGENT_IDENTITY.

Key Design Decision

The override is placed inside _EnrichingBatchSpanProcessor.on_end()\ which exclusively feeds the A365 exporter. This ensures other exporters (Azure Monitor, OTLP, Console) are not affected.

Changes

  • *\src/microsoft/opentelemetry/a365/constants.py* — Added \FOUNDRY_HOSTING_ENVIRONMENT_ENV\ and \FOUNDRY_AGENT_IDENTITY_ENV\ constants
  • *\src/microsoft/opentelemetry/a365/core/exporters/enriching_span_processor.py* — Updated _EnrichingBatchSpanProcessor\ to read env vars at init and apply override via \EnrichedReadableSpan\
  • *\ ests/a365/test_enriching_span_processor.py* — Added 7 new tests covering all scenarios

Behavior

FOUNDRY_HOSTING_ENVIRONMENT FOUNDRY_AGENT_IDENTITY Result
\1\ set Override applied (always, even if attribute exists)
absent / other any No override
\1\ absent / empty No override

Testing

All 16 tests in \ est_enriching_span_processor.py\ pass. No regressions in the broader A365 test suite (270 passing).

When running in a Foundry-hosted environment (FOUNDRY_HOSTING_ENVIRONMENT=1),
override gen_ai.agent.id on spans sent to the A365 exporter with the value
from FOUNDRY_AGENT_IDENTITY. This override only affects the A365 export path
and does not impact other exporters (Azure Monitor, OTLP, Console).

The logic is added to _EnrichingBatchSpanProcessor which exclusively feeds
the A365 exporter. The env check happens at processor init time for performance.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 1, 2026 17:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a Foundry-hosted behavior to the A365 export pipeline: when running under a Foundry hosting environment, spans exported via the A365 exporter have their gen_ai.agent.id overridden from a Foundry-provided environment variable, without affecting other exporters.

Changes:

  • Added Foundry-related environment variable name constants.
  • Updated _EnrichingBatchSpanProcessor to read Foundry env vars at init and override gen_ai.agent.id via EnrichedReadableSpan in on_end().
  • Expanded unit tests to cover Foundry override scenarios and updated existing tests to account for the new processor field.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/microsoft/opentelemetry/a365/constants.py Introduces env var name constants for Foundry hosting detection and identity.
src/microsoft/opentelemetry/a365/core/exporters/enriching_span_processor.py Implements Foundry env detection at init and applies gen_ai.agent.id override during span processing for the A365 exporter pipeline.
tests/a365/test_enriching_span_processor.py Adds coverage for Foundry override behavior and adjusts existing tests for the new processor attribute.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +88 to +90
foundry_env = os.environ.get(FOUNDRY_HOSTING_ENVIRONMENT_ENV, "").strip()
agent_identity = os.environ.get(FOUNDRY_AGENT_IDENTITY_ENV, "").strip()
if foundry_env == "1" and agent_identity:
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

FOUNDRY_HOSTING_ENVIRONMENT_ENV is being treated as truthy only when the env var equals the literal string "1". Elsewhere in this repo, env-bool parsing normalizes with strip().lower() and accepts ("true", "1", "yes", "on"). To avoid surprising behavior (e.g., "True"/"on" not enabling the override) and to stay consistent with existing env parsing, normalize + use the same truthy set (or reuse an existing env-bool helper) before deciding whether to apply the override.

Suggested change
foundry_env = os.environ.get(FOUNDRY_HOSTING_ENVIRONMENT_ENV, "").strip()
agent_identity = os.environ.get(FOUNDRY_AGENT_IDENTITY_ENV, "").strip()
if foundry_env == "1" and agent_identity:
foundry_env = os.environ.get(FOUNDRY_HOSTING_ENVIRONMENT_ENV, "").strip().lower()
agent_identity = os.environ.get(FOUNDRY_AGENT_IDENTITY_ENV, "").strip()
truthy_env_values = ("true", "1", "yes", "on")
if foundry_env in truthy_env_values and agent_identity:

Copilot uses AI. Check for mistakes.
Comment on lines +180 to +185
@patch.dict("os.environ", {"FOUNDRY_HOSTING_ENVIRONMENT": "1", "FOUNDRY_AGENT_IDENTITY": "env-agent-456"})
def test_init_reads_env_vars_when_foundry_hosted(self):
with patch.object(_EnrichingBatchSpanProcessor.__bases__[0], "__init__", return_value=None):
processor = _EnrichingBatchSpanProcessor(MagicMock())
self.assertEqual(processor._foundry_agent_id, "env-agent-456")

Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

These tests hardcode Foundry env var names ("FOUNDRY_HOSTING_ENVIRONMENT" / "FOUNDRY_AGENT_IDENTITY") and span attribute keys ("gen_ai.agent.id"). Since constants for these values exist in the codebase now, using the constants here would prevent test drift if names change and keep tests aligned with production code.

Copilot uses AI. Check for mistakes.
@rads-1996
Copy link
Copy Markdown
Member

rads-1996 commented May 1, 2026

@singankit I have a quick question, are you enabling A365 when running your application in the foundary hosted environment?

@rads-1996
Copy link
Copy Markdown
Member

rads-1996 commented May 1, 2026

@singankit I have a quick question, are you enabling A365 when running your application in the foundary hosted environment?

@singankit Is this a known issue? Was this also an issue when you were using the A365 sdk independently. If there are any associated issues, it would quite helpful if you could link those here.

Additionally, can you also share the setup you are using for testing this scenario.

@singankit
Copy link
Copy Markdown
Member Author

@singankit I have a quick question, are you enabling A365 when running your application in the foundary hosted environment?

@singankit Is this a known issue? Was this also an issue when you were using the A365 sdk independently. If there are any associated issues, it would quite helpful if you could link those here.

Additionally, can you also share the setup you are using for testing this scenario.

@rads-1996 This is to enable using A365 exporter for Hosted Agents in Foundry. Hosted Agents already setup some of these information as env vars and we want to leverage those when customer codes run as part of Hosted Agent

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.

3 participants