DEV Community

Query Filter
Query Filter

Posted on

bridge37

public static class ProfilerAdvice {
    // 1. CHANGE TO PUBLIC: So inlined code in other classes can see it
    public static final Set<String> seenMethods = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());

    @Advice.OnMethodEnter
    static long enter(@Advice.Origin("#t.#m") String methodName) {
        // We use the full path to ensure the inlined code finds it
        if (comet.agent.ProfilerAgent.ProfilerAdvice.seenMethods.add(methodName)) {
            System.out.println(">>> HEARTBEAT: First call detected in " + methodName);
            System.out.flush();
        }
        return System.nanoTime();
    }

    @Advice.OnMethodExit(onThrowable = Throwable.class)
    static void exit(@Advice.Enter long start, @Advice.Origin("#t.#m") String methodName) {
        if (start == 0L) return; 

        long duration = System.nanoTime() - start;
        // 2. Ensure metrics is public (it already is)
        comet.agent.ProfilerAgent.Stats s = comet.agent.ProfilerAgent.metrics.get(methodName);
        if (s == null) {
            comet.agent.ProfilerAgent.metrics.putIfAbsent(methodName, new comet.agent.ProfilerAgent.Stats());
            s = comet.agent.ProfilerAgent.metrics.get(methodName);
        }
        s.record(duration);
    }
}

// 3. Ensure Stats class and its record method are PUBLIC
public static class Stats {
    public final LongAdder count = new LongAdder();
    public final LongAdder totalTime = new LongAdder();

    public void record(long nanos) {
        count.increment();
        totalTime.add(nanos);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)