Skip to content

[Python] Open Telemetry tracing (part 2)#42190

Open
Zgoda91 wants to merge 40 commits into
grpc:masterfrom
Zgoda91:A72_text_map_propagator_integration
Open

[Python] Open Telemetry tracing (part 2)#42190
Zgoda91 wants to merge 40 commits into
grpc:masterfrom
Zgoda91:A72_text_map_propagator_integration

Conversation

@Zgoda91

@Zgoda91 Zgoda91 commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Implementing gRFC A72 grpc/proposal/pull/474

@Zgoda91 Zgoda91 added the release notes: yes Indicates if PR needs to be in release notes label Apr 20, 2026
@Zgoda91 Zgoda91 force-pushed the A72_text_map_propagator_integration branch from 2f1e5f4 to aad0d5e Compare April 30, 2026 09:31
@Zgoda91 Zgoda91 force-pushed the A72_text_map_propagator_integration branch from aad0d5e to ca8cd00 Compare April 30, 2026 09:55
@sergiitk

Copy link
Copy Markdown
Member

/gemini review

@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 adds OpenTelemetry tracing support to gRPC Python for both sync and async (AIO) calls, enabling span context propagation and recording of span events. The implementation involves updates to Cython bindings, context management, and the addition of extensive tests. Feedback highlights a critical issue where the OpenTelemetry Tracer's internal ID generator is incorrectly accessed, which would prevent ID propagation, and recommends defensive programming when accessing the plugin list to avoid index errors.

# to prevent concurrent RPCs from overwriting shared state
with self._tracer_lock:
# this step is needed to propagate span ID correctly
self._tracer.id_generator = _GrpcIdGenerator(

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.

high

The opentelemetry.sdk.trace.Tracer class in the OpenTelemetry Python SDK uses _id_generator (with a leading underscore) as its internal attribute for the ID generator. Using id_generator without the underscore will create a new attribute that the SDK's start_span method will ignore, causing the custom ID propagation to fail.

Suggested change
self._tracer.id_generator = _GrpcIdGenerator(
self._tracer._id_generator = _GrpcIdGenerator(

Comment on lines +588 to +590
current_span = trace.get_current_span(
context=self._plugins[0].get_trace_context()
).get_span_context()

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

Accessing self._plugins[0] directly can lead to an IndexError if the _plugins list is empty. While observability might be initialized, it's safer to handle cases where no plugins are registered or configured for tracing.

        context = self._plugins[0].get_trace_context() if self._plugins else None
        current_span = trace.get_current_span(
            context=context
        ).get_span_context()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lang/Python release notes: yes Indicates if PR needs to be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants