Skip to content

fix(gapic-generator): default client_options.api_endpoint to transport host for OpenTelemetry spans - #18869

Draft
chalmerlowe wants to merge 1 commit into
fix/otel-http-t3-t4-attributesfrom
fix/otel-default-host-endpoint-attributes
Draft

chalmerlowe wants to merge 1 commit into
fix/otel-http-t3-t4-attributesfrom
fix/otel-default-host-endpoint-attributes

Conversation

@chalmerlowe

Copy link
Copy Markdown
Contributor

Summary

Stacked on #18868.

Populates client_options.api_endpoint with the transport's resolved self._host in Transport.__init__ (base.py.j2) when client_options is provided without an explicit api_endpoint override.

Why

  • Previously, _extract_endpoint_attributes in google-api-core only populated server.address and non-default server.port on Tier 3 (GAPIC client) and gRPC Tier 4 (wire attempt) spans when the caller explicitly passed ClientOptions(api_endpoint=...).
  • When a client or transport connected using the service's DEFAULT_HOST (or a host passed directly to the transport constructor) without setting client_options.api_endpoint, server.address and server.port were omitted from Tier 3 and gRPC Tier 4 spans.
  • Defaulting client_options.api_endpoint to self._host in the base transport constructor ensures Tier 3 and gRPC Tier 4 spans consistently record server.address and non-default server.port.

Files Modified

  • packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2
  • packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2
  • packages/gapic-generator/tests/system/telemetry_requirements_matrix.csv
  • packages/gapic-generator/tests/integration/goldens/**

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request updates the base transport templates and generated golden files to automatically populate the api_endpoint in client_options with the transport's host if it is not already set, supporting both ClientOptions instances and dictionaries. It also updates the corresponding unit tests and telemetry requirements matrix. A review comment correctly points out that mutating client_options in-place can cause unexpected side effects if the caller reuses the options object, and suggests copying the options before modification.

Comment on lines +165 to +168
if isinstance(client_options, client_options_lib.ClientOptions) and not client_options.api_endpoint:
client_options.api_endpoint = self._host
elif isinstance(client_options, dict) and not client_options.get("api_endpoint"):
client_options["api_endpoint"] = self._host

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.

medium

Mutating the client_options argument in-place (whether it is a ClientOptions instance or a dict) can lead to unexpected side effects if the caller reuses the same options object across multiple client or transport instances. To prevent this, we should create a copy of the options before modifying them.

        if isinstance(client_options, client_options_lib.ClientOptions):
            if not client_options.api_endpoint:
                import copy
                client_options = copy.copy(client_options)
                client_options.api_endpoint = self._host
        elif isinstance(client_options, dict):
            if not client_options.get("api_endpoint"):
                client_options = dict(client_options)
                client_options["api_endpoint"] = self._host

@chalmerlowe
chalmerlowe force-pushed the fix/otel-default-host-endpoint-attributes branch from 5591f09 to 3c63f33 Compare October 27, 2026 20:37
@chalmerlowe
chalmerlowe force-pushed the fix/otel-http-t3-t4-attributes branch from f636b0e to a8fbedc Compare October 27, 2026 20:37
@chalmerlowe
chalmerlowe force-pushed the fix/otel-default-host-endpoint-attributes branch from 3c63f33 to 63c5e32 Compare October 27, 2026 23:03
@chalmerlowe
chalmerlowe force-pushed the fix/otel-http-t3-t4-attributes branch from a8fbedc to fd9c5ba Compare October 27, 2026 23:03
@chalmerlowe
chalmerlowe force-pushed the fix/otel-default-host-endpoint-attributes branch from 63c5e32 to aded2a1 Compare October 28, 2026 09:27
@chalmerlowe
chalmerlowe force-pushed the fix/otel-http-t3-t4-attributes branch from fd9c5ba to 6855304 Compare October 28, 2026 09:27
@chalmerlowe
chalmerlowe force-pushed the fix/otel-http-t3-t4-attributes branch from 6855304 to 64d36b7 Compare October 28, 2026 09:39
@chalmerlowe
chalmerlowe force-pushed the fix/otel-default-host-endpoint-attributes branch from aded2a1 to 7568958 Compare October 28, 2026 09:39

This branch has not been deployed

No deployments
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.

1 participant