Description
Micrometer 2.0.0 changes
Micrometer 2.0.0 comes with the following changes
- Moving part of Micrometer Core to Micrometer API
- Introduction of the Observation API
- Tracing (see Add support for Micrometer tracingย #30156)
Observation API
The most notable change is adding the ObservationRegistry
interface. MeterRegistry
implements it so there wouldn't be any change there. The thing that does change is that MeterRegistry
now has a observationConfig
configuration that allows to plug in ObservationHandlers
.
Required by Boot AutoConfiguration
To create an Observation
one has to call factory methods on Observation
(start
or createNotStarted
). The MeterRegistry#ObservationConfig
can have a list of ObservationPredicate
injected to define whether for given Observation name and metadata an Observation should be created or a NoOp observation should be returned. Example use case is AutoTimer
in Spring Boot. Now AutoTimer
is an inbuilt feature of the Observation API.
ObservationHandler
An ObservationHandler
allows plugging in into the lifecycle of an Observation. That means that we can react to events such as observation was started, stopped, there was an error etc.
Micrometer comes with an interface MeterObservationHandler
whose default implementation is TimerObservationHandler
. That in turn will create a Timer
when an Observation was started and will stop it when the Observation was finished. MeterRegistry
comes with a method withTimerObservationHandler
that will by default create this handler. Theoretically there might be case that someone adds Micrometer but doesn't want to create a Timer
when an Observation is created.
Micrometer Tracing comes with an interface TracingObservationHandler
that contains additional logic for tracing related handlers.
Micrometer comes with 2 additional interfaces that group ObservationHandler
s. First is ObservationHandler.FirstMatchingCompositeObservationHandler
. It will group ObservationHandler
s and pick the first one whose supportsContext
method returns true
.
Second is ObservationHandler.AllMatchingCompositeObservationHandler
. It will group ObservationHandler
s and pick all whose supportsContext
method returns true
.
After having grouped the handlers they have to be injected to the ObservationRegistry
via the ObservationRegistry#observationConfig()#observationHandler
method.
Required by Boot AutoConfiguration
The following rules of handler groupings should take place:
- All
MeterObservationHandler
implementations should be automatically grouped in aObservationHandler.FirstMatchingCompositeObservationHandler
. - All
TracingObservationHandler
implementations should be automatically grouped inObservationHandler.FirstMatchingCompositeObservationHandler
. - All
ObservationHandler
implementations should be grouped inObservationHandler.AllMatchingCompositeObservationHandler
(without duplicates)
Example:
- We have a
MeterObservationHandler
bean calledMeter
coming from the framework - We have a
TracingObservationHandler
bean calledTracing
coming from the framework - We have a
ObservationHandler
bean calledHandler
coming from the framework - We have a
FirstMatchingCompositeObservationHandler
bean calledFirst
coming from the framework - We have a
AllMatchingCompositeObservationHandler
bean calledAll
coming from the framework - We have a
FirstMatchingCompositeObservationHandler
bean calledFirst from the user
coming from the user - We have a
AllMatchingCompositeObservationHandler
bean calledAll from the user
coming from the user - We have a
MeterObservationHandler
bean calledMeter from the user
coming from the user - We have a
TracingObservationHandler
bean calledTracing from the user
coming from the user - We have a
ObservationHandler
bean calledHandler from the user
coming from the user
We should have the following setup of beans by Boot
FirstMatchingCompositeObservationHandler
created by Boot that groups all meter related handlersMeterObservationHandler
calledMeter
MeterObservationHandler
caledMeter from the user
(order can vary depending onOrdered
)
FirstMatchingCompositeObservationHandler
created by Boot that groups all tracing related handlersTracingObservationHandler
calledTracing
TracingObservationHandler
caledTracing from the user
(order can vary depending onOrdered
)
ObservationHandler
bean calledHandler
(order can vary depending onOrdered
)ObservationHandler
bean calledHandler from the user
(order can vary depending onOrdered
)FirstMatchingCompositeObservationHandler
bean calledFirst
(order can vary depending onOrdered
)FirstMatchingCompositeObservationHandler
bean calledFirst from the user
(order can vary depending onOrdered
)AllMatchingCompositeObservationHandler
bean calledAll
(order can vary depending onOrdered
)AllMatchingCompositeObservationHandler
bean calledAll from the user
(order can vary depending onOrdered
)
ObservationRegistry.observationConfig().observationHandler()
will be called to register the beans presented above.
Following conditionals have to be applicable
- When this feature is disabled (no Micrometer on the classpath / a property is explicitly disabled) nothing is created
FirstMatchingCompositeObservationHandler
created by Boot that groups all meter related handlerssupportsContext
should returnfalse
when a property to disable metrics is turned on (Micrometer on the classpath / a property for metrics enabling is explicitly disabled) - we can toggle this on and off at runtime
FirstMatchingCompositeObservationHandler
created by Boot that groups all tracing related handlers- should not be created when Micrometer Tracing is not on the classpath
supportsContext
should returnfalse
when a property to disable tracing is turned on (Micrometer & Micrometer Tracing on the classpath / a property for tracing enabling is explicitly disabled) - we can toggle this on and off at runtime
GlobalTagsProvider
A user can provide a GlobalTagsProvider
that can be applied to all observations. We would need Boot to autowire a list of GlobalTagsProvider
and put them on the ObservationRegistry
via the ObservationRegistry.tagsProvider(...)
method.