HttpSender with Resilience4j Retry

HttpSender is an interface for HTTP clients that are used in meter registries for HTTP-based metrics publication. There are two implementations:

  • HttpUrlConnectionSender: HttpURLConnection-based HttpSender

  • OkHttpSender: OkHttp-based HttpSender

Micrometer does not include support for retry, but you can decorate it with Resilience4j retry, as follows:

	public DatadogMeterRegistry datadogMeterRegistry(DatadogConfig datadogConfig, Clock clock) {
		return DatadogMeterRegistry.builder(datadogConfig)
				.httpClient(new RetryHttpClient())

	private static class RetryHttpClient extends HttpUrlConnectionSender {

		private final RetryConfig retryConfig = RetryConfig.custom()

		private final Retry retry = Retry.of("datadog-metrics", this.retryConfig);

		public Response send(Request request) {
			CheckedFunction0<Response> retryableSupplier = Retry.decorateCheckedSupplier(
					() -> super.send(request));
			return Try.of(retryableSupplier).get();