Interface Observation
- All Superinterfaces:
ObservationView
- All Known Implementing Classes:
NullObservation
Observation so that actions
can take place within the lifecycle of that observation via the
ObservationHandler.
According to what is configured the actions can be e.g. taking measurements via
Timer, creating spans for distributed tracing, correlating logs or just logging
out additional information. You instrument your code once with an Observation
but you can get as many benefits out of it as many ObservationHandler you have.- Since:
- 1.10.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceObservation.CheckedCallable<T,E extends Throwable> static interfaceObservation.CheckedRunnable<E extends Throwable>static classA mutable holder of data required by anObservationHandler.static interfaceRead only view on theObservation.Context.static interfaceAn arbitrary event that you can extend and signal during anObservation.static interfaceScope represent an action within which certain resources (e.g. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptioncontextualName(String contextualName) Sets the name that can be defined from the contents of the context.static ObservationcreateNotStarted(ObservationConvention<Observation.Context> observationConvention, ObservationRegistry registry) Creates but does not start anObservation.static <T extends Observation.Context>
ObservationcreateNotStarted(ObservationConvention<T> customConvention, ObservationConvention<T> defaultConvention, Supplier<T> contextSupplier, ObservationRegistry registry) Creates but does not start anObservation.static <T extends Observation.Context>
ObservationcreateNotStarted(ObservationConvention<T> observationConvention, Supplier<T> contextSupplier, ObservationRegistry registry) Creates but does not start anObservation.static ObservationcreateNotStarted(String name, ObservationRegistry registry) Creates but does not start anObservation.static <T extends Observation.Context>
ObservationcreateNotStarted(String name, Supplier<T> contextSupplier, ObservationRegistry registry) Creates but does not start anObservation.Signals an error.event(Observation.Event event) Signals an arbitraryObservation.Event.Returns the context attached to this observation.default Observation.ContextViewReturns the context attached to this observation as a read only view.highCardinalityKeyValue(io.micrometer.common.KeyValue keyValue) Adds a high cardinality key value.default ObservationhighCardinalityKeyValue(String key, String value) Adds a high cardinality key value.default ObservationhighCardinalityKeyValues(io.micrometer.common.KeyValues keyValues) Adds multiple high cardinality key value instances.default booleanisNoop()Checks whether thisObservationis no-op.lowCardinalityKeyValue(io.micrometer.common.KeyValue keyValue) Adds a low cardinality key value.default ObservationlowCardinalityKeyValue(String key, String value) Adds a low cardinality key value.default ObservationlowCardinalityKeyValues(io.micrometer.common.KeyValues keyValues) Adds multiple low cardinality key value instances.observationConvention(ObservationConvention<?> observationConvention) Sets an observation convention that can be used to attach key values to the observation.default voidObserves the passedRunnable, this means the followings: Starts theObservationOpens aScopeCallsRunnable.run()Closes theScopeSignals the error to theObservationif any Stops theObservationdefault <T> TObserves the passedSupplier, this means the followings: Starts theObservationOpens aScopeCallsSupplier.get()Closes theScopeSignals the error to theObservationif any Stops theObservationdefault <T,E extends Throwable>
TobserveChecked(Observation.CheckedCallable<T, E> checkedCallable) Observes the passedObservation.CheckedCallable, this means the followings: Starts theObservationOpens aScopeCallsObservation.CheckedCallable.call()Closes theScopeSignals the error to theObservationif any Stops theObservationdefault <E extends Throwable>
voidobserveChecked(Observation.CheckedRunnable<E> checkedRunnable) Observes the passedObservation.CheckedRunnable, this means the followings: Starts theObservationOpens aScopeCallsObservation.CheckedRunnable.run()Closes theScopeSignals the error to theObservationif any Stops theObservationWhen put in scope, additional operations can take place by theObservationHandlers such as putting entries in thread local.parentObservation(Observation parentObservation) If you have access to a previously createdObservationyou can manually set the parentObservationusing this method - that way you won't need to open scopes just to create a child observation.default voidWraps the given action in a scope and signals an error.default <T> TWraps the given action in a scope and signals an error.default <T,E extends Throwable>
TscopedChecked(Observation.CheckedCallable<T, E> checkedCallable) Wraps the given action in a scope and signals an error.default <E extends Throwable>
voidscopedChecked(Observation.CheckedRunnable<E> checkedRunnable) Wraps the given action in a scope and signals an error.start()Starts the observation.static Observationstart(ObservationConvention<Observation.Context> observationConvention, ObservationRegistry registry) Creates and starts anObservation.static <T extends Observation.Context>
Observationstart(ObservationConvention<T> customConvention, ObservationConvention<T> defaultConvention, Supplier<T> contextSupplier, ObservationRegistry registry) Creates and starts anObservation.static <T extends Observation.Context>
Observationstart(ObservationConvention<T> observationConvention, Supplier<T> contextSupplier, ObservationRegistry registry) Creates and starts anObservation.static Observationstart(String name, ObservationRegistry registry) Create and start anObservationwith the given name.static <T extends Observation.Context>
Observationstart(String name, Supplier<T> contextSupplier, ObservationRegistry registry) Creates and starts anObservation.voidstop()Stop the observation.static voidtryScoped(Observation parent, Runnable action) Tries to run the action against an Observation.static <T> TtryScoped(Observation parent, Supplier<T> action) Tries to run the action against an Observation.static <T,E extends Throwable>
TtryScopedChecked(Observation parent, Observation.CheckedCallable<T, E> checkedCallable) Tries to run the action against an Observation.static <E extends Throwable>
voidtryScopedChecked(Observation parent, Observation.CheckedRunnable<E> checkedRunnable) Tries to run the action against an Observation.default Runnabledefault <T> Supplier<T>default <T,E extends Throwable>
Observation.CheckedCallable<T,E> wrapChecked(Observation.CheckedCallable<T, E> checkedCallable) default <E extends Throwable>
Observation.CheckedRunnable<E>wrapChecked(Observation.CheckedRunnable<E> checkedRunnable) Methods inherited from interface io.micrometer.observation.ObservationView
getEnclosingScope, getObservationRegistry
-
Field Details
-
NOOP
No-op observation.
-
-
Method Details
-
start
Create and start anObservationwith the given name. All Observations of the same type must share the same name.When no registry is passed or the observation is
not applicable, a no-op observation will be returned.- Parameters:
name- name of the observationregistry- observation registry- Returns:
- a started observation
-
start
static <T extends Observation.Context> Observation start(String name, Supplier<T> contextSupplier, @Nullable ObservationRegistry registry) Creates and starts anObservation. When theObservationRegistryis null or the no-op registry, this fast returns a no-opObservationand skips the creation of theObservation.Context. This check avoids unnecessaryObservation.Contextcreation, which is why it takes aSupplierfor the context rather than the context directly. If the observation is not enabled (seeObservationConfig#observationPredicate), a no-op observation will also be returned.- Parameters:
name- name of the observationcontextSupplier- mutable context supplierregistry- observation registry- Returns:
- started observation
-
createNotStarted
Creates but does not start anObservation. Remember to callstart()when you want the measurements to start. When no registry is passed or observation is not applicable will return a no-op observation.- Parameters:
name- name of the observationregistry- observation registry- Returns:
- created but not started observation
-
createNotStarted
static <T extends Observation.Context> Observation createNotStarted(String name, Supplier<T> contextSupplier, @Nullable ObservationRegistry registry) Creates but does not start anObservation. Remember to callstart()when you want the measurements to start. When theObservationRegistryis null or the no-op registry, this fast returns a no-opObservationand skips the creation of theObservation.Context. This check avoids unnecessaryObservation.Contextcreation, which is why it takes aSupplierfor the context rather than the context directly. If the observation is not enabled (seeObservationConfig#observationPredicate), a no-op observation will also be returned.- Parameters:
name- name of the observationcontextSupplier- supplier for mutable contextregistry- observation registry- Returns:
- created but not started observation
-
createNotStarted
static <T extends Observation.Context> Observation createNotStarted(@Nullable ObservationConvention<T> customConvention, ObservationConvention<T> defaultConvention, Supplier<T> contextSupplier, @Nullable ObservationRegistry registry) Creates but does not start anObservation.Remember to call
start()when you want the measurements to start. When theObservationRegistryis null or the no-op registry, this returns a no-opObservationand skips the creation of theObservation.Context. If the observation is not enabled (seeObservationConfig#observationPredicate), a no-op observation will also be returned.A single
conventionwill be used for this observation for getting its name andkey values:- the
customConventiongiven as an argument, if notnull - a
GlobalObservationConventionconfigured on theObservationRegistry.ObservationConfig.observationConvention(GlobalObservationConvention)that matches this observation - as a fallback, the
defaultConventionwill be used if none of the above are available
- Type Parameters:
T- type of context- Parameters:
customConvention- custom convention. Ifnull, the default one will be picked.defaultConvention- default convention when no custom convention was passed, nor a pre-configured one was foundcontextSupplier- supplier for the observation contextregistry- observation registry- Returns:
- created but not started observation
- the
-
start
static Observation start(ObservationConvention<Observation.Context> observationConvention, ObservationRegistry registry) Creates and starts anObservation. When no registry is passed or observation is not applicable will return a no-op observation.Please check the javadoc of
createNotStarted(ObservationConvention, ObservationConvention, Supplier, ObservationRegistry)method for the logic of choosing the convention.- Parameters:
observationConvention- observation conventionregistry- observation registry- Returns:
- started observation
-
start
static <T extends Observation.Context> Observation start(ObservationConvention<T> observationConvention, Supplier<T> contextSupplier, ObservationRegistry registry) Creates and starts anObservation. When theObservationRegistryis null or the no-op registry, this fast returns a no-opObservationand skips the creation of theObservation.Context. This check avoids unnecessaryObservation.Contextcreation, which is why it takes aSupplierfor the context rather than the context directly. If the observation is not enabled (seeObservationConfig#observationPredicate), a no-op observation will also be returned.Please check the javadoc of
createNotStarted(ObservationConvention, ObservationConvention, Supplier, ObservationRegistry)method for the logic of choosing the convention.- Type Parameters:
T- type of context- Parameters:
observationConvention- observation conventioncontextSupplier- mutable context supplierregistry- observation registry- Returns:
- started observation
-
start
static <T extends Observation.Context> Observation start(@Nullable ObservationConvention<T> customConvention, ObservationConvention<T> defaultConvention, Supplier<T> contextSupplier, ObservationRegistry registry) Creates and starts anObservation. When theObservationRegistryis null or the no-op registry, this fast returns a no-opObservationand skips the creation of theObservation.Context. This check avoids unnecessaryObservation.Contextcreation, which is why it takes aSupplierfor the context rather than the context directly. If the observation is not enabled (seeObservationConfig#observationPredicate), a no-op observation will also be returned. Allows to set a customObservationConventionand requires to provide a default one if neither a custom nor a pre-configured one (viaObservationRegistry.ObservationConfig.getObservationConvention(Context, ObservationConvention)) was found.Please check the javadoc of
createNotStarted(ObservationConvention, ObservationConvention, Supplier, ObservationRegistry)method for the logic of choosing the convention.- Type Parameters:
T- type of context- Parameters:
customConvention- custom convention. Ifnull, the default one will be picked.defaultConvention- default convention when no custom convention was passed, nor a configured one was foundcontextSupplier- the observation context supplierregistry- observation registry- Returns:
- started observation
-
createNotStarted
static Observation createNotStarted(ObservationConvention<Observation.Context> observationConvention, ObservationRegistry registry) Creates but does not start anObservation. Remember to callstart()when you want the measurements to start. When no registry is passed or observation is not applicable will return a no-op observation.Please check the javadoc of
createNotStarted(ObservationConvention, ObservationConvention, Supplier, ObservationRegistry)method for the logic of choosing the convention.- Parameters:
observationConvention- observation conventionregistry- observation registry- Returns:
- created but not started observation
-
createNotStarted
static <T extends Observation.Context> Observation createNotStarted(ObservationConvention<T> observationConvention, Supplier<T> contextSupplier, ObservationRegistry registry) Creates but does not start anObservation. Remember to callstart()when you want the measurements to start. When theObservationRegistryis null or the no-op registry, this fast returns a no-opObservationand skips the creation of theObservation.Context. This check avoids unnecessaryObservation.Contextcreation, which is why it takes aSupplierfor the context rather than the context directly. If the observation is not enabled (seeObservationConfig#observationPredicate), a no-op observation will also be returned.Important! If you're using the
ObservationConvention.getContextualName(Context)method to override the contextual name you MUST use a nonnullcontext (i.e. thecontextparameter of this method MUST NOT benull). TheObservationConvention.getContextualName(Context)requires a concrete type ofObservation.Contextto be passed and if you're not providing one we won't be able to initialize it ourselves.Please check the javadoc of
createNotStarted(ObservationConvention, ObservationConvention, Supplier, ObservationRegistry)method for the logic of choosing the convention.- Type Parameters:
T- type of context- Parameters:
observationConvention- observation conventioncontextSupplier- mutable context supplierregistry- observation registry- Returns:
- created but not started observation
-
contextualName
Sets the name that can be defined from the contents of the context. E.g. a span name should not be the default observation name but one coming from an HTTP request.- Parameters:
contextualName- contextual name- Returns:
- this
-
parentObservation
If you have access to a previously createdObservationyou can manually set the parentObservationusing this method - that way you won't need to open scopes just to create a child observation. If you're using theopenScope()method then the parent observation will be automatically set, and you don't have to call this method.- Parameters:
parentObservation- parent observation to set- Returns:
- this
-
lowCardinalityKeyValue
Adds a low cardinality key value. Low cardinality means that this key value will have a bounded number of possible values. A templated HTTP URL is a good example of such a key value (e.g. /foo/{userId}).- Parameters:
keyValue- key value- Returns:
- this
-
lowCardinalityKeyValue
Adds a low cardinality key value. Low cardinality means that this key value will have a bounded number of possible values. A templated HTTP URL is a good example of such a key value (e.g. /foo/{userId}).- Parameters:
key- keyvalue- value- Returns:
- this
-
lowCardinalityKeyValues
Adds multiple low cardinality key value instances. Low cardinality means that the key value will have a bounded number of possible values. A templated HTTP URL is a good example of such a key value (e.g. /foo/{userId}).- Parameters:
keyValues- key value instances- Returns:
- this
-
highCardinalityKeyValue
Adds a high cardinality key value. High cardinality means that this key value will have an unbounded number of possible values. An HTTP URL is a good example of such a key value (e.g. /foo/bar, /foo/baz etc.).- Parameters:
keyValue- key value- Returns:
- this
-
highCardinalityKeyValue
Adds a high cardinality key value. High cardinality means that this key value will have an unbounded number of possible values. An HTTP URL is a good example of such a key value (e.g. /foo/bar, /foo/baz etc.).- Parameters:
key- keyvalue- value- Returns:
- this
-
highCardinalityKeyValues
Adds multiple high cardinality key value instances. High cardinality means that the key value will have an unbounded number of possible values. An HTTP URL is a good example of such a key value (e.g. /foo/bar, /foo/baz etc.).- Parameters:
keyValues- key value instances- Returns:
- this
-
isNoop
default boolean isNoop()Checks whether thisObservationis no-op.- Returns:
truewhen this is a no-op observation
-
observationConvention
Sets an observation convention that can be used to attach key values to the observation. Implementations should only set the convention when it supports (see:ObservationConvention.supportsContext(Context)) the currentObservation.Context. WARNING: You must set the ObservationConvention to the Observation before it is started.- Parameters:
observationConvention- observation convention- Returns:
- this
-
error
Signals an error.- Parameters:
error- error- Returns:
- this
-
event
Signals an arbitraryObservation.Event.- Parameters:
event- event- Returns:
- this
-
start
Observation start()Starts the observation. Remember to call this method, otherwise timing calculations will not take place.- Returns:
- this
-
getContext
Observation.Context getContext()Returns the context attached to this observation.- Returns:
- corresponding context
-
getContextView
Returns the context attached to this observation as a read only view.- Specified by:
getContextViewin interfaceObservationView- Returns:
- corresponding context
-
stop
void stop()Stop the observation. Remember to call this method, otherwise timing calculations won't be finished. -
openScope
Observation.Scope openScope()When put in scope, additional operations can take place by theObservationHandlers such as putting entries in thread local.- Returns:
- new scope
-
observe
Observes the passedRunnable, this means the followings:- Starts the
Observation - Opens a
Scope - Calls
Runnable.run() - Closes the
Scope - Signals the error to the
Observationif any - Stops the
Observation
- Parameters:
runnable- theRunnableto run
- Starts the
-
wrap
-
observeChecked
default <E extends Throwable> void observeChecked(Observation.CheckedRunnable<E> checkedRunnable) throws E Observes the passedObservation.CheckedRunnable, this means the followings:- Starts the
Observation - Opens a
Scope - Calls
Observation.CheckedRunnable.run() - Closes the
Scope - Signals the error to the
Observationif any - Stops the
Observation
- Type Parameters:
E- type of exception thrown- Parameters:
checkedRunnable- theObservation.CheckedRunnableto run- Throws:
E
- Starts the
-
wrapChecked
default <E extends Throwable> Observation.CheckedRunnable<E> wrapChecked(Observation.CheckedRunnable<E> checkedRunnable) throws E - Throws:
E
-
observe
Observes the passedSupplier, this means the followings:- Starts the
Observation - Opens a
Scope - Calls
Supplier.get() - Closes the
Scope - Signals the error to the
Observationif any - Stops the
Observation
- Type Parameters:
T- the type parameter of theSupplier- Parameters:
supplier- theSupplierto call- Returns:
- the result from
Supplier.get()
- Starts the
-
wrap
-
observeChecked
default <T,E extends Throwable> T observeChecked(Observation.CheckedCallable<T, E> checkedCallable) throws EObserves the passedObservation.CheckedCallable, this means the followings:- Starts the
Observation - Opens a
Scope - Calls
Observation.CheckedCallable.call() - Closes the
Scope - Signals the error to the
Observationif any - Stops the
Observation
- Type Parameters:
T- the return type of theObservation.CheckedCallableE- type of exception checkedCallable throws- Parameters:
checkedCallable- theObservation.CheckedCallableto call- Returns:
- the result from
Observation.CheckedCallable.call() - Throws:
E
- Starts the
-
wrapChecked
default <T,E extends Throwable> Observation.CheckedCallable<T,E> wrapChecked(Observation.CheckedCallable<T, E> checkedCallable) throws E- Throws:
E
-
scoped
Wraps the given action in a scope and signals an error.- Parameters:
runnable- theRunnableto run
-
scopedChecked
default <E extends Throwable> void scopedChecked(Observation.CheckedRunnable<E> checkedRunnable) throws E Wraps the given action in a scope and signals an error.- Type Parameters:
E- type of exception thrown- Parameters:
checkedRunnable- theObservation.CheckedRunnableto run- Throws:
E
-
scoped
Wraps the given action in a scope and signals an error.- Type Parameters:
T- the return type of theSupplier- Parameters:
supplier- theSupplierto call- Returns:
- the result from
Supplier.get()
-
scopedChecked
default <T,E extends Throwable> T scopedChecked(Observation.CheckedCallable<T, E> checkedCallable) throws EWraps the given action in a scope and signals an error.- Type Parameters:
T- the return type of theObservation.CheckedCallableE- type of exception checkedCallable throws- Parameters:
checkedCallable- theObservation.CheckedCallableto call- Returns:
- the result from
Observation.CheckedCallable.call() - Throws:
E
-
tryScoped
Tries to run the action against an Observation. If the Observation is null, we just run the action, otherwise we run the action in scope.- Parameters:
parent- observation, potentiallynullaction- action to run
-
tryScopedChecked
static <E extends Throwable> void tryScopedChecked(@Nullable Observation parent, Observation.CheckedRunnable<E> checkedRunnable) throws E Tries to run the action against an Observation. If the Observation is null, we just run the action, otherwise we run the action in scope.- Type Parameters:
E- type of exception checkedRunnable throws- Parameters:
parent- observation, potentiallynullcheckedRunnable- theObservation.CheckedRunnableto run- Throws:
E
-
tryScoped
Tries to run the action against an Observation. If the Observation is null, we just run the action, otherwise we run the action in scope.- Parameters:
parent- observation, potentiallynullaction- action to run- Returns:
- result of the action
-
tryScopedChecked
static <T,E extends Throwable> T tryScopedChecked(@Nullable Observation parent, Observation.CheckedCallable<T, E> checkedCallable) throws ETries to run the action against an Observation. If the Observation is null, we just run the action, otherwise we run the action in scope.- Type Parameters:
T- the return type of theObservation.CheckedCallableE- type of exception checkedCallable throws- Parameters:
parent- observation, potentiallynullcheckedCallable- theObservation.CheckedCallableto call- Returns:
- the result from
Observation.CheckedCallable.call() - Throws:
E
-