-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Add thread context propagation for span-profile correlation in thread pools #45
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
dordor12
wants to merge
2
commits into
grafana:main
Choose a base branch
from
dordor12:feat/thread-context-propagation
base: main
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
99 changes: 99 additions & 0 deletions
99
src/main/java/io/otel/pyroscope/ProfilingContextStorage.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,99 @@ | ||
| package io.otel.pyroscope; | ||
|
|
||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.ContextStorage; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.sdk.trace.ReadableSpan; | ||
|
|
||
| import io.pyroscope.PyroscopeAsyncProfiler; | ||
| import io.pyroscope.labels.v2.Pyroscope; | ||
| import io.pyroscope.vendor.one.profiler.AsyncProfiler; | ||
|
|
||
| /** | ||
| * EXPERIMENTAL: This API is experimental and may change or be removed in future versions. | ||
| * | ||
| * Complements PyroscopeOtelSpanProcessor for wall-clock profiling. SpanProcessor handles | ||
| * span start/end on the originating thread, while this ContextStorage wrapper handles | ||
| * context propagation to other threads (e.g., via executors), keeping native TLS in sync. | ||
| */ | ||
| public final class ProfilingContextStorage implements ContextStorage { | ||
| private final ContextStorage delegate; | ||
| private final AsyncProfiler asprof; | ||
|
|
||
| public ProfilingContextStorage(ContextStorage delegate) { | ||
| this.delegate = delegate; | ||
| this.asprof = PyroscopeAsyncProfiler.getAsyncProfiler(); | ||
| } | ||
|
|
||
| @Override | ||
| public Scope attach(Context toAttach) { | ||
| long spanId = 0L; | ||
| long spanNameId = 0L; | ||
|
|
||
| if (toAttach != null) { | ||
| Span span = Span.fromContext(toAttach); | ||
| SpanContext spanContext = span.getSpanContext(); | ||
|
|
||
| if (spanContext.isValid()) { | ||
| spanId = PyroscopeOtelSpanProcessor.parseSpanId(spanContext.getSpanId()); | ||
|
|
||
| if (span instanceof ReadableSpan) { | ||
| String spanName = ((ReadableSpan) span).getName(); | ||
| spanNameId = Pyroscope.LabelsWrapper.registerConstant(spanName); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (asprof != null) { | ||
| asprof.setTracingContext(spanId, spanNameId); | ||
| } | ||
|
|
||
| Scope originalScope = delegate.attach(toAttach); | ||
| return new ProfilingScope(originalScope, delegate, asprof); | ||
| } | ||
|
|
||
| @Override | ||
| public Context current() { | ||
| return delegate.current(); | ||
| } | ||
|
|
||
| private static final class ProfilingScope implements Scope { | ||
| private final Scope delegate; | ||
| private final ContextStorage storage; | ||
| private final AsyncProfiler asprof; | ||
|
|
||
| ProfilingScope(Scope delegate, ContextStorage storage, AsyncProfiler asprof) { | ||
| this.delegate = delegate; | ||
| this.storage = storage; | ||
| this.asprof = asprof; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| delegate.close(); | ||
|
|
||
| Context currentContext = storage.current(); | ||
| long spanId = 0L; | ||
| long spanNameId = 0L; | ||
|
|
||
| if (currentContext != null) { | ||
| Span span = Span.fromContext(currentContext); | ||
| SpanContext spanContext = span.getSpanContext(); | ||
| if (spanContext.isValid()) { | ||
| spanId = PyroscopeOtelSpanProcessor.parseSpanId(spanContext.getSpanId()); | ||
|
|
||
| if (span instanceof ReadableSpan) { | ||
| String spanName = ((ReadableSpan) span).getName(); | ||
| spanNameId = Pyroscope.LabelsWrapper.registerConstant(spanName); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (asprof != null) { | ||
| asprof.setTracingContext(spanId, spanNameId); | ||
| } | ||
| } | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
src/main/java/io/otel/pyroscope/PyroscopeBootstrapConfig.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,15 @@ | ||
| package io.otel.pyroscope; | ||
|
|
||
| import io.opentelemetry.javaagent.tooling.bootstrap.BootstrapPackagesBuilder; | ||
| import io.opentelemetry.javaagent.tooling.bootstrap.BootstrapPackagesConfigurer; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
|
|
||
| /** | ||
| * Configures bootstrap classloader to include Pyroscope packages. | ||
| */ | ||
| public class PyroscopeBootstrapConfig implements BootstrapPackagesConfigurer { | ||
| @Override | ||
| public void configure(BootstrapPackagesBuilder bootstrapPackagesBuilder, ConfigProperties configProperties) { | ||
| bootstrapPackagesBuilder.add("io.pyroscope"); | ||
| } | ||
| } |
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
1 change: 1 addition & 0 deletions
1
...ETA-INF/services/io.opentelemetry.javaagent.tooling.bootstrap.BootstrapPackagesConfigurer
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 @@ | ||
| io.otel.pyroscope.PyroscopeBootstrapConfig |
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.
lets mark it "experimental". Make sure this API is experimental and we have not commited to maintaining it and it may change or be removed in the future
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.
Done. Added
EXPERIMENTALwarning to javadoc.