Skip to content

Commit 857c609

Browse files
authored
Merge pull request #176 from grafana/feat/add-profiler-api
Add `ProfilerApi` to improve interoperability with the open telemetry extension
2 parents d7469e9 + 2999072 commit 857c609

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.pyroscope.javaagent;
2+
3+
import io.pyroscope.javaagent.api.ProfilerScopedContext;
4+
import io.pyroscope.javaagent.api.ProfilerApi;
5+
import io.pyroscope.javaagent.config.Config;
6+
import io.pyroscope.javaagent.impl.ProfilerScopedContextWrapper;
7+
import io.pyroscope.labels.LabelsSet;
8+
import io.pyroscope.labels.ScopedContext;
9+
10+
import java.util.Map;
11+
12+
public class ProfilerSdk implements ProfilerApi {
13+
14+
@Override
15+
public void startProfiling() {
16+
PyroscopeAgent.start(Config.build());
17+
}
18+
19+
@Override
20+
public boolean isProfilingStarted() {
21+
return PyroscopeAgent.isStarted();
22+
}
23+
24+
@Override
25+
public ProfilerScopedContext createScopedContext(Map<String, String> labels) {
26+
return new ProfilerScopedContextWrapper(new ScopedContext(new LabelsSet(labels)));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.pyroscope.javaagent.api;
2+
3+
import java.util.Map;
4+
5+
public interface ProfilerApi {
6+
void startProfiling();
7+
boolean isProfilingStarted();
8+
ProfilerScopedContext createScopedContext(Map<String, String> labels);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.pyroscope.javaagent.api;
2+
3+
import java.util.function.BiConsumer;
4+
5+
public interface ProfilerScopedContext {
6+
void forEachLabel(BiConsumer<String, String> consumer);
7+
void close();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.pyroscope.javaagent.impl;
2+
3+
import io.pyroscope.javaagent.api.ProfilerScopedContext;
4+
import io.pyroscope.labels.ScopedContext;
5+
6+
import java.util.function.BiConsumer;
7+
8+
public class ProfilerScopedContextWrapper implements ProfilerScopedContext {
9+
private final ScopedContext ctx;
10+
11+
public ProfilerScopedContextWrapper(ScopedContext ctx) {
12+
this.ctx = ctx;
13+
}
14+
15+
@Override
16+
public void forEachLabel(BiConsumer<String, String> consumer) {
17+
ctx.forEachLabel(consumer);
18+
}
19+
20+
@Override
21+
public void close() {
22+
ctx.close();
23+
}
24+
}

async-profiler-context/src/main/java/io/pyroscope/labels/ScopedContext.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public ScopedContext(LabelsSet labels) {
6161
context.set(current);
6262
}
6363

64-
6564
@Override
6665
public void close() {
6766
if (closed) {
@@ -73,9 +72,9 @@ public void close() {
7372
AsyncProfiler.getInstance().setContextId(previous.id);
7473
}
7574

76-
public void forEach(BiConsumer<String, String> consumer) {
75+
public void forEachLabel(BiConsumer<String, String> labelConsumer) {
7776
for (Map.Entry<Ref<String>, Ref<String>> it : current.labels.entrySet()) {
78-
consumer.accept(it.getKey().val, it.getValue().val);
77+
labelConsumer.accept(it.getKey().val, it.getValue().val);
7978
}
8079
}
8180

0 commit comments

Comments
 (0)