Skip to content

Commit baf94e7

Browse files
authored
fix: measure total execution time in metrics (operator-framework#1059)
* fix: measure total time execution (reconcile/cleanup) * refactor: optimize controller metrics call
1 parent c1467e8 commit baf94e7

File tree

1 file changed

+9
-10
lines changed
  • operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing

1 file changed

+9
-10
lines changed

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.LinkedList;
44
import java.util.List;
5+
import java.util.Optional;
56
import java.util.stream.Collectors;
67

78
import org.slf4j.Logger;
@@ -54,13 +55,17 @@ public class Controller<P extends HasMetadata> implements Reconciler<P>, Cleaner
5455
private final boolean contextInitializer;
5556
private final boolean hasDeleterDependents;
5657
private final boolean isCleaner;
58+
private final Metrics metrics;
59+
5760

5861
public Controller(Reconciler<P> reconciler,
5962
ControllerConfiguration<P> configuration,
6063
KubernetesClient kubernetesClient) {
6164
this.reconciler = reconciler;
6265
this.configuration = configuration;
6366
this.kubernetesClient = kubernetesClient;
67+
this.metrics = Optional.ofNullable(ConfigurationServiceProvider.instance().getMetrics())
68+
.orElse(Metrics.NOOP);
6469
contextInitializer = reconciler instanceof ContextInitializer;
6570

6671
eventSourceManager = new EventSourceManager<>(this);
@@ -105,9 +110,8 @@ private void initContextIfNeeded(P resource, Context<P> context) {
105110

106111
@Override
107112
public DeleteControl cleanup(P resource, Context<P> context) {
108-
initContextIfNeeded(resource, context);
109113
try {
110-
return metrics()
114+
return metrics
111115
.timeControllerExecution(
112116
new ControllerExecution<>() {
113117
@Override
@@ -127,6 +131,7 @@ public String successTypeName(DeleteControl deleteControl) {
127131

128132
@Override
129133
public DeleteControl execute() {
134+
initContextIfNeeded(resource, context);
130135
if (hasDeleterDependents) {
131136
dependents.stream()
132137
.filter(d -> d instanceof Deleter)
@@ -147,8 +152,7 @@ public DeleteControl execute() {
147152

148153
@Override
149154
public UpdateControl<P> reconcile(P resource, Context<P> context) throws Exception {
150-
initContextIfNeeded(resource, context);
151-
return metrics().timeControllerExecution(
155+
return metrics.timeControllerExecution(
152156
new ControllerExecution<>() {
153157
@Override
154158
public String name() {
@@ -174,18 +178,13 @@ public String successTypeName(UpdateControl<P> result) {
174178

175179
@Override
176180
public UpdateControl<P> execute() throws Exception {
181+
initContextIfNeeded(resource, context);
177182
dependents.forEach(dependent -> dependent.reconcile(resource, context));
178183
return reconciler.reconcile(resource, context);
179184
}
180185
});
181186
}
182187

183-
184-
private Metrics metrics() {
185-
final var metrics = ConfigurationServiceProvider.instance().getMetrics();
186-
return metrics != null ? metrics : Metrics.NOOP;
187-
}
188-
189188
@Override
190189
public List<EventSource> prepareEventSources(EventSourceContext<P> context) {
191190
List<EventSource> sources = new LinkedList<>();

0 commit comments

Comments
 (0)