Class CallDepth

java.lang.Object
io.opentelemetry.javaagent.bootstrap.CallDepth

public final class CallDepth extends Object
A utility to track nested calls in an instrumentation.

For example, this can be used to track nested calls to super() in constructors by calling getAndIncrement() at the beginning of each constructor.

This works the following way: when you enter some method that you want to track, you call getAndIncrement() method. If returned number is larger than 0, then you have already been in this method and are in recursive call now. When you then leave the method, you call decrementAndGet() method. If returned number is larger than 0, then you have already been in this method and are in recursive call now.

In short, the semantic of both methods is the same: they will return value 0 if and only if current method invocation is the first one for the current call stack.

  • Method Details

    • forClass

      public static CallDepth forClass(Class<?> cls)
      Return the current call depth for a given class (not method; we want to be able to track calls between different methods in a class).

      The returned instance is unique per given class and per thread.

    • getAndIncrement

      public int getAndIncrement()
      Increment the current call depth and return the previous value. This method will always return 0 if it's the first (outermost) call.
    • decrementAndGet

      public int decrementAndGet()
      Decrement the current call depth and return the current value. This method will always return 0 if it's the last (outermost) call.