From 47a23591ff8911e1dd5fe96a48c51403ae6dcdeb Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 8 May 2025 15:20:26 -0400 Subject: [PATCH] GH-491: Fix NPE in the MetricsRetryListener when `label` is null Fixes: https://github.com/spring-projects/spring-retry/issues/491 The Micrometer tag cannot be with `null` value. When `RetryCallback` does not provide a proper `getLabel()` implementation, the `MetricsRetryListener` fails with a `NullPointerException` * Fix `MetricsRetryListener.close()` to fallback to the `callback.getClass().getName()` if `callback.getLabel() == null` * Cover behavior in the new `RetryMetricsTests.labelFallbackToClassName()` --- .../retry/support/MetricsRetryListener.java | 9 +++++--- .../retry/support/RetryMetricsTests.java | 23 ++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/retry/support/MetricsRetryListener.java b/src/main/java/org/springframework/retry/support/MetricsRetryListener.java index 92b9d0c..bf0a922 100644 --- a/src/main/java/org/springframework/retry/support/MetricsRetryListener.java +++ b/src/main/java/org/springframework/retry/support/MetricsRetryListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.retry.support; import java.util.Collections; import java.util.IdentityHashMap; import java.util.Map; +import java.util.Objects; import java.util.function.Function; import io.micrometer.core.instrument.MeterRegistry; @@ -43,7 +44,8 @@ import org.springframework.util.Assert; *

* The registered {@value #TIMER_NAME} {@link Timer} has these tags by default: *