PServiceCallInstrumentation.java

  1. package net.morimekta.providence;

  2. import javax.annotation.Nonnull;
  3. import javax.annotation.Nullable;
  4. import java.util.concurrent.TimeUnit;

  5. /**
  6.  * Interface handling the instrumentation of service calls.
  7.  */
  8. @FunctionalInterface
  9. public interface PServiceCallInstrumentation {
  10.     /**
  11.      * After each service call this method is called with the duration, call and
  12.      * response objects. Exceptions from the call is ignored, and will not affect
  13.      * the response in any way.
  14.      * <p>
  15.      * Note that the timing may not include the whole stack time since receiving
  16.      * the first packet, that is dependent on the specific implementation and the
  17.      * limitations there. E.g. it does not include the time receiving the first
  18.      * HTTP packet with the headers, or waiting for free worker threads when using
  19.      * <code>ProvidenceServlet</code>.
  20.      *
  21.      * @param duration The duration of handling the service call in milliseconds,
  22.      *                 including receiving and sending it.
  23.      * @param call     The call triggered.
  24.      * @param reply    The reply returned.
  25.      */
  26.     void onComplete(double duration,
  27.                     @Nullable PServiceCall<?> call,
  28.                     @Nullable PServiceCall<?> reply);

  29.     /**
  30.      * Called when the service call failed in the transport layer itself with something
  31.      * not related to the actual service call. The attempted call will be provided if
  32.      * known. When this method is called, then some part of the service call did not
  33.      * complete either inward (e.g. invalid call definition), or outward (e.g. network
  34.      * errors).
  35.      *
  36.      * @param e        The exception thrown.
  37.      * @param duration The duration of handling the service call in milliseconds,
  38.      *                 including receiving and sending it.
  39.      * @param call     The service call.
  40.      * @param reply    The service reply.
  41.      */
  42.     default void onTransportException(@Nonnull Exception e,
  43.                                       double duration,
  44.                                       @Nullable PServiceCall<?> call,
  45.                                       @Nullable PServiceCall<?> reply) {
  46.         // Ignored
  47.     }

  48.     /**
  49.      * A simple NO-OP instrumentation instance.
  50.      */
  51.     PServiceCallInstrumentation NOOP = (duration, call, reply) -> {};

  52.     /**
  53.      * Handy constant for calculating MS duration.
  54.      */
  55.     long NS_IN_MILLIS = TimeUnit.MILLISECONDS.toNanos(1);
  56. }