Skip to content

feat: add setTraceId and clearTraceId to ProfilerApi#313

Merged
marcsanmi merged 7 commits into
mainfrom
feat/trace-id-profiler-api
Jun 10, 2026
Merged

feat: add setTraceId and clearTraceId to ProfilerApi#313
marcsanmi merged 7 commits into
mainfrom
feat/trace-id-profiler-api

Conversation

@marcsanmi

@marcsanmi marcsanmi commented May 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds two methods to ProfilerApi so the OTel integration can attach the trace ID as a string label on profile samples. setTracingContext(long, long) cannot carry a 128 bit value, hence a separate entry point.

The ProfilerSdk impl backs them with a ThreadLocal<ScopedContext> that opens ScopedContext("trace_id", traceId) on set and closes it on clear, reusing the existing labels pipeline.

Both new methods are declared default (no op) on the interface so older runtimes stay wire compatible with newer callers (the label simply does not appear).

Related PR https://github.com/grafana/otel-profiling-java/pull/TBD will land once this is released.

@cla-assistant

cla-assistant Bot commented May 21, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cla-assistant

cla-assistant Bot commented May 21, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@marcsanmi marcsanmi force-pushed the feat/trace-id-profiler-api branch from e8b271d to 7827187 Compare May 21, 2026 11:38
@marcsanmi marcsanmi changed the title feat: add setTraceId/clearTraceId to ProfilerApi for trace-to-profile correlation feat: add setTraceId and clearTraceId to ProfilerApi May 21, 2026
@marcsanmi marcsanmi marked this pull request as ready for review May 21, 2026 11:39
@marcsanmi marcsanmi requested a review from a team as a code owner May 21, 2026 11:39
@marcsanmi marcsanmi requested a review from simonswine May 21, 2026 11:49

@korniltsev-grafanista korniltsev-grafanista 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.

I would prefer if we did not use the labels infrastructure (ScopedContext, LabelsSet) for this and instead passed trace id through setTracingContext and JFR (that would require async-profiler changes, we can put trace id as well as spanId and spanName into the thread local structure they recently added asprof_thread_local_data ).

The main reason is that the current implementation will break in few ways:

  • If one already associated labels to the thread with ScopedContext, the new set of methods will not be aware of it and silently override it.
  • Similarly the other way around: if we set labels with setTraceId and then someone else uses ScopedContext - it will override trace id label, unless we expose the new java ThreadLocal somehow, which may be not trivial, especially across class loaders.

Another reason is performance (premature?) optimizations. I got no proof, but gut feeling tells me storing __int128 would be cheaper then keeping every traceId strings between profile uploads.

@Override
public void setTraceId(@NotNull String traceId) {
// W3C trace ID is 32 hex chars (128 bits). Split into two longs.
long hi = Long.parseUnsignedLong(traceId.substring(0, 16), 16);

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.

String#substring allocates, can we somehow try parsing hexes without it?

@marcsanmi marcsanmi marked this pull request as ready for review June 9, 2026 14:30

Copilot AI 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.

Pull request overview

This PR extends the cross-classloader ProfilerApi with setTraceId(String) / clearTraceId() so the OTel integration can associate a 128-bit W3C trace ID with samples, and updates the embedded async-profiler fork version/distribution artifacts accordingly.

Changes:

  • Add setTraceId(String) and clearTraceId() as default methods on ProfilerApi for backward-compatible implementations.
  • Implement setTraceId/clearTraceId in ProfilerSdk by parsing a 32-hex-char trace ID into two longs and delegating to async-profiler.
  • Bump bundled Grafana async-profiler version and update .so.sha1 checksums; add unit tests for hex parsing.

Reviewed changes

Copilot reviewed 7 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Makefile Bumps the Grafana async-profiler version used by the download script.
async-profiler-grafana-fork-dist/lib/libasyncProfiler-macos.so.sha1 Updates checksum for the macOS native library.
async-profiler-grafana-fork-dist/lib/libasyncProfiler-linux-x64.so.sha1 Updates checksum for the Linux x64 native library.
async-profiler-grafana-fork-dist/lib/libasyncProfiler-linux-arm64.so.sha1 Updates checksum for the Linux arm64 native library.
agent/src/test/java/io/pyroscope/javaagent/ProfilerSdkTest.java Adds unit tests validating parseHex64 parsing behavior.
agent/src/main/java/io/pyroscope/javaagent/ProfilerSdk.java Implements trace ID support and introduces parseHex64.
agent/src/main/java/io/pyroscope/javaagent/api/ProfilerApi.java Adds the new API methods as defaults for compatibility.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread agent/src/main/java/io/pyroscope/javaagent/ProfilerSdk.java
Comment thread agent/src/main/java/io/pyroscope/javaagent/api/ProfilerApi.java Outdated
marcsanmi and others added 2 commits June 9, 2026 16:37
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@marcsanmi marcsanmi merged commit 49c863e into main Jun 10, 2026
72 checks passed
@marcsanmi marcsanmi deleted the feat/trace-id-profiler-api branch June 10, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants