-
Notifications
You must be signed in to change notification settings - Fork 4.6k
[OpenTelemetry] Context propagation for Runner v1 and SimpleDoFnRunner #39152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stankiewicz
wants to merge
1
commit into
apache:master
Choose a base branch
from
stankiewicz:otel_propagate_context_through_runner_v1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−16
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
.../java/org/apache/beam/runners/dataflow/worker/WindmillOpenTelemetryContextPropagator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.beam.runners.dataflow.worker; | ||
|
|
||
| import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.propagation.TextMapGetter; | ||
| import io.opentelemetry.context.propagation.TextMapSetter; | ||
| import org.apache.beam.model.fnexecution.v1.BeamFnApi; | ||
| import org.apache.beam.sdk.annotations.Internal; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| @Internal | ||
| public class WindmillOpenTelemetryContextPropagator { | ||
|
|
||
| private static final TextMapSetter<BeamFnApi.Elements.ElementMetadata.Builder> SETTER = | ||
| (carrier, key, value) -> { | ||
| if (carrier == null) { | ||
| return; | ||
| } | ||
| if ("traceparent".equals(key)) { | ||
| carrier.setTraceparent(value); | ||
| } else if ("tracestate".equals(key)) { | ||
| carrier.setTracestate(value); | ||
| } | ||
| }; | ||
|
|
||
| private static final TextMapGetter<BeamFnApi.Elements.ElementMetadata> GETTER = | ||
| new TextMapGetter<BeamFnApi.Elements.ElementMetadata>() { | ||
| @Override | ||
| public Iterable<String> keys(BeamFnApi.Elements.ElementMetadata carrier) { | ||
| return Lists.newArrayList("traceparent", "tracestate"); | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable String get( | ||
| BeamFnApi.Elements.@Nullable ElementMetadata carrier, String key) { | ||
| if (carrier == null) { | ||
| return null; | ||
| } | ||
| if ("traceparent".equals(key)) { | ||
| return carrier.getTraceparent(); | ||
| } else if ("tracestate".equals(key)) { | ||
| return carrier.getTracestate(); | ||
| } | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| public static void set(Context from, BeamFnApi.Elements.ElementMetadata.Builder builder) { | ||
| W3CTraceContextPropagator.getInstance().inject(from, builder, SETTER); | ||
| } | ||
|
|
||
| public static Context read(BeamFnApi.Elements.ElementMetadata from) { | ||
| return W3CTraceContextPropagator.getInstance().extract(Context.root(), from, GETTER); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dynamically evaluating
Context.current()inside the gettergetOpenTelemetryContext()is highly problematic and breaks context propagation across thread boundaries.WindowedValueis an immutable value representation of an element. IfgetOpenTelemetryContext()is called on a different thread (e.g., during asynchronous processing, buffering, or when writing to a sink on a background thread),Context.current()will return the context of that background thread (orContext.root()), which differs fromopenTelemetryContext. As a result, the getter will incorrectly return the background thread's context instead of the element's original context.To fix this correctness issue, the current OpenTelemetry context should be captured at construction time (e.g., in the constructor or builder of
WindowedValue), andgetOpenTelemetryContext()should simply return the captured field without any dynamic checks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point but usage is from within the thread that is having the proper context.