1
1
/*
2
- * Copyright (c) 2021, 2023 Oracle and/or its affiliates.
2
+ * Copyright (c) 2021, 2025 Oracle and/or its affiliates.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import java .util .Map ;
23
23
import java .util .concurrent .atomic .AtomicInteger ;
24
24
25
+ import io .helidon .metrics .api .BuiltInMeterNameFormat ;
25
26
import io .helidon .metrics .api .Counter ;
26
27
import io .helidon .metrics .api .Gauge ;
27
28
import io .helidon .metrics .api .KeyPerformanceIndicatorMetricsConfig ;
@@ -55,24 +56,31 @@ class KeyPerformanceIndicatorMetricsImpls {
55
56
56
57
private static final Map <String , KeyPerformanceIndicatorSupport .Metrics > KPI_METRICS = new HashMap <>();
57
58
59
+ // Maps camelCase names to snake_case, but only for those names that are actually different in the two cases.
60
+ private static final Map <String , String > CAMEL_TO_SNAKE_CASE_METER_NAMES = Map .of ("inFlight" , "in_flight" ,
61
+ "longRunning" , "long_running" );
62
+
63
+
58
64
private KeyPerformanceIndicatorMetricsImpls () {
59
65
}
60
66
61
67
/**
62
68
* Provides a KPI metrics instance.
63
69
*
64
- * @param kpiMeterRegistry meter registry holding the KPI metrics
65
- * @param meterNamePrefix prefix to use for the created metrics (e.g., "requests")
66
- * @param kpiConfig KPI metrics config which may influence the construction of the metrics
70
+ * @param kpiMeterRegistry meter registry holding the KPI metrics
71
+ * @param meterNamePrefix prefix to use for the created metrics (e.g., "requests")
72
+ * @param kpiConfig KPI metrics config which may influence the construction of the metrics
73
+ * @param builtInMeterNameFormat format to use for meter names
67
74
* @return properly prepared new KPI metrics instance
68
75
*/
69
76
static KeyPerformanceIndicatorSupport .Metrics get (MeterRegistry kpiMeterRegistry ,
70
77
String meterNamePrefix ,
71
- KeyPerformanceIndicatorMetricsConfig kpiConfig ) {
78
+ KeyPerformanceIndicatorMetricsConfig kpiConfig ,
79
+ BuiltInMeterNameFormat builtInMeterNameFormat ) {
72
80
return KPI_METRICS .computeIfAbsent (meterNamePrefix , prefix ->
73
81
kpiConfig .extended ()
74
- ? new Extended (kpiMeterRegistry , meterNamePrefix , kpiConfig )
75
- : new Basic (kpiMeterRegistry , meterNamePrefix ));
82
+ ? new Extended (kpiMeterRegistry , meterNamePrefix , kpiConfig , builtInMeterNameFormat )
83
+ : new Basic (kpiMeterRegistry , meterNamePrefix , builtInMeterNameFormat ));
76
84
}
77
85
78
86
static void close () {
@@ -87,11 +95,13 @@ private static class Basic implements KeyPerformanceIndicatorSupport.Metrics {
87
95
private final Counter totalCount ;
88
96
private final MeterRegistry meterRegistry ;
89
97
private final List <Meter > meters = new ArrayList <>();
98
+ private final BuiltInMeterNameFormat builtInMeterNameFormat ;
90
99
91
- protected Basic (MeterRegistry kpiMeterRegistry , String meterNamePrefix ) {
100
+ protected Basic (MeterRegistry kpiMeterRegistry , String meterNamePrefix , BuiltInMeterNameFormat builtInMeterNameFormat ) {
92
101
meterRegistry = kpiMeterRegistry ;
102
+ this .builtInMeterNameFormat = builtInMeterNameFormat ;
93
103
totalCount = add (kpiMeterRegistry .getOrCreate (
94
- Counter .builder (meterNamePrefix + REQUESTS_COUNT_NAME )
104
+ Counter .builder (meterNamePrefix + meterName ( REQUESTS_COUNT_NAME ) )
95
105
.description (
96
106
"Each request (regardless of HTTP method) will increase this counter" )
97
107
.scope (KPI_METERS_SCOPE )));
@@ -116,6 +126,12 @@ protected <M extends Meter> M add(M meter) {
116
126
protected Counter totalCount () {
117
127
return totalCount ;
118
128
}
129
+
130
+ protected String meterName (String camelCaseMeterName ){
131
+ return builtInMeterNameFormat == BuiltInMeterNameFormat .CAMEL
132
+ ? camelCaseMeterName
133
+ : CAMEL_TO_SNAKE_CASE_METER_NAMES .getOrDefault (camelCaseMeterName , camelCaseMeterName );
134
+ }
119
135
}
120
136
121
137
/**
@@ -136,15 +152,20 @@ private static class Extended extends Basic {
136
152
137
153
protected Extended (MeterRegistry kpiMeterRegistry ,
138
154
String meterNamePrefix ,
139
- KeyPerformanceIndicatorMetricsConfig kpiConfig ) {
140
- this (kpiMeterRegistry , meterNamePrefix , kpiConfig .longRunningRequestThreshold ());
155
+ KeyPerformanceIndicatorMetricsConfig kpiConfig ,
156
+ BuiltInMeterNameFormat builtInMeterNameFormat ) {
157
+ this (kpiMeterRegistry , meterNamePrefix , kpiConfig .longRunningRequestThreshold (), builtInMeterNameFormat );
141
158
}
142
159
143
- private Extended (MeterRegistry kpiMeterRegistry , String meterNamePrefix , Duration longRunningRequestThreshold ) {
144
- super (kpiMeterRegistry , meterNamePrefix );
160
+ private Extended (MeterRegistry kpiMeterRegistry ,
161
+ String meterNamePrefix ,
162
+ Duration longRunningRequestThreshold ,
163
+ BuiltInMeterNameFormat builtInMeterNameFormat ) {
164
+ super (kpiMeterRegistry , meterNamePrefix , builtInMeterNameFormat );
145
165
this .longRunningRequestThresdholdMs = longRunningRequestThreshold .toMillis ();
146
166
147
- inflightRequests = kpiMeterRegistry .getOrCreate (Gauge .builder (meterNamePrefix + INFLIGHT_REQUESTS_NAME ,
167
+ inflightRequests = kpiMeterRegistry .getOrCreate (Gauge .builder (meterNamePrefix
168
+ + meterName (INFLIGHT_REQUESTS_NAME ),
148
169
inflightRequestsCount ,
149
170
AtomicInteger ::get )
150
171
.scope (KPI_METERS_SCOPE ));
@@ -155,12 +176,12 @@ private Extended(MeterRegistry kpiMeterRegistry, String meterNamePrefix, Duratio
155
176
.scope (KPI_METERS_SCOPE )
156
177
);
157
178
158
- load = kpiMeterRegistry .getOrCreate (Counter .builder (meterNamePrefix + LOAD_NAME )
179
+ load = kpiMeterRegistry .getOrCreate (Counter .builder (meterNamePrefix + meterName ( LOAD_NAME ) )
159
180
.description (LOAD_DESCRIPTION )
160
181
.scope (KPI_METERS_SCOPE ));
161
182
162
183
deferredRequests = new DeferredRequests ();
163
- kpiMeterRegistry .getOrCreate (Gauge .builder (meterNamePrefix + DEFERRED_NAME ,
184
+ kpiMeterRegistry .getOrCreate (Gauge .builder (meterNamePrefix + meterName ( DEFERRED_NAME ) ,
164
185
deferredRequests ,
165
186
DeferredRequests ::value )
166
187
.description ("Measures deferred requests" )
0 commit comments