Class ObservationOrTimerCompatibleInstrumentation<T extends io.micrometer.observation.Observation.Context>

java.lang.Object
io.micrometer.core.instrument.observation.ObservationOrTimerCompatibleInstrumentation<T>
Type Parameters:
T - context type if Observation used for instrumentation

public class ObservationOrTimerCompatibleInstrumentation<T extends io.micrometer.observation.Observation.Context> extends Object
Abstracts instrumenting code with a Timer or an Observation. This can be useful for avoiding complexity and minimizing overhead when converting instrumentation that was previously instrumented with a Timer to being optionally instrumented with an Observation. This may be necessary where backwards compatibility is a concern such that you cannot require an ObservationRegistry. If there are no backwards compatibility concerns, generally direct instrumentation with Observation should be preferred. While this was designed for use internally in micrometer-core where we have this need, it may also be useful to other libraries with pre-existing Timer-based instrumentation. If an ObservationRegistry is provided that is not the no-op registry, an Observation will be used for instrumentation. Otherwise, a Timer will be used.
Since:
1.10.0
  • Method Summary

    Modifier and Type
    Method
    Description
    <RES> void
    setResponse(RES response)
    If using an Observation for instrumentation and the context is a ResponseContext, set the response object on it.
    void
    If using an Observation, it will set the error on Observation.
    static <T extends io.micrometer.observation.Observation.Context>
    ObservationOrTimerCompatibleInstrumentation<T>
    start(MeterRegistry meterRegistry, io.micrometer.observation.ObservationRegistry observationRegistry, Supplier<T> context, io.micrometer.observation.ObservationConvention<T> convention, io.micrometer.observation.ObservationConvention<T> defaultConvention)
    Start timing based on Observation and the convention for it if ObservationRegistry is not null and not the no-op registry.
    void
    stop(String timerName, String timerDescription, Supplier<Iterable<Tag>> tagsSupplier)
    Stop the timing.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • start

      public static <T extends io.micrometer.observation.Observation.Context> ObservationOrTimerCompatibleInstrumentation<T> start(MeterRegistry meterRegistry, @Nullable io.micrometer.observation.ObservationRegistry observationRegistry, Supplier<T> context, @Nullable io.micrometer.observation.ObservationConvention<T> convention, io.micrometer.observation.ObservationConvention<T> defaultConvention)
      Start timing based on Observation and the convention for it if ObservationRegistry is not null and not the no-op registry. Otherwise, timing will be instrumented with a Timer using the MeterRegistry.
      Type Parameters:
      T - context type if Observation used for instrumentation
      Parameters:
      meterRegistry - registry for Timer-based instrumentation
      observationRegistry - registry for Observation-based instrumentation
      context - supplier for the context to use if instrumenting with Observation
      convention - convention that overrides the default convention and any conventions configured on the registry, if not null
      defaultConvention - convention to use if one is not configured
      Returns:
      a started instrumentation
    • setResponse

      public <RES> void setResponse(RES response)
      If using an Observation for instrumentation and the context is a ResponseContext, set the response object on it. Otherwise, do nothing.
      Type Parameters:
      RES - type of the response
      Parameters:
      response - response for the RequestReplySenderContext
    • setThrowable

      public void setThrowable(Throwable throwable)
      If using an Observation, it will set the error on Observation. For metrics, it will do nothing.
      Parameters:
      throwable - error that got recorded
    • stop

      public void stop(String timerName, @Nullable String timerDescription, Supplier<Iterable<Tag>> tagsSupplier)
      Stop the timing. The tags that should be applied to the timer need to be passed here. These parameters will only be used if instrumentation is Timer-based. Observation-based instrumentation will use tags and the name from the applicable convention.
      Parameters:
      timerName - name of the timer if instrumentation is done with Timer
      timerDescription - description for the timer
      tagsSupplier - tags supplier to apply if using the Timer API