Skip to content

fix(telemetry): attribute internal SDK calls to their caller module#200

Merged
jplbrun merged 7 commits into
mainfrom
fix/no-duplicate-internal-metrics
Jul 9, 2026
Merged

fix(telemetry): attribute internal SDK calls to their caller module#200
jplbrun merged 7 commits into
mainfrom
fix/no-duplicate-internal-metrics

Conversation

@jplbrun

@jplbrun jplbrun commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Disclaimer: Do not include SAP-internal or customer-specific information in this PR (e.g. internal system URLs, customer names, tenant IDs, or confidential configurations). This is a public repository.

Description

When one SDK module calls another under the hood, the resulting OpenTelemetry metric was emitted with sap.cloud_sdk.source="user-facing" — indistinguishable from a real customer call. This double-counted customer activity in usage dashboards.

Canonical example: AgentGatewayClient.list_mcp_tools() internally calls destination.get_destination(...) to resolve auth tokens for MCP servers. Before this PR, that internal call was billed as if a customer had directly called get_destination themselves.

This PR fixes that by:

  1. Callee-side — adds _telemetry_source support to the two factories that get called internally today: destination (all three factories: create_client, create_fragment_client, create_certificate_client) and agentgateway.create_client. Each factory forwards it to the client constructor, which stores it as self._telemetry_source.

  2. Caller-side — passes _telemetry_source=Module.<caller> at every internal cross-module call site:

    • agentgateway/_lob.py:91Module.AGENTGATEWAY
    • agentgateway/_fragments.py:38Module.AGENTGATEWAY
    • extensibility/client.py:615Module.EXTENSIBILITY
    • extensibility/_ums_transport.py:452Module.EXTENSIBILITY
    • core/data_anonymization/_http_transport.py:366 and :379Module.DATA_ANONYMIZATION
    • core/auditlog_ng/__init__.py:119Module.AUDITLOG_NG

Related Issue

N/A

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code refactoring
  • Dependency update

How to Test

Unit tests

  1. uv run pytest tests/destination/unit/test_init.py -v -k "TelemetrySource" — 6 tests covering the three destination factories.
  2. uv run pytest tests/agentgateway/unit/test_agw_client.py -v -k "TelemetrySource" — 2 tests for the agentgateway factory.
  3. uv run pytest tests/ -m "not integration" — full unit-test suite. Expected: all tests pass.

End-to-end verification via local OpenTelemetry:

  1. Start a local OTLP receiver
  2. Drive any flow that transitively hits destination (e.g. AgentGatewayClient.list_mcp_tools).
  3. Expected result: the sap.cloud_sdk.source filter shows two values — user-facing (direct customer calls) and agentgateway (internal calls). Before the fix, only user-facing appeared.

Checklist

  • I have read the Contributing Guidelines
  • I have verified that my changes solve the issue
  • I have added/updated automated tests to cover my changes
  • All tests pass locally
  • I have verified that my code follows the Code Guidelines
  • I have updated documentation (if applicable)
  • I have added type hints for all public APIs
  • My code does not contain sensitive information (credentials, tokens, etc.)
  • I have followed Conventional Commits for commit messages

Breaking Changes

None. _telemetry_source is a keyword-only argument with a default of None, so all existing callers of create_client() continue to work unchanged.

Additional Notes

Adding the kwarg to production call sites broke a few pre-existing strict-signature checks. Fixed by loosening them to check only what each test cares about:

  • assert_called_once_with(instance=...)assert_called_once() + call_args.kwargs["instance"] == ...
  • lambda: fake_clientlambda **_: fake_client

Both changes decouple the tests from unrelated production kwargs.

@jplbrun jplbrun changed the title Fix/no duplicate internal metrics fix(telemetry): attribute internal SDK calls to their caller module Jul 6, 2026
@jplbrun jplbrun marked this pull request as ready for review July 6, 2026 12:48
@jplbrun jplbrun requested a review from a team as a code owner July 6, 2026 12:49
@jplbrun jplbrun marked this pull request as draft July 6, 2026 18:53
@jplbrun jplbrun force-pushed the fix/no-duplicate-internal-metrics branch from f893daa to aacbd09 Compare July 6, 2026 18:59
@jplbrun jplbrun marked this pull request as ready for review July 6, 2026 19:31
Comment thread src/sap_cloud_sdk/agentgateway/_lob.py Outdated

@LucasAlvesSoares LucasAlvesSoares 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.

img

@jplbrun jplbrun merged commit 05be4e4 into main Jul 9, 2026
11 checks passed
@jplbrun jplbrun deleted the fix/no-duplicate-internal-metrics branch July 9, 2026 14:31
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