All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.pyroscope.labels.Pyroscope Maven / Gradle / Ivy

package io.pyroscope.labels;


import io.pyroscope.labels.pb.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

public class Pyroscope {
    public static class LabelsWrapper {

        public static  T run(LabelsSet labels, Callable c) throws Exception {
            try (ScopedContext s = new ScopedContext(labels)) {
                return c.call();
            }
        }

        public static void run(LabelsSet labels, Runnable c) {
            try (ScopedContext s = new ScopedContext(labels)) {
                c.run();
            }
        }

        public static JfrLabels.Snapshot dump() {
            JfrLabels.Snapshot.Builder sb = JfrLabels.Snapshot.newBuilder();

            for (Ref it : RefCounted.strings.valueToRef.values()) {
                sb.putStrings(it.id, it.val);
            }
            for (Ref, Ref>> it : RefCounted.contexts.valueToRef.values()) {
                JfrLabels.Context.Builder cb = JfrLabels.Context.newBuilder();
                for (Map.Entry, Ref> kv : it.val.entrySet()) {
                    cb.putLabels(kv.getKey().id, kv.getValue().id);
                }
                sb.putContexts(it.id, cb.build());
            }
            RefCounted.contexts.gc();
            RefCounted.strings.gc();
            return sb.build();
        }
    }

    private static Map staticLabels = Collections.emptyMap();

    public static void setStaticLabels(Map labels) {
        staticLabels = Collections.unmodifiableMap(labels);
    }

    public static Map getStaticLabels() {
        return staticLabels;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy