-
Notifications
You must be signed in to change notification settings - Fork 41.6k
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 ObservationHandlers. First is ObservationHandler.FirstMatchingCompositeObservationHandler . It will group ObservationHandlers and pick the first one whose supportsContext method returns true.
Second is ObservationHandler.AllMatchingCompositeObservationHandler. It will group ObservationHandlers 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
MeterObservationHandlerimplementations should be automatically grouped in aObservationHandler.FirstMatchingCompositeObservationHandler. - All
TracingObservationHandlerimplementations should be automatically grouped inObservationHandler.FirstMatchingCompositeObservationHandler. - All
ObservationHandlerimplementations should be grouped inObservationHandler.AllMatchingCompositeObservationHandler(without duplicates)
Example:
- We have a
MeterObservationHandlerbean calledMetercoming from the framework - We have a
TracingObservationHandlerbean calledTracingcoming from the framework - We have a
ObservationHandlerbean calledHandlercoming from the framework - We have a
FirstMatchingCompositeObservationHandlerbean calledFirstcoming from the framework - We have a
AllMatchingCompositeObservationHandlerbean calledAllcoming from the framework - We have a
FirstMatchingCompositeObservationHandlerbean calledFirst from the usercoming from the user - We have a
AllMatchingCompositeObservationHandlerbean calledAll from the usercoming from the user - We have a
MeterObservationHandlerbean calledMeter from the usercoming from the user - We have a
TracingObservationHandlerbean calledTracing from the usercoming from the user - We have a
ObservationHandlerbean calledHandler from the usercoming from the user
We should have the following setup of beans by Boot
FirstMatchingCompositeObservationHandlercreated by Boot that groups all meter related handlersMeterObservationHandlercalledMeterMeterObservationHandlercaledMeter from the user(order can vary depending onOrdered)
FirstMatchingCompositeObservationHandlercreated by Boot that groups all tracing related handlersTracingObservationHandlercalledTracingTracingObservationHandlercaledTracing from the user(order can vary depending onOrdered)
ObservationHandlerbean calledHandler(order can vary depending onOrdered)ObservationHandlerbean calledHandler from the user(order can vary depending onOrdered)FirstMatchingCompositeObservationHandlerbean calledFirst(order can vary depending onOrdered)FirstMatchingCompositeObservationHandlerbean calledFirst from the user(order can vary depending onOrdered)AllMatchingCompositeObservationHandlerbean calledAll(order can vary depending onOrdered)AllMatchingCompositeObservationHandlerbean 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
FirstMatchingCompositeObservationHandlercreated by Boot that groups all meter related handlerssupportsContextshould returnfalsewhen 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
FirstMatchingCompositeObservationHandlercreated by Boot that groups all tracing related handlers- should not be created when Micrometer Tracing is not on the classpath
supportsContextshould returnfalsewhen 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.