2626import org .fireflyframework .web .error .service .ErrorResponseCache ;
2727import org .fireflyframework .web .error .service .ErrorResponseNegotiator ;
2828import org .fireflyframework .web .logging .service .PiiMaskingService ;
29- import io .micrometer .core .instrument .Counter ;
3029import io .micrometer .core .instrument .MeterRegistry ;
31- import io .micrometer .core .instrument .Timer ;
3230import io .micrometer .tracing .Tracer ;
31+ import org .fireflyframework .observability .metrics .FireflyMetricsSupport ;
3332import io .swagger .v3 .oas .annotations .Hidden ;
3433import lombok .extern .slf4j .Slf4j ;
3534import org .springframework .beans .factory .annotation .Value ;
7877@ Order (-2 )
7978@ Configuration
8079@ RestControllerAdvice
81- public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
80+ public class GlobalExceptionHandler extends FireflyMetricsSupport implements ErrorWebExceptionHandler {
8281
8382 private final ExceptionConverterService converterService ;
8483 private final Optional <PiiMaskingService > piiMaskingService ;
8584 private final ErrorHandlingProperties errorProperties ;
8685 private final Optional <Tracer > tracer ;
87- private final Optional <MeterRegistry > meterRegistry ;
8886 private final Environment environment ;
8987 private final ObjectMapper objectMapper ;
9088 private final ErrorResponseNegotiator responseNegotiator ;
@@ -93,10 +91,6 @@ public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
9391 @ Value ("${spring.application.name:unknown}" )
9492 private String applicationName ;
9593
96- // Metrics
97- private final Map <String , Counter > errorCounters = new HashMap <>();
98- private final Map <String , Timer > errorTimers = new HashMap <>();
99-
10094 /**
10195 * Creates a new GlobalExceptionHandler with comprehensive dependencies.
10296 *
@@ -119,11 +113,11 @@ public GlobalExceptionHandler(ExceptionConverterService converterService,
119113 ObjectMapper objectMapper ,
120114 ErrorResponseNegotiator responseNegotiator ,
121115 Optional <ErrorResponseCache > errorResponseCache ) {
116+ super (meterRegistry .orElse (null ), "web" );
122117 this .converterService = converterService ;
123118 this .piiMaskingService = piiMaskingService ;
124119 this .errorProperties = errorProperties ;
125120 this .tracer = tracer ;
126- this .meterRegistry = meterRegistry ;
127121 this .environment = environment ;
128122 this .objectMapper = objectMapper ;
129123 this .responseNegotiator = responseNegotiator ;
@@ -817,44 +811,25 @@ private void logAtLevel(String level, String message, Throwable ex) {
817811 * Records error metric.
818812 */
819813 private void recordErrorMetric (Throwable ex ) {
820- if (!errorProperties .isEnableMetrics () || meterRegistry . isEmpty ()) {
814+ if (!errorProperties .isEnableMetrics () || ! isEnabled ()) {
821815 return ;
822816 }
823817
824818 String exceptionType = ex .getClass ().getSimpleName ();
825- String metricName = "errors.count" ;
826-
827- Counter counter = errorCounters .computeIfAbsent (exceptionType , type ->
828- Counter .builder (metricName )
829- .tag ("exception" , type )
830- .tag ("application" , applicationName )
831- .description ("Count of errors by exception type" )
832- .register (meterRegistry .get ())
833- );
834-
835- counter .increment ();
819+ counter ("errors.count" , "exception" , exceptionType , "application" , applicationName ).increment ();
836820 }
837821
838822 /**
839823 * Records error handling duration.
840824 */
841825 private void recordErrorDuration (Throwable ex , long durationMs ) {
842- if (!errorProperties .isEnableMetrics () || meterRegistry . isEmpty ()) {
826+ if (!errorProperties .isEnableMetrics () || ! isEnabled ()) {
843827 return ;
844828 }
845829
846830 String exceptionType = ex .getClass ().getSimpleName ();
847- String metricName = "errors.duration" ;
848-
849- Timer timer = errorTimers .computeIfAbsent (exceptionType , type ->
850- Timer .builder (metricName )
851- .tag ("exception" , type )
852- .tag ("application" , applicationName )
853- .description ("Duration of error handling" )
854- .register (meterRegistry .get ())
855- );
856-
857- timer .record (java .time .Duration .ofMillis (durationMs ));
831+ timer ("errors.duration" , "exception" , exceptionType , "application" , applicationName )
832+ .record (java .time .Duration .ofMillis (durationMs ));
858833 }
859834
860835 /**
0 commit comments