Skip to content

Commit c5217ff

Browse files
committed
feat: record deleted events, reconciliation success and retry info
1 parent 5a4cce3 commit c5217ff

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

micrometer-support/src/main/java/io/javaoperatorsdk/operator/monitoring/micrometer/MicrometerMetrics.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import java.util.Map;
77

8+
import io.javaoperatorsdk.operator.api.RetryInfo;
89
import io.javaoperatorsdk.operator.api.monitoring.Metrics;
910
import io.javaoperatorsdk.operator.processing.event.CustomResourceID;
1011
import io.javaoperatorsdk.operator.processing.event.Event;
@@ -13,7 +14,8 @@
1314

1415
public class MicrometerMetrics implements Metrics {
1516

16-
public static final String PREFIX = "operator.sdk.";
17+
private static final String PREFIX = "operator.sdk.";
18+
private static final String RECONCILIATIONS = "reconciliations.";
1719
private final MeterRegistry registry;
1820

1921
public MicrometerMetrics(MeterRegistry registry) {
@@ -46,11 +48,25 @@ public <T> T timeControllerExecution(ControllerExecution<T> execution) {
4648
}
4749

4850
public void receivedEvent(Event event) {
49-
incrementCounter(event, "events.received");
51+
incrementCounter(event.getRelatedCustomResourceID(), "events.received", "event",
52+
event.getClass().getSimpleName());
5053
}
5154

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");
5470
}
5571

5672
public void failedReconciliation(CustomResourceID customResourceID, RuntimeException exception) {
@@ -60,7 +76,7 @@ public void failedReconciliation(CustomResourceID customResourceID, RuntimeExcep
6076
} else if (cause instanceof RuntimeException) {
6177
cause = cause.getCause() != null ? cause.getCause() : cause;
6278
}
63-
incrementCounter(customResourceID, "reconciliation.failed", "exception",
79+
incrementCounter(customResourceID, RECONCILIATIONS + "failed", "exception",
6480
cause.getClass().getSimpleName());
6581
}
6682

@@ -69,21 +85,10 @@ public void failedReconciliation(CustomResourceID customResourceID, RuntimeExcep
6985
}
7086

7187
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");
8792
if (additionalTags != null && additionalTags.length > 0) {
8893
tags = new LinkedList<>(tags);
8994
tags.addAll(List.of(additionalTags));

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/monitoring/Metrics.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Map;
44

5+
import io.javaoperatorsdk.operator.api.RetryInfo;
56
import io.javaoperatorsdk.operator.processing.event.CustomResourceID;
67
import io.javaoperatorsdk.operator.processing.event.Event;
78

@@ -10,11 +11,16 @@ public interface Metrics {
1011

1112
default void receivedEvent(Event event) {}
1213

13-
default void reconcileCustomResource(CustomResourceID customResourceID) {}
14+
default void reconcileCustomResource(CustomResourceID customResourceID,
15+
RetryInfo retryInfo) {}
1416

1517
default void failedReconciliation(CustomResourceID customResourceID,
1618
RuntimeException exception) {}
1719

20+
default void cleanupDoneFor(CustomResourceID customResourceUid) {};
21+
22+
default void finishedReconciliation(CustomResourceID resourceID) {};
23+
1824

1925
interface ControllerExecution<T> {
2026
String name();

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/DefaultEventHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public void handleEvent(Event event) {
105105
handleEventMarking(event);
106106
if (!eventMarker.deleteEventPresent(resourceID)) {
107107
submitReconciliationExecution(resourceID);
108+
metrics.finishedReconciliation(resourceID);
108109
} else {
109110
cleanupForDeletedEvent(resourceID);
110111
}
@@ -122,12 +123,13 @@ private void submitReconciliationExecution(CustomResourceID customResourceUid) {
122123
if (!controllerUnderExecution
123124
&& latestCustomResource.isPresent()) {
124125
setUnderExecutionProcessing(customResourceUid);
126+
final var retryInfo = retryInfo(customResourceUid);
125127
ExecutionScope<R> executionScope =
126128
new ExecutionScope<>(
127129
latestCustomResource.get(),
128-
retryInfo(customResourceUid));
130+
retryInfo);
129131
eventMarker.unMarkEventReceived(customResourceUid);
130-
metrics.reconcileCustomResource(customResourceUid);
132+
metrics.reconcileCustomResource(customResourceUid, retryInfo);
131133
log.debug("Executing events for custom resource. Scope: {}", executionScope);
132134
executor.execute(new ControllerExecution(executionScope));
133135
} else {
@@ -294,6 +296,7 @@ private RetryExecution getOrInitRetryExecution(ExecutionScope<R> executionScope)
294296
private void cleanupForDeletedEvent(CustomResourceID customResourceUid) {
295297
eventSourceManager.cleanupForCustomResource(customResourceUid);
296298
eventMarker.cleanup(customResourceUid);
299+
metrics.cleanupDoneFor(customResourceUid);
297300
}
298301

299302
private boolean isControllerUnderExecution(CustomResourceID customResourceUid) {

0 commit comments

Comments
 (0)