Skip to content

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

Open
stankiewicz wants to merge 1 commit into
apache:masterfrom
stankiewicz:tace_logs_stitch
Open

[OpenTelemetry] Enable OpenTelemetry stitching with Logs for Dataflow worker#39625
stankiewicz wants to merge 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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.15%. Comparing base (f2c622b) to head (945ffdd).
⚠️ Report is 8 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #39625      +/-   ##
============================================
- Coverage     58.15%   58.15%   -0.01%     
+ Complexity    13085    13084       -1     
============================================
  Files          2521     2521              
  Lines        264850   264863      +13     
  Branches      10788    10788              
============================================
- Hits         154023   154020       -3     
- Misses       105051   105066      +15     
- Partials       5776     5777       +1     
Flag Coverage Δ
java 64.25% <ø> (-0.02%) ⬇️

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).

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