10
10
import io .micrometer .core .instrument .binder .cache .CacheMeterBinder ;
11
11
import io .micrometer .core .instrument .binder .cache .HazelcastCacheMetrics ;
12
12
import io .micrometer .core .lang .Nullable ;
13
- import java .lang .reflect .Method ;
14
13
import java .util .concurrent .TimeUnit ;
15
14
import javax .annotation .Nonnull ;
16
- import org .slf4j .Logger ;
17
- import org .slf4j .LoggerFactory ;
18
15
19
16
/**
20
17
* {@link HazelcastCacheMetrics} equivalent which is compatible with Hazelcast 2 (Alfresco 5.x)
21
18
*/
22
19
public class Hazelcast2CacheMetrics extends CacheMeterBinder {
23
20
24
- private static final Logger logger = LoggerFactory .getLogger (Hazelcast2CacheMetrics .class );
21
+ static final String METER_CACHE_GETS_LATENCY = "cache.gets.latency" ;
22
+ static final String METER_CACHE_PUTS_LATENCY = "cache.puts.latency" ;
23
+ static final String METER_CACHE_REMOVALS_LATENCY = "cache.removals.latency" ;
25
24
26
25
private static final String TAG_OWNERSHIP = "ownership" ;
27
- private static final String METHOD_GET_OPERATION_STATS = "getOperationStats" ;
28
26
29
27
private final IMap <?, ?> cache ;
30
28
@@ -97,7 +95,7 @@ protected Long evictionCount() {
97
95
98
96
@ Override
99
97
protected long putCount () {
100
- return cache .getLocalMapStats ().getPutOperationCount ();
98
+ return cache .getLocalMapStats ().getOperationStats (). getNumberOfPuts ();
101
99
}
102
100
103
101
@ Override
@@ -125,8 +123,7 @@ protected void bindImplementationSpecificMetrics(@Nonnull MeterRegistry registry
125
123
.register (registry );
126
124
127
125
FunctionCounter .builder ("cache.partition.gets" , cache ,
128
- cache -> extractMetricWithReflection (cache .getLocalMapStats (), METHOD_GET_OPERATION_STATS ,
129
- "getNumberOfGets" ))
126
+ c -> c .getLocalMapStats ().getOperationStats ().getNumberOfGets ())
130
127
.tags (getTagsWithCacheName ())
131
128
.description ("The total number of get operations executed against this partition" )
132
129
.register (registry );
@@ -136,50 +133,29 @@ protected void bindImplementationSpecificMetrics(@Nonnull MeterRegistry registry
136
133
}
137
134
138
135
private void timings (MeterRegistry registry ) {
139
- FunctionTimer .builder ("cache.gets.latency" , cache ,
140
- cache -> extractMetricWithReflection (cache .getLocalMapStats (), METHOD_GET_OPERATION_STATS ,
141
- "getNumberOfGets" ),
142
- cache -> extractMetricWithReflection (cache .getLocalMapStats (), METHOD_GET_OPERATION_STATS ,
143
- "getTotalGetLatency" ),
136
+ FunctionTimer .builder (METER_CACHE_GETS_LATENCY , cache ,
137
+ c -> c .getLocalMapStats ().getOperationStats ().getNumberOfGets (),
138
+ c -> c .getLocalMapStats ().getOperationStats ().getTotalGetLatency (),
144
139
TimeUnit .NANOSECONDS )
145
140
.tags (getTagsWithCacheName ())
146
141
.description ("Cache gets" )
147
142
.register (registry );
148
143
149
- FunctionTimer .builder ("cache.puts.latency" , cache ,
150
- cache -> extractMetricWithReflection (cache .getLocalMapStats (), METHOD_GET_OPERATION_STATS ,
151
- "getNumberOfPuts" ),
152
- cache -> extractMetricWithReflection (cache .getLocalMapStats (), METHOD_GET_OPERATION_STATS ,
153
- "getTotalPutLatency" ),
144
+ FunctionTimer .builder (METER_CACHE_PUTS_LATENCY , cache ,
145
+ c -> c .getLocalMapStats ().getOperationStats ().getNumberOfPuts (),
146
+ c -> c .getLocalMapStats ().getOperationStats ().getTotalPutLatency (),
154
147
TimeUnit .NANOSECONDS )
155
148
.tags (getTagsWithCacheName ())
156
149
.description ("Cache puts" )
157
150
.register (registry );
158
151
159
- FunctionTimer .builder ("cache.removals.latency" , cache ,
160
- cache -> extractMetricWithReflection (cache .getLocalMapStats (), METHOD_GET_OPERATION_STATS ,
161
- "getNumberOfRemoves" ),
162
- cache -> extractMetricWithReflection (cache .getLocalMapStats (), METHOD_GET_OPERATION_STATS ,
163
- "getTotalRemoveLatency" ),
152
+ FunctionTimer .builder (METER_CACHE_REMOVALS_LATENCY , cache ,
153
+ c -> c .getLocalMapStats ().getOperationStats ().getNumberOfRemoves (),
154
+ c -> c .getLocalMapStats ().getOperationStats ().getTotalRemoveLatency (),
164
155
TimeUnit .NANOSECONDS )
165
156
.tags (getTagsWithCacheName ())
166
157
.description ("Cache removals" )
167
158
.register (registry );
168
159
}
169
160
170
- public static long extractMetricWithReflection (final Object object , final String ... methods ) {
171
- try {
172
- Object currentObject = object ;
173
- for (String methodToExecute : methods ) {
174
- final Method method = currentObject .getClass ().getMethod (methodToExecute );
175
- method .setAccessible (true );
176
- currentObject = method .invoke (currentObject );
177
- }
178
- return (long ) currentObject ;
179
- } catch (Throwable e ) {
180
- logger .warn ("Unable to extract metric using reflection" , e );
181
- return -1 ;
182
- }
183
- }
184
-
185
161
}
0 commit comments