Skip to content

[OpenTelemetry] Enable OpenTelemetry stitching with Logs for Dataflow worker - #39625

Merged
stankiewicz merged 1 commit into
apache:masterfrom
stankiewicz:tace_logs_stitch
Aug 10, 2026
Merged

stankiewicz merged 1 commit into
apache:masterfrom
stankiewicz:tace_logs_stitch

Conversation

@stankiewicz

Copy link
Copy Markdown
Contributor

This change will add trace, spanId and sampled fields supported by Logging if Spans are available in current context and if feature is enabled (disabled by default).

Outcome is trace link visible in logging and logging visible in trace:

233vp3hrr861g

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@codecov

codecov Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 55.95%. Comparing base (f2c622b) to head (bf6b257).
⚠️ Report is 45 commits behind head on master.

Files with missing lines Patch % Lines
...w/worker/logging/DataflowWorkerLoggingHandler.java 75.00% 0 Missing and 4 partials ⚠️
...rker/logging/DataflowWorkerLoggingInitializer.java 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #39625      +/-   ##
============================================
- Coverage     58.15%   55.95%   -2.20%     
+ Complexity    13085     2275   -10810     
============================================
  Files          2521     1113    -1408     
  Lines        264850   175079   -89771     
  Branches      10788     1465    -9323     
============================================
- Hits         154023    97968   -56055     
+ Misses       105051    74647   -30404     
+ Partials       5776     2464    -3312     
Flag Coverage Δ
java 75.62% <66.66%> (+11.36%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Do you also want to add this to LogRecordHandler in BeamFnLoggingClient.java?
That is used with the dataflow portable runner harness.

.setTimestamp(Instant.ofEpochMilli(record.getMillis()))
.setSeverity(severityFor(record.getLevel()));

SpanContext spanContext = Span.current().getSpanContext();

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.

maybe better to just get the current span context if logOpenTelemetryTraceSpanIdAndSampled is enabled in case there is some overhead like a thread-local

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, will do

writeIfNotEmpty(generator, "logger", record.getLoggerName());
writeIfNotEmpty(generator, "exception", formatException(record.getThrown()));
SpanContext spanContext = Span.current().getSpanContext();
if (logOpenTelemetryTraceSpanIdAndSampled.get() && spanContext.isValid()) {

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.

ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

writeIfNotEmpty(generator, "exception", formatException(record.getThrown()));
SpanContext spanContext = Span.current().getSpanContext();
if (logOpenTelemetryTraceSpanIdAndSampled.get() && spanContext.isValid()) {
generator.writeStringField("trace", spanContext.getTraceId());

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.

does isValid imply that the trace id and span id are not empty? otherwise use writeIfNotEmpty

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, it means that there is something non empty and not default (different to "00000000000000000000000000000000")

if (logOpenTelemetryTraceSpanIdAndSampled.get() && spanContext.isValid()) {
generator.writeStringField("trace", spanContext.getTraceId());
generator.writeStringField("spanId", spanContext.getSpanId());
generator.writeBooleanField("traceSampled", spanContext.isSampled());

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.

would it be better to reduce overhead by just writing this if it is true and omitting it if false?

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.

maybe this should be trace_sampled as well instead of camel-case to match custom_data?

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.

hmm the documented agent format is also inconsistent https://docs.cloud.google.com/logging/docs/structured-logging#structured_logging_special_fields

uses trace_sampled and spanId

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but json is format used between dataflow worker and agent that reads data and sends asynchronously to clod logging. Happy to change field names.

@stankiewicz stankiewicz Aug 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on skipping writing when sampled=false - sounds like good idea, will implement that as well.

@Nullable DataflowExecutionState executionState,
ImmutableMap<String, String> defaultResourceLabels) {
Struct.Builder payloadBuilder = Struct.newBuilder();
//

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.

rm

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @kennknowles for label java.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@stankiewicz
stankiewicz force-pushed the tace_logs_stitch branch 2 times, most recently from 20804a3 to 873e645 Compare August 5, 2026 13:51
@stankiewicz
stankiewicz requested a review from scwhittle August 5, 2026 13:51
if (spanContext.isValid() && spanContext.isSampled()) {
generator.writeStringField("trace", spanContext.getTraceId());
generator.writeStringField("spanId", spanContext.getSpanId());
generator.writeBooleanField("trace_sampled", spanContext.isSampled());

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.

I meant to just omit
"trace_sampled": false
since that is probably common and we could just infer it was false.

This is a little different since we're also not logging the other fields.
Is there still value in adding the trace and spanId to the log if it is not-sampled? If not, this seems better to avoid unnecessary writes/parsing. but perhaps there could be some use for these fields, if they could correlate to other logs even if it isn't sampled? That is indicated by https://docs.cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.trace_sampled

I think it would be better then to log trace/spanId if the context is valid. And just conditionally log trace_sampled if it is valid and true. Alternatively we could add another option controlling if we only log sampled trace/spanids.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, will update that today.

@stankiewicz
stankiewicz force-pushed the tace_logs_stitch branch 3 times, most recently from 1028d85 to c52573d Compare August 10, 2026 12:21
@github-actions github-actions Bot added build and removed build labels Aug 10, 2026
@stankiewicz
stankiewicz requested a review from scwhittle August 10, 2026 12:41
@github-actions github-actions Bot added build and removed build labels Aug 10, 2026

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

lgtm with comment

Tracer tracer = openTelemetry.getTracer("foo");
Span span = tracer.spanBuilder("test").startSpan();
try (Scope scope = span.makeCurrent()) {
SpanContext spanContext = Span.current().getSpanContext();

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.

if spanContext is not sampled, the below assertEquals will fail since we won't have trace_sampled entry

can you have the test handle both or otherwise make sure it is always sampled?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% sample is default but I've added it explicitly.

@github-actions github-actions Bot added build and removed build labels Aug 10, 2026
@stankiewicz
stankiewicz merged commit f0da6f3 into apache:master Aug 10, 2026
28 of 29 checks passed
@stankiewicz
stankiewicz deleted the tace_logs_stitch branch October 13, 2026 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants