Skip to content

Fix - use flags api for trace flags#478

Merged
cleverchuk merged 1 commit into
mainfrom
cc/NH-123751
May 1, 2026
Merged

Fix - use flags api for trace flags#478
cleverchuk merged 1 commit into
mainfrom
cc/NH-123751

Conversation

@cleverchuk
Copy link
Copy Markdown
Contributor

Summary

Replace the hardcoded "01" trace-flags value in the traceparent SQL comment injection with the OpenTelemetry SpanContext.getTraceFlags().asHex() API to correctly propagate all valid flag values.

Details

The TraceContextInjector builds a W3C traceparent header and injects it as a SQL comment for query tagging. Previously, the trace-flags field was hardcoded to "01". This is incorrect because trace flags today can be 00, 01, 02, or 03. The isSampled() guard ensures only sampled spans reach this code, but a sampled span can have flags 01 or 03 — the old hardcoded value would silently drop the second bit.

Using getTraceFlags().asHex() ensures the injected traceparent accurately reflects the span's actual flags.

The test is updated to stub getTraceFlags() on the mocked SpanContext, returning TraceFlags.getSampled().

Test services data

  1. e-1712644058766987264
  2. e-1712643928659124224
  3. e-1742334541200846848
  4. e-1777406072376840192

Copilot AI review requested due to automatic review settings April 30, 2026 19:38
@cleverchuk cleverchuk requested review from a team as code owners April 30, 2026 19:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates SQL comment query tagging to generate the W3C traceparent trace-flags field from the active OpenTelemetry span context rather than hardcoding "01", ensuring all valid flag bit combinations are propagated accurately.

Changes:

  • Updated TraceContextInjector to use SpanContext.getTraceFlags().asHex() when building the injected traceparent.
  • Updated the unit test to stub SpanContext.getTraceFlags() for the mocked span context.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
instrumentation/instrumentation-shared/src/main/java/com/solarwinds/opentelemetry/instrumentation/TraceContextInjector.java Uses OpenTelemetry’s TraceFlags API when constructing the traceparent string for SQL comment injection.
instrumentation/instrumentation-shared/src/test/java/com/solarwinds/opentelemetry/instrumentation/TraceContextInjectorTest.java Updates mocking to provide trace flags so the new implementation path is exercised.
Comments suppressed due to low confidence (1)

instrumentation/instrumentation-shared/src/test/java/com/solarwinds/opentelemetry/instrumentation/TraceContextInjectorTest.java:63

  • The expected value still hardcodes the trace-flags as 01. Since the production code now uses spanContext.getTraceFlags().asHex(), consider building the expected string using TraceFlags.getSampled().asHex() so the test stays aligned with the implementation and avoids duplicating the literal.
      String actual = TraceContextInjector.inject(Context.current(), sql);
      String expected = String.format("/*traceparent='00-%s-%s-01'*/ %s", traceId, spanId, sql);
      assertEquals(expected, actual);

Copy link
Copy Markdown

@jerrytfleung jerrytfleung left a comment

Choose a reason for hiding this comment

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

LGTM

@cleverchuk cleverchuk merged commit 2cfeabb into main May 1, 2026
20 checks passed
@cleverchuk cleverchuk deleted the cc/NH-123751 branch May 1, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants