Description
Spring Data repository invocations are not metered yet and it would make sense to expose invocation metrics to improve observability from a repository perspective.
Ideally, repository metrics measure the number of invocations and the timing for each repository method. The metering should use a Micrometer Timer
with the following tags per method invocation:
repository
: Simple name of the repository interface classmethod
: Method name of the invoked methodinvocation
: Invocation type:SYNC
(Person
,List
, …),ASYNC
(CompletionStage
,ListenableFuture
),Stream
(Java 8 Stream),REACTIVE
(a supported Reactive type according toReactiveAdapterRegistry
)result
: Whether the method invocation yielded a result (NULL
,EMPTY
(forOptional.empty()
or an emptyCollection
),PRESENT
(forOptional.of(…)
))exception
: Simple name of the exception if an exception was thrown
Specifics to consider:
- Asynchronous calls: Repository queries may offload calls to a worker thread and return
CompletionStage
orListenableFuture
- Reactive repositories: Return a reactive type, metrics get collected on success or on error
- Stream queries: Metrics get collected when the
Stream
is fully consumed/Stream gets closed
Metrics can be collected through a MethodInterceptor
as repositories are pure proxy objects that internally dispatch method calls, so from an outside an interceptor seems appropriate. The interceptor can be attached through a RepositoryProxyPostProcessor
which needs to be configured on repository factory beans (RepositoryFactoryBeanSupport
). That change needs to be done in Spring Data Commons (see DATACMNS-1688).
I have a PoC that uses BeanPostProcessor.postProcessBeforeInitialization(…)
so we can turn that into a pull request.
Likely, this feature would require a bit of auto-configuration since metrics so we would need to find a good spot for configuration properties.
Limitations:
JPA runs some activity that happens outside of repository calls (e.g. lazy loading, defer activities until transaction cleanup). These activities would not be included in these metrics.