Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public static void clear() {
ScopedContext.CONTEXTS.clear();
}

public static LabelsSet getCurrentLabels() {
ScopedContext context = ScopedContext.getCurrentContext();
return context != null ? context.labels : new LabelsSet();
}

public static JfrLabels.LabelsSnapshot dump() {
final JfrLabels.LabelsSnapshot.Builder sb = JfrLabels.LabelsSnapshot.newBuilder();
final StringTableBuilder stb = new StringTableBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
public final class ScopedContext implements AutoCloseable {
public static final AtomicBoolean ENABLED = new AtomicBoolean(false);
static final AtomicLong CONTEXT_COUNTER = new AtomicLong(0);
static ThreadLocal<Long> CURRENT_CONTEXT_ID = ThreadLocal.withInitial(() -> 0L);
static final ConcurrentHashMap<Long, ScopedContext> CONTEXTS = new ConcurrentHashMap<>();
static final ConcurrentHashMap<Long, LabelsSet> CONSTANT_CONTEXTS = new ConcurrentHashMap<>();

Expand All @@ -70,6 +71,10 @@ static AsyncProfiler getAsyncProfiler() {
final long prevContextId;
final AtomicBoolean closed = new AtomicBoolean(false);

public static ScopedContext getCurrentContext() {
return CONTEXTS.get(CURRENT_CONTEXT_ID.get());
}

/**
* Creates a new ScopedContext with the given labels.
* The previous context ID is set to 0 (root context).
Expand Down Expand Up @@ -113,6 +118,7 @@ public ScopedContext(@NotNull LabelsSet labels, @NotNull ScopedContext prev) {
this.prevContextId = prevContextId;
CONTEXTS.put(contextId, this);
getAsyncProfiler().setContextId(contextId);
CURRENT_CONTEXT_ID.set(contextId);
} else {
this.contextId = 0;
this.prevContextId = 0;
Expand All @@ -133,6 +139,7 @@ public void close() {
}
if (ENABLED.get()) {
getAsyncProfiler().setContextId(this.prevContextId);
CURRENT_CONTEXT_ID.set(this.prevContextId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ void testNestedEqualLabelSets() {
}
}

@Test
void testCaptureContext() {
try (ScopedContext s = new ScopedContext(new LabelsSet("k1", "v1"))) {
assertEquals(s.contextId, ScopedContext.CURRENT_CONTEXT_ID.get());
}
assertEquals(0, ScopedContext.CURRENT_CONTEXT_ID.get());
}


@Test
void exception() {
Expand Down