Micrometer defines an Observation concept that enables both Metrics and Traces in applications. Metrics support offers a way to create timers, gauges or counters for collecting statistics about the runtime behavior of your application. Metrics can help you to track error rates, usage patterns, performance and more. Traces provide a holistic view of an entire system, crossing application boundaries; you can zoom in on particular user requests and follow their entire completion across applications.
Spring Framework instruments various parts of its own codebase to publish observations if an ObservationRegistry
is configured.
You can learn more about {docs-spring-boot}/html/actuator.html#actuator.metrics[configuring the observability infrastructure in Spring Boot].
If you are not familiar with Micrometer Observation, here’s a quick summary of the new concepts you should know about.
-
Observation
is the actual recording of something happening in your application. This is processed byObservationHandler
implementations to produce metrics or traces. -
Each observation has a corresponding
ObservationContext
implementation; this type holds all the relevant information for extracting metadata for it. In the case of an HTTP server observation, the context implementation could hold the HTTP request, the HTTP response, any Exception thrown during processing… -
Each
Observation
holdsKeyValues
metadata. In the case of a server HTTP observation, this could be the HTTP request method, the HTTP response status… This metadata is contributed byObservationConvention
implementations which should declare the type ofObservationContext
they support. -
KeyValues
are said to be "low cardinality" if there is a low, bounded number of possible values for theKeyValue
tuple (HTTP method is a good example). Low cardinality values are contributed to metrics only. High cardinality values are on the other hand unbounded (for example, HTTP request URIs) and are only contributed to Traces. -
An
ObservationDocumentation
documents all observations in a particular domain, listing the expected key names and their meaning.
Global configuration options are available at the ObservationRegistry#observationConfig()
level.
Each instrumented component will provide two extension points:
-
setting the
ObservationRegistry
; if not set, observations will not be recorded and will be no-ops -
providing a custom
ObservationConvention
to change the default observation name and extractedKeyValues
Let’s take the example of the Spring MVC "http.server.requests" metrics instrumentation with the ServerHttpObservationFilter
.
This observation is using a ServerRequestObservationConvention
with a ServerRequestObservationContext
; custom conventions can be configured on the Servlet filter.
If you would like to customize the metadata produced with the observation, you can extend the DefaultServerRequestObservationConvention
for your requirements:
code:ExtendedServerRequestObservationConvention
If you want full control, you can then implement the entire convention contract for the observation you’re interested in:
code:CustomServerRequestObservationConvention
You can also achieve similar goals using a custom ObservationFilter
- adding or removing key values for an observation.
Filters do not replace the default convention and are used as a post-processing component.
code:ServerRequestObservationFilter
You can configure ObservationFilter
instances on the ObservationRegistry
.
HTTP server exchanges observations are created with the name "http.server.requests"
for Servlet and Reactive applications.
Applications need to configure the org.springframework.web.filter.ServerHttpObservationFilter
Servlet filter in their application.
It is using the org.springframework.http.server.observation.DefaultServerRequestObservationConvention
by default, backed by the ServerRequestObservationContext
.
By default, the following KeyValues
are created:
Name |
Description |
|
Name of the exception thrown during the exchange, or |
|
Name of HTTP request method or |
|
Outcome of the HTTP server exchange. |
|
HTTP response raw status code, or |
|
URI pattern for the matching handler if available, falling back to |
Name |
Description |
|
HTTP request URI. |
Applications need to configure the org.springframework.web.filter.reactive.ServerHttpObservationFilter
reactive WebFilter
in their application.
It is using the org.springframework.http.server.reactive.observation.DefaultServerRequestObservationConvention
by default, backed by the ServerRequestObservationContext
.
By default, the following KeyValues
are created:
Name |
Description |
|
Name of the exception thrown during the exchange, or |
|
Name of HTTP request method or |
|
Outcome of the HTTP server exchange. |
|
HTTP response raw status code, or |
|
URI pattern for the matching handler if available, falling back to |
Name |
Description |
|
HTTP request URI. |
HTTP client exchanges observations are created with the name "http.client.requests"
for blocking and reactive clients.
Unlike their server counterparts, the instrumentation is implemented directly in the client so the only required step is to configure an ObservationRegistry
on the client.
Instrumentation is using the org.springframework.http.client.observation.ClientRequestObservationConvention
by default, backed by the ClientRequestObservationContext
.
Name |
Description |
|
Name of the exception thrown during the exchange, or |
|
Name of HTTP request method or |
|
Outcome of the HTTP client exchange. |
|
HTTP response raw status code, or |
|
URI template used for HTTP request, or |
Name |
Description |
|
Client name derived from the request URI host. |
|
HTTP request URI. |
Instrumentation is using the org.springframework.web.reactive.function.client.ClientRequestObservationConvention
by default, backed by the ClientRequestObservationContext
.
Name |
Description |
|
Name of the exception thrown during the exchange, or |
|
Name of HTTP request method or |
|
Outcome of the HTTP client exchange. |
|
HTTP response raw status code, or |
|
URI template used for HTTP request, or |
Name |
Description |
|
Client name derived from the request URI host. |
|
HTTP request URI. |