1
1
package io .javaoperatorsdk .operator ;
2
2
3
- import java .util .Collections ;
4
3
import java .util .Map ;
5
- import java .util .concurrent .TimeUnit ;
6
- import java .util .function .ToDoubleFunction ;
7
- import java .util .function .ToLongFunction ;
8
4
9
- import io .micrometer .core .instrument .Clock ;
10
- import io .micrometer .core .instrument .Counter ;
11
- import io .micrometer .core .instrument .DistributionSummary ;
12
- import io .micrometer .core .instrument .FunctionCounter ;
13
- import io .micrometer .core .instrument .FunctionTimer ;
14
- import io .micrometer .core .instrument .Gauge ;
15
- import io .micrometer .core .instrument .Measurement ;
16
- import io .micrometer .core .instrument .Meter ;
17
- import io .micrometer .core .instrument .MeterRegistry ;
18
- import io .micrometer .core .instrument .Timer ;
19
- import io .micrometer .core .instrument .distribution .DistributionStatisticConfig ;
20
- import io .micrometer .core .instrument .distribution .pause .PauseDetector ;
21
- import io .micrometer .core .instrument .noop .NoopCounter ;
22
- import io .micrometer .core .instrument .noop .NoopDistributionSummary ;
23
- import io .micrometer .core .instrument .noop .NoopFunctionCounter ;
24
- import io .micrometer .core .instrument .noop .NoopFunctionTimer ;
25
- import io .micrometer .core .instrument .noop .NoopGauge ;
26
- import io .micrometer .core .instrument .noop .NoopMeter ;
27
- import io .micrometer .core .instrument .noop .NoopTimer ;
5
+ public interface Metrics {
6
+ Metrics NOOP = new Metrics () {};
28
7
29
- public class Metrics {
30
- public static final Metrics NOOP = new Metrics (new NoopMeterRegistry (Clock .SYSTEM ));
31
- public static final String PREFIX = "operator.sdk." ;
32
- private final MeterRegistry registry ;
33
8
34
- public Metrics (MeterRegistry registry ) {
35
- this .registry = registry ;
36
- }
37
-
38
- public interface ControllerExecution <T > {
9
+ interface ControllerExecution <T > {
39
10
String name ();
40
11
41
12
String controllerName ();
@@ -45,111 +16,15 @@ public interface ControllerExecution<T> {
45
16
T execute ();
46
17
}
47
18
48
- public <T > T timeControllerExecution (ControllerExecution <T > execution ) {
49
- final var name = execution .controllerName ();
50
- final var execName = PREFIX + "controllers.execution." + execution .name ();
51
- final var timer =
52
- Timer .builder (execName )
53
- .tags ("controller" , name )
54
- .publishPercentiles (0.3 , 0.5 , 0.95 )
55
- .publishPercentileHistogram ()
56
- .register (registry );
57
- try {
58
- final var result = timer .record (execution ::execute );
59
- final var successType = execution .successTypeName (result );
60
- registry
61
- .counter (execName + ".success" , "controller" , name , "type" , successType )
62
- .increment ();
63
- return result ;
64
- } catch (Exception e ) {
65
- final var exception = e .getClass ().getSimpleName ();
66
- registry
67
- .counter (execName + ".failure" , "controller" , name , "exception" , exception )
68
- .increment ();
69
- throw e ;
70
- }
71
- }
72
-
73
- public void incrementControllerRetriesNumber () {
74
- registry
75
- .counter (
76
- PREFIX + "retry.on.exception" , "retry" , "retryCounter" , "type" ,
77
- "retryException" )
78
- .increment ();
79
-
19
+ default <T > T timeControllerExecution (ControllerExecution <T > execution ) {
20
+ return execution .execute ();
80
21
}
81
22
82
- public void incrementProcessedEventsNumber () {
83
- registry
84
- .counter (
85
- PREFIX + "total.events.received" , "events" , "totalEvents" , "type" ,
86
- "eventsReceived" )
87
- .increment ();
88
-
89
- }
90
-
91
- public <T extends Map <?, ?>> T monitorSizeOf (T map , String name ) {
92
- return registry .gaugeMapSize (PREFIX + name + ".size" , Collections .emptyList (), map );
93
- }
94
-
95
- public static class NoopMeterRegistry extends MeterRegistry {
96
- public NoopMeterRegistry (Clock clock ) {
97
- super (clock );
98
- }
99
-
100
- @ Override
101
- protected <T > Gauge newGauge (Meter .Id id , T t , ToDoubleFunction <T > toDoubleFunction ) {
102
- return new NoopGauge (id );
103
- }
104
-
105
- @ Override
106
- protected Counter newCounter (Meter .Id id ) {
107
- return new NoopCounter (id );
108
- }
109
-
110
- @ Override
111
- protected Timer newTimer (
112
- Meter .Id id ,
113
- DistributionStatisticConfig distributionStatisticConfig ,
114
- PauseDetector pauseDetector ) {
115
- return new NoopTimer (id );
116
- }
117
-
118
- @ Override
119
- protected DistributionSummary newDistributionSummary (
120
- Meter .Id id , DistributionStatisticConfig distributionStatisticConfig , double v ) {
121
- return new NoopDistributionSummary (id );
122
- }
123
-
124
- @ Override
125
- protected Meter newMeter (Meter .Id id , Meter .Type type , Iterable <Measurement > iterable ) {
126
- return new NoopMeter (id );
127
- }
128
-
129
- @ Override
130
- protected <T > FunctionTimer newFunctionTimer (
131
- Meter .Id id ,
132
- T t ,
133
- ToLongFunction <T > toLongFunction ,
134
- ToDoubleFunction <T > toDoubleFunction ,
135
- TimeUnit timeUnit ) {
136
- return new NoopFunctionTimer (id );
137
- }
138
-
139
- @ Override
140
- protected <T > FunctionCounter newFunctionCounter (
141
- Meter .Id id , T t , ToDoubleFunction <T > toDoubleFunction ) {
142
- return new NoopFunctionCounter (id );
143
- }
23
+ default void incrementControllerRetriesNumber () {}
144
24
145
- @ Override
146
- protected TimeUnit getBaseTimeUnit () {
147
- return TimeUnit .SECONDS ;
148
- }
25
+ default void incrementProcessedEventsNumber () {}
149
26
150
- @ Override
151
- protected DistributionStatisticConfig defaultHistogramConfig () {
152
- return DistributionStatisticConfig .NONE ;
153
- }
27
+ default <T extends Map <?, ?>> T monitorSizeOf (T map , String name ) {
28
+ return map ;
154
29
}
155
30
}
0 commit comments