Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where the TRACEPARENT environment variable was not updating when entering new stages or nodes in Jenkins pipelines. The fix changes the implementation to use the current active span from the thread instead of always using the root span of the build.
Changes:
- Modified
OtelEnvironmentContributorto useSpan.current()to get the active span on the thread, with a fallback to the run's root span if no valid span is found - Migrated
ConfigurationKeyTestfrom JUnit 4 to JUnit 5 assertions
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java | Updated to use Span.current() instead of otelTraceService.getSpan(run) to capture the active span context, enabling TRACEPARENT to change between stages/nodes |
| src/test/java/io/jenkins/plugins/opentelemetry/semconv/ConfigurationKeyTest.java | Migrated from JUnit 4 (org.junit.Assert) to JUnit 5 (org.junit.jupiter.api.Assertions) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java
Outdated
Show resolved
Hide resolved
src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java
Outdated
Show resolved
Hide resolved
src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java
Show resolved
Hide resolved
src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java
Outdated
Show resolved
Hide resolved
src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java
Show resolved
Hide resolved
src/test/java/io/jenkins/plugins/opentelemetry/semconv/ConfigurationKeyTest.java
Outdated
Show resolved
Hide resolved
src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java
Show resolved
Hide resolved
6823eba to
03618fd
Compare
03618fd to
b72cbd8
Compare
|
sorry for this, don't know how but i ran |
|
The change needs a unit test to verify that the traces are correct and that the order is as expected. |
|
@kuisathaverat I attempted to add a regression test using JenkinsRule and tried both Controller execution and a DummySlave agent, but the test fails to propagate the OpenTelemetry Context correctly in the test harness. |
The JenkinsRule works as a regular Jenkins (mostly). We have tests that verify traces in memory in other places, and the tests should be more or less similar. Without a unit test, we cannot validate the change. |
okay, working on it again !! |
so i have added a unit test file in it! |
| mockedSpan.when(Span::current).thenReturn(currentSpan); | ||
| contributor.buildEnvironmentFor(run, envVars, listener); | ||
| // Verification | ||
| verify(environmentContributorService).addEnvironmentVariables(run, envVars, currentSpan); |
There was a problem hiding this comment.
The test does not verify the current change in the behavior.
| mockedSpan.when(Span::current).thenReturn(invalidSpan); | ||
| contributor.buildEnvironmentFor(run, envVars, listener); | ||
| // Verification | ||
| verify(environmentContributorService).addEnvironmentVariables(run, envVars, rootSpan); |
There was a problem hiding this comment.
The test does not verify the current change in the behavior.
|
I've updated the test to be more explicit about the behavior change. |
| verify(environmentContributorService).addEnvironmentVariables(run, envVars, currentSpan); | ||
|
|
||
| // PROOF OF CHANGE | ||
| verify(otelTraceService, never()).getSpan(any()); |
There was a problem hiding this comment.
It verifies that the method otelTraceService.getSpan(...) is never called when the current span is valid. But do not verify the spans of the step is the parant span and not the root span
| verify(environmentContributorService).addEnvironmentVariables(run, envVars, rootSpan); | ||
|
|
||
| // Verify that it was actually called the fallback service | ||
| verify(otelTraceService).getSpan(run); |
|
I've updated the test to strictly verify the span identity. |
|
@kuisathaverat, Gentle ping |
|
The requested changes on testing are not addressed. The test provided do not verify the context pass between layers in a pipeline. |
|
Thanks for the feedback @kuisathaverat |
|
Manual test does not remove the need of a proper test. |
ef25e0f to
7c1d73d
Compare
|
@kuisathaverat Thanks for the guidance. I have updated the PR with a new dedicated integration test that addresses your concerns.
|
2130fc2 to
02f6e9d
Compare
52a9bab to
cc87aea
Compare
src/test/java/io/jenkins/plugins/opentelemetry/job/TraceParentIntegrationTest.java
Outdated
Show resolved
Hide resolved
|
@kuisathaverat Thanks for your review, this new test file created by me adds a regression test that validates TRACEPARENT propagation and span creation across nested pipeline stages, if there are any changes that to be done pls specifically guide me, also it passes |
src/test/java/io/jenkins/plugins/opentelemetry/job/TraceParentPipelineTest.java
Outdated
Show resolved
Hide resolved
|
@kuisathaverat i have added an extra step |
src/test/java/io/jenkins/plugins/opentelemetry/job/TraceParentPipelineTest.java
Show resolved
Hide resolved
…PipelineTest.java
|
Applied the suggestion and pushed the update! |
|
I need to verify an issue is not new (not related to this PR), before merging it and releasing it |
|
I am testing the incremental version on main and the incremental of this PR and the change cause #1234 |
|
I think the propagation now links Jenkins internal operations to the pipeline span. |
|
Hi @kuisathaverat and @Zenith1415, I reviewed this PR and the The concern about One possible approach to investigate: could we check This might prevent Jenkins internal spans from leaking into |

Context
Fix for #1202.
Currently, the
TRACEPARENTenvironment variable injected into the Groovyenvobject is stuck on the Root Span of the build. It does not update when entering new stages or nodes, breaking distributed tracing context for scripts that rely onenv.TRACEPARENT.Changes
OtelEnvironmentContributor.javato useSpan.current()instead ofotelTraceService.getSpan(run).Testing done
Reproduced the issue locally with a pipeline script. verified that
env.TRACEPARENTnow changes between stages.Before Fix:

Stage 1:
00-abc...Stage 2:
00-abc...After Fix:

Stage 1:
00-abc...Stage 2:
00-xyz...