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 TypeMethodDescription<RES> voidsetResponse(RES response) If using an Observation for instrumentation and the context is aResponseContext, set the response object on it.voidsetThrowable(Throwable throwable) If using an Observation, it will set the error on Observation.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 ifObservationRegistryis not null and not the no-op registry.voidStop the timing.
-
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 ifObservationRegistryis not null and not the no-op registry. Otherwise, timing will be instrumented with aTimerusing theMeterRegistry.- Type Parameters:
T- context type if Observation used for instrumentation- Parameters:
meterRegistry- registry for Timer-based instrumentationobservationRegistry- registry for Observation-based instrumentationcontext- supplier for the context to use if instrumenting with Observationconvention- convention that overrides the default convention and any conventions configured on the registry, if not nulldefaultConvention- 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 aResponseContext, set the response object on it. Otherwise, do nothing.- Type Parameters:
RES- type of the response- Parameters:
response- response for the RequestReplySenderContext
-
setThrowable
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 TimertimerDescription- description for the timertagsSupplier- tags supplier to apply if using the Timer API
-