|
4 | 4 | import java.util.function.ToDoubleFunction;
|
5 | 5 | import java.util.function.ToLongFunction;
|
6 | 6 |
|
7 |
| -import io.fabric8.kubernetes.client.CustomResource; |
8 |
| -import io.javaoperatorsdk.operator.api.Context; |
9 |
| -import io.javaoperatorsdk.operator.api.DeleteControl; |
10 |
| -import io.javaoperatorsdk.operator.api.ResourceController; |
11 |
| -import io.javaoperatorsdk.operator.api.UpdateControl; |
12 |
| -import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; |
13 | 7 | import io.micrometer.core.instrument.Clock;
|
14 | 8 | import io.micrometer.core.instrument.Counter;
|
15 | 9 | import io.micrometer.core.instrument.DistributionSummary;
|
@@ -38,76 +32,36 @@ public Metrics(MeterRegistry registry) {
|
38 | 32 | this.registry = registry;
|
39 | 33 | }
|
40 | 34 |
|
41 |
| - public <R extends CustomResource> UpdateControl<R> timeControllerCreateOrUpdate( |
42 |
| - ResourceController<R> controller, |
43 |
| - ControllerConfiguration<R> configuration, |
44 |
| - R resource, |
45 |
| - Context<R> context) { |
46 |
| - final var name = configuration.getName(); |
47 |
| - final var timer = |
48 |
| - Timer.builder("operator.sdk.controllers.execution.createorupdate") |
49 |
| - .tags("controller", name) |
50 |
| - .publishPercentiles(0.3, 0.5, 0.95) |
51 |
| - .publishPercentileHistogram() |
52 |
| - .register(registry); |
53 |
| - try { |
54 |
| - final var result = timer.record(() -> controller.createOrUpdateResource(resource, context)); |
55 |
| - String successType = "cr"; |
56 |
| - if (result.isUpdateStatusSubResource()) { |
57 |
| - successType = "status"; |
58 |
| - } |
59 |
| - if (result.isUpdateCustomResourceAndStatusSubResource()) { |
60 |
| - successType = "both"; |
61 |
| - } |
62 |
| - registry |
63 |
| - .counter( |
64 |
| - "operator.sdk.controllers.execution.success", "controller", name, "type", successType) |
65 |
| - .increment(); |
66 |
| - return result; |
67 |
| - } catch (Exception e) { |
68 |
| - registry |
69 |
| - .counter( |
70 |
| - "operator.sdk.controllers.execution.failure", |
71 |
| - "controller", |
72 |
| - name, |
73 |
| - "exception", |
74 |
| - e.getClass().getSimpleName()) |
75 |
| - .increment(); |
76 |
| - throw e; |
77 |
| - } |
| 35 | + public interface ControllerExecution<T> { |
| 36 | + String name(); |
| 37 | + |
| 38 | + String controllerName(); |
| 39 | + |
| 40 | + String successTypeName(T result); |
| 41 | + |
| 42 | + T execute(); |
78 | 43 | }
|
79 | 44 |
|
80 |
| - public DeleteControl timeControllerDelete( |
81 |
| - ResourceController controller, |
82 |
| - ControllerConfiguration configuration, |
83 |
| - CustomResource resource, |
84 |
| - Context context) { |
85 |
| - final var name = configuration.getName(); |
| 45 | + public <T> T timeControllerExecution(ControllerExecution<T> execution) { |
| 46 | + final var name = execution.controllerName(); |
| 47 | + final var execName = "operator.sdk.controllers.execution." + execution.name(); |
86 | 48 | final var timer =
|
87 |
| - Timer.builder("operator.sdk.controllers.execution.delete") |
| 49 | + Timer.builder(execName) |
88 | 50 | .tags("controller", name)
|
89 | 51 | .publishPercentiles(0.3, 0.5, 0.95)
|
90 | 52 | .publishPercentileHistogram()
|
91 | 53 | .register(registry);
|
92 | 54 | try {
|
93 |
| - final var result = timer.record(() -> controller.deleteResource(resource, context)); |
94 |
| - String successType = "notDelete"; |
95 |
| - if (result == DeleteControl.DEFAULT_DELETE) { |
96 |
| - successType = "delete"; |
97 |
| - } |
| 55 | + final var result = timer.record(execution::execute); |
| 56 | + final var successType = execution.successTypeName(result); |
98 | 57 | registry
|
99 |
| - .counter( |
100 |
| - "operator.sdk.controllers.execution.success", "controller", name, "type", successType) |
| 58 | + .counter(execName + ".success", "controller", name, "type", successType) |
101 | 59 | .increment();
|
102 | 60 | return result;
|
103 | 61 | } catch (Exception e) {
|
| 62 | + final var exception = e.getClass().getSimpleName(); |
104 | 63 | registry
|
105 |
| - .counter( |
106 |
| - "operator.sdk.controllers.execution.failure", |
107 |
| - "controller", |
108 |
| - name, |
109 |
| - "exception", |
110 |
| - e.getClass().getSimpleName()) |
| 64 | + .counter(execName + ".failure", "controller", name, "exception", exception) |
111 | 65 | .increment();
|
112 | 66 | throw e;
|
113 | 67 | }
|
|
0 commit comments