Skip to content

Add auto-configuration for Micrometer 2.0 Observation APIย #29666

Closed
@marcingrzejszczak

Description

@marcingrzejszczak

Micrometer 2.0.0 changes

Micrometer 2.0.0 comes with the following changes

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 MeterObservationHandler implementations should be automatically grouped in a ObservationHandler.FirstMatchingCompositeObservationHandler.
  • All TracingObservationHandler implementations should be automatically grouped in ObservationHandler.FirstMatchingCompositeObservationHandler.
  • All ObservationHandler implementations should be grouped in ObservationHandler.AllMatchingCompositeObservationHandler (without duplicates)

Example:

  • We have a MeterObservationHandler bean called Meter coming from the framework
  • We have a TracingObservationHandler bean called Tracing coming from the framework
  • We have a ObservationHandler bean called Handler coming from the framework
  • We have a FirstMatchingCompositeObservationHandler bean called First coming from the framework
  • We have a AllMatchingCompositeObservationHandler bean called All coming from the framework
  • We have a FirstMatchingCompositeObservationHandler bean called First from the user coming from the user
  • We have a AllMatchingCompositeObservationHandler bean called All from the user coming from the user
  • We have a MeterObservationHandler bean called Meter from the user coming from the user
  • We have a TracingObservationHandler bean called Tracing from the user coming from the user
  • We have a ObservationHandler bean called Handler 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 handlers
    • MeterObservationHandler called Meter
    • MeterObservationHandler caled Meter from the user (order can vary depending on Ordered)
  • FirstMatchingCompositeObservationHandler created by Boot that groups all tracing related handlers
    • TracingObservationHandler called Tracing
    • TracingObservationHandler caled Tracing from the user (order can vary depending on Ordered)
  • ObservationHandler bean called Handler (order can vary depending on Ordered)
  • ObservationHandler bean called Handler from the user (order can vary depending on Ordered)
  • FirstMatchingCompositeObservationHandler bean called First (order can vary depending on Ordered)
  • FirstMatchingCompositeObservationHandler bean called First from the user (order can vary depending on Ordered)
  • AllMatchingCompositeObservationHandler bean called All (order can vary depending on Ordered)
  • AllMatchingCompositeObservationHandler bean called All from the user (order can vary depending on Ordered)

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 handlers
    • supportsContext should return false 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 return false 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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions