14
14
import org .slf4j .LoggerFactory ;
15
15
16
16
import io .fabric8 .kubernetes .client .CustomResource ;
17
+ import io .javaoperatorsdk .operator .Metrics ;
17
18
import io .javaoperatorsdk .operator .api .RetryInfo ;
18
19
import io .javaoperatorsdk .operator .api .config .ConfigurationService ;
19
20
import io .javaoperatorsdk .operator .api .config .ExecutorServiceManager ;
35
36
public class DefaultEventHandler <R extends CustomResource <?, ?>> implements EventHandler {
36
37
37
38
private static final Logger log = LoggerFactory .getLogger (DefaultEventHandler .class );
38
- private static EventMonitor monitor = new EventMonitor () {
39
- @ Override
40
- public void processedEvent (String uid , Event event ) {}
41
39
42
- @ Override
43
- public void failedEvent (String uid , Event event ) {}
44
- };
40
+ @ Deprecated
41
+ private static EventMonitor monitor = EventMonitor .NOOP ;
45
42
46
43
private final EventBuffer eventBuffer ;
47
44
private final Set <String > underProcessing = new HashSet <>();
@@ -51,23 +48,25 @@ public void failedEvent(String uid, Event event) {}
51
48
private final ExecutorService executor ;
52
49
private final String controllerName ;
53
50
private final ReentrantLock lock = new ReentrantLock ();
51
+ private final EventMonitor eventMonitor ;
54
52
private volatile boolean running ;
55
53
private DefaultEventSourceManager <R > eventSourceManager ;
56
54
57
55
public DefaultEventHandler (ConfiguredController <R > controller ) {
58
56
this (ExecutorServiceManager .instance ().executorService (),
59
57
controller .getConfiguration ().getName (),
60
58
new EventDispatcher <>(controller ),
61
- GenericRetry .fromConfiguration (controller .getConfiguration ().getRetryConfiguration ()));
59
+ GenericRetry .fromConfiguration (controller .getConfiguration ().getRetryConfiguration ()),
60
+ controller .getConfiguration ().getConfigurationService ().getMetrics ().getEventMonitor ());
62
61
}
63
62
64
63
DefaultEventHandler (EventDispatcher <R > eventDispatcher , String relatedControllerName ,
65
64
Retry retry ) {
66
- this (null , relatedControllerName , eventDispatcher , retry );
65
+ this (null , relatedControllerName , eventDispatcher , retry , null );
67
66
}
68
67
69
68
private DefaultEventHandler (ExecutorService executor , String relatedControllerName ,
70
- EventDispatcher <R > eventDispatcher , Retry retry ) {
69
+ EventDispatcher <R > eventDispatcher , Retry retry , EventMonitor monitor ) {
71
70
this .running = true ;
72
71
this .executor =
73
72
executor == null
@@ -78,14 +77,44 @@ private DefaultEventHandler(ExecutorService executor, String relatedControllerNa
78
77
this .eventDispatcher = eventDispatcher ;
79
78
this .retry = retry ;
80
79
this .eventBuffer = new EventBuffer ();
80
+ this .eventMonitor = monitor != null ? monitor : EventMonitor .NOOP ;
81
+ }
82
+
83
+ public void setEventSourceManager (DefaultEventSourceManager <R > eventSourceManager ) {
84
+ this .eventSourceManager = eventSourceManager ;
81
85
}
82
86
87
+ /**
88
+ * @deprecated the EventMonitor to be used should now be retrieved from
89
+ * {@link Metrics#getEventMonitor()}
90
+ * @param monitor
91
+ */
92
+ @ Deprecated
83
93
public static void setEventMonitor (EventMonitor monitor ) {
84
94
DefaultEventHandler .monitor = monitor ;
85
95
}
86
96
87
- public void setEventSourceManager (DefaultEventSourceManager <R > eventSourceManager ) {
88
- this .eventSourceManager = eventSourceManager ;
97
+ /*
98
+ * TODO: promote this interface to top-level, probably create a `monitoring` package?
99
+ */
100
+ public interface EventMonitor {
101
+ EventMonitor NOOP = new EventMonitor () {
102
+ @ Override
103
+ public void processedEvent (String uid , Event event ) {}
104
+
105
+ @ Override
106
+ public void failedEvent (String uid , Event event ) {}
107
+ };
108
+
109
+ void processedEvent (String uid , Event event );
110
+
111
+ void failedEvent (String uid , Event event );
112
+ }
113
+
114
+ private EventMonitor monitor () {
115
+ // todo: remove us of static monitor, only here for backwards compatibility
116
+ return DefaultEventHandler .monitor != EventMonitor .NOOP ? DefaultEventHandler .monitor
117
+ : eventMonitor ;
89
118
}
90
119
91
120
@ Override
@@ -102,6 +131,7 @@ public void handleEvent(Event event) {
102
131
log .debug ("Received event: {}" , event );
103
132
104
133
final Predicate <CustomResource > selector = event .getCustomResourcesSelector ();
134
+ final var monitor = monitor ();
105
135
for (String uid : eventSourceManager .getLatestResourceUids (selector )) {
106
136
eventBuffer .addEvent (uid , event );
107
137
monitor .processedEvent (uid , event );
@@ -168,6 +198,7 @@ void eventProcessingFinished(
168
198
169
199
if (retry != null && postExecutionControl .exceptionDuringExecution ()) {
170
200
handleRetryOnException (executionScope );
201
+ final var monitor = monitor ();
171
202
executionScope .getEvents ()
172
203
.forEach (e -> monitor .failedEvent (executionScope .getCustomResourceUid (), e ));
173
204
return ;
@@ -296,11 +327,6 @@ private void unsetUnderExecution(String customResourceUid) {
296
327
underProcessing .remove (customResourceUid );
297
328
}
298
329
299
- public interface EventMonitor {
300
- void processedEvent (String uid , Event event );
301
-
302
- void failedEvent (String uid , Event event );
303
- }
304
330
305
331
private class ControllerExecution implements Runnable {
306
332
private final ExecutionScope <R > executionScope ;
0 commit comments