5
5
import java .util .List ;
6
6
import java .util .Map ;
7
7
8
+ import io .javaoperatorsdk .operator .api .RetryInfo ;
8
9
import io .javaoperatorsdk .operator .api .monitoring .Metrics ;
9
10
import io .javaoperatorsdk .operator .processing .event .CustomResourceID ;
10
11
import io .javaoperatorsdk .operator .processing .event .Event ;
13
14
14
15
public class MicrometerMetrics implements Metrics {
15
16
16
- public static final String PREFIX = "operator.sdk." ;
17
+ private static final String PREFIX = "operator.sdk." ;
18
+ private static final String RECONCILIATIONS = "reconciliations." ;
17
19
private final MeterRegistry registry ;
18
20
19
21
public MicrometerMetrics (MeterRegistry registry ) {
@@ -46,11 +48,25 @@ public <T> T timeControllerExecution(ControllerExecution<T> execution) {
46
48
}
47
49
48
50
public void receivedEvent (Event event ) {
49
- incrementCounter (event , "events.received" );
51
+ incrementCounter (event .getRelatedCustomResourceID (), "events.received" , "event" ,
52
+ event .getClass ().getSimpleName ());
50
53
}
51
54
52
- public void reconcileCustomResource (CustomResourceID customResourceID ) {
53
- incrementCounter (customResourceID , "reconciliation.times" );
55
+ @ Override
56
+ public void cleanupDoneFor (CustomResourceID customResourceUid ) {
57
+ incrementCounter (customResourceUid , "events.delete" );
58
+ }
59
+
60
+ public void reconcileCustomResource (CustomResourceID customResourceID ,
61
+ RetryInfo retryInfo ) {
62
+ incrementCounter (customResourceID , RECONCILIATIONS + "started" ,
63
+ RECONCILIATIONS + "retries.number" , "" + retryInfo .getAttemptCount (),
64
+ RECONCILIATIONS + "retries.last" , "" + retryInfo .isLastAttempt ());
65
+ }
66
+
67
+ @ Override
68
+ public void finishedReconciliation (CustomResourceID customResourceID ) {
69
+ incrementCounter (customResourceID , RECONCILIATIONS + "success" );
54
70
}
55
71
56
72
public void failedReconciliation (CustomResourceID customResourceID , RuntimeException exception ) {
@@ -60,7 +76,7 @@ public void failedReconciliation(CustomResourceID customResourceID, RuntimeExcep
60
76
} else if (cause instanceof RuntimeException ) {
61
77
cause = cause .getCause () != null ? cause .getCause () : cause ;
62
78
}
63
- incrementCounter (customResourceID , "reconciliation. failed" , "exception" ,
79
+ incrementCounter (customResourceID , RECONCILIATIONS + " failed" , "exception" ,
64
80
cause .getClass ().getSimpleName ());
65
81
}
66
82
@@ -69,21 +85,10 @@ public void failedReconciliation(CustomResourceID customResourceID, RuntimeExcep
69
85
}
70
86
71
87
private void incrementCounter (CustomResourceID id , String counterName , String ... additionalTags ) {
72
- var tags = List .of ("name" , id .getName (), "namespace" , id .getNamespace ().orElse ("" ),
73
- "scope" , id .getNamespace ().isPresent () ? "namespace" : "cluster" ,
74
- "type" , "reconciliation" );
75
- if (additionalTags != null && additionalTags .length > 0 ) {
76
- tags = new LinkedList <>(tags );
77
- tags .addAll (List .of (additionalTags ));
78
- }
79
- registry .counter (PREFIX + counterName , tags .toArray (new String [0 ])).increment ();
80
- }
81
-
82
- private void incrementCounter (Event event , String counterName , String ... additionalTags ) {
83
- final var id = event .getRelatedCustomResourceID ();
84
- var tags = List .of ("name" , id .getName (), "namespace" , id .getNamespace ().orElse ("" ),
85
- "scope" , id .getNamespace ().isPresent () ? "namespace" : "cluster" ,
86
- "type" , event .getClass ().getSimpleName ());
88
+ var tags = List .of (
89
+ "name" , id .getName (),
90
+ "name" , id .getName (), "namespace" , id .getNamespace ().orElse ("" ),
91
+ "scope" , id .getNamespace ().isPresent () ? "namespace" : "cluster" );
87
92
if (additionalTags != null && additionalTags .length > 0 ) {
88
93
tags = new LinkedList <>(tags );
89
94
tags .addAll (List .of (additionalTags ));
0 commit comments