Thanks to visit codestin.com
Credit goes to github.com

Skip to content

StreamingPull shares opentelemetry context across multiple messages #2086

@gaurang101197

Description

@gaurang101197

Please make sure you have searched for information in the following guides.

A screenshot that you have tested with "Try this API".

https://github.com/user-attachments/assets/a25724ad-c4f0-4894-b2df-212d5b5e23ff

Above repository has detailed steps to reproduce the issue. Below is the screenshot of output indicating multiple messages having same traceId.

Image

Link to the code that reproduces this issue. A link to a public Github Repository or gist with a minimal reproduction.

https://github.com/gaurang101197/pubsub-shared-traceid-repro

A step-by-step description of how to reproduce the issue, based on the linked reproduction.

Please follow README of pubsub-shared-traceid-repro repository to reproduce the issue.

A clear and concise description of what the bug is, and what you expected to happen.

Description

When Pub/Sub telemetry is enabled in subscription clien, disabled in publisher client and OpenTelemetry gRPC auto-instrumentation is active, the StreamingPull client call runs under a single active context and the response stream is bound to that context. As a result, every message emitted from the same stream sees the same context.active() trace and Pub/Sub attaches a span with same context to message.parentSpan.

Root-cause hypothesis

Above case lead to all messages from a single StreamingPull stream share the same active context which make all messages sharing same context in parentSpan field. And all messages processing spans become child span of grpc.google.pubsub.v1.Subscriber/StreamingPull grpc span.

What I expected

In case of telemetry is disabled on publisher client and enabled on subscription client, each message should have unique traceId.

Probable Solution - Use root context instead of active context when parentSpan is not found in message. https://github.com/googleapis/nodejs-pubsub/blob/e546f2cbeb165f7d7e6602b39478ebd73f89b31d/src/telemetry-tracing.ts#L425C5-L439C6

if (parent) {
      return getTracer().startSpan(
        name,
        {
          kind: SpanKind.CONSUMER,
          attributes,
        },
        parent,
      );
    } else {
      return getTracer().startSpan(name, {
        kind: SpanKind.CONSUMER,
        attributes,
      }, ROOT_CONTEXT);
    }

A clear and concise description WHY you expect this behavior, i.e., was it a recent change, there is documentation that points to this behavior, etc. **

When tracing is not enabled in publisher client, all messages of streamingPull grpc client shares the same parent span. Which lead to all child span of message getting linked to single trace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: pubsubIssues related to the googleapis/nodejs-pubsub API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions