Skip to content
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

Add ProfilerApi to improve interoperability with the open telemetry extension #176

Merged
merged 1 commit into from
Jan 2, 2025
Merged
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
28 changes: 28 additions & 0 deletions agent/src/main/java/io/pyroscope/javaagent/ProfilerSdk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.pyroscope.javaagent;

import io.pyroscope.javaagent.api.ProfilerScopedContext;
import io.pyroscope.javaagent.api.ProfilerApi;
import io.pyroscope.javaagent.config.Config;
import io.pyroscope.javaagent.impl.ProfilerScopedContextWrapper;
import io.pyroscope.labels.LabelsSet;
import io.pyroscope.labels.ScopedContext;

import java.util.Map;

public class ProfilerSdk implements ProfilerApi {

@Override
public void startProfiling() {
PyroscopeAgent.start(Config.build());
}

@Override
public boolean isProfilingStarted() {
return PyroscopeAgent.isStarted();
}

@Override
public ProfilerScopedContext createScopedContext(Map<String, String> labels) {
return new ProfilerScopedContextWrapper(new ScopedContext(new LabelsSet(labels)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.pyroscope.javaagent.api;

import java.util.Map;

public interface ProfilerApi {
void startProfiling();
boolean isProfilingStarted();
ProfilerScopedContext createScopedContext(Map<String, String> labels);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.pyroscope.javaagent.api;

import java.util.function.BiConsumer;

public interface ProfilerScopedContext {
void forEachLabel(BiConsumer<String, String> consumer);
void close();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.pyroscope.javaagent.impl;

import io.pyroscope.javaagent.api.ProfilerScopedContext;
import io.pyroscope.labels.ScopedContext;

import java.util.function.BiConsumer;

public class ProfilerScopedContextWrapper implements ProfilerScopedContext {
private final ScopedContext ctx;

public ProfilerScopedContextWrapper(ScopedContext ctx) {
this.ctx = ctx;
}

@Override
public void forEachLabel(BiConsumer<String, String> consumer) {
ctx.forEachLabel(consumer);
}

@Override
public void close() {
ctx.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public ScopedContext(LabelsSet labels) {
context.set(current);
}


@Override
public void close() {
if (closed) {
Expand All @@ -73,9 +72,9 @@ public void close() {
AsyncProfiler.getInstance().setContextId(previous.id);
}

public void forEach(BiConsumer<String, String> consumer) {
public void forEachLabel(BiConsumer<String, String> labelConsumer) {
for (Map.Entry<Ref<String>, Ref<String>> it : current.labels.entrySet()) {
consumer.accept(it.getKey().val, it.getValue().val);
labelConsumer.accept(it.getKey().val, it.getValue().val);
}
}

Expand Down
Loading