Add Foundry-hosted agent ID override for A365 exporter#102
Add Foundry-hosted agent ID override for A365 exporter#102
Conversation
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>
There was a problem hiding this comment.
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
_EnrichingBatchSpanProcessorto read Foundry env vars at init and overridegen_ai.agent.idviaEnrichedReadableSpaninon_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.
| 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: |
There was a problem hiding this comment.
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.
| 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: |
| @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") | ||
|
|
There was a problem hiding this comment.
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.
|
@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 |
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
Behavior
Testing
All 16 tests in \ est_enriching_span_processor.py\ pass. No regressions in the broader A365 test suite (270 passing).