19
19
import io .micrometer .observation .Observation .Context ;
20
20
import io .micrometer .observation .Observation .Event ;
21
21
22
+ import java .util .function .Consumer ;
23
+
22
24
import org .apache .commons .logging .Log ;
23
25
import org .apache .commons .logging .LogFactory ;
26
+
24
27
import org .springframework .data .cassandra .observability .CassandraObservation .Events ;
25
28
import org .springframework .data .cassandra .observability .CassandraObservation .HighCardinalityKeyNames ;
26
29
import org .springframework .lang .Nullable ;
@@ -50,9 +53,9 @@ public enum ObservationRequestTracker implements RequestTracker {
50
53
public void onSuccess (Request request , long latencyNanos , DriverExecutionProfile executionProfile , Node node ,
51
54
String requestLogPrefix ) {
52
55
53
- if (request instanceof CassandraObservationSupplier ) {
56
+ if (request instanceof CassandraObservationSupplier supplier ) {
54
57
55
- Observation observation = (( CassandraObservationSupplier ) request ) .getObservation ();
58
+ Observation observation = supplier .getObservation ();
56
59
57
60
if (log .isDebugEnabled ()) {
58
61
log .debug ("Closing observation [" + observation + "]" );
@@ -66,9 +69,9 @@ public void onSuccess(Request request, long latencyNanos, DriverExecutionProfile
66
69
public void onError (Request request , Throwable error , long latencyNanos , DriverExecutionProfile executionProfile ,
67
70
@ Nullable Node node , String requestLogPrefix ) {
68
71
69
- if (request instanceof CassandraObservationSupplier ) {
72
+ if (request instanceof CassandraObservationSupplier supplier ) {
70
73
71
- Observation observation = (( CassandraObservationSupplier ) request ) .getObservation ();
74
+ Observation observation = supplier .getObservation ();
72
75
observation .error (error );
73
76
74
77
if (log .isDebugEnabled ()) {
@@ -83,22 +86,17 @@ public void onError(Request request, Throwable error, long latencyNanos, DriverE
83
86
public void onNodeError (Request request , Throwable error , long latencyNanos , DriverExecutionProfile executionProfile ,
84
87
Node node , String requestLogPrefix ) {
85
88
86
- if (request instanceof CassandraObservationSupplier ) {
87
-
88
- Observation observation = ((CassandraObservationSupplier ) request ).getObservation ();
89
- Context context = observation .getContext ();
90
-
91
- if (context instanceof CassandraObservationContext ) {
89
+ if (request instanceof CassandraObservationSupplier supplier ) {
92
90
93
- ((CassandraObservationContext ) context ).setNode (node );
91
+ Observation observation = supplier .getObservation ();
92
+ ifContextPresent (observation , CassandraObservationContext .class , context -> context .setNode (node ));
94
93
95
- observation .highCardinalityKeyValue (
96
- String .format (HighCardinalityKeyNames .NODE_ERROR_TAG .asString (), node .getEndPoint ()), error .toString ());
97
- observation .event (Event .of (Events .NODE_ERROR .getValue ()));
94
+ observation .highCardinalityKeyValue (
95
+ String .format (HighCardinalityKeyNames .NODE_ERROR_TAG .asString (), node .getEndPoint ()), error .toString ());
96
+ observation .event (Event .of (Events .NODE_ERROR .getValue ()));
98
97
99
- if (log .isDebugEnabled ()) {
100
- log .debug ("Marking node error for [" + observation + "]" );
101
- }
98
+ if (log .isDebugEnabled ()) {
99
+ log .debug ("Marking node error for [" + observation + "]" );
102
100
}
103
101
}
104
102
}
@@ -107,20 +105,15 @@ public void onNodeError(Request request, Throwable error, long latencyNanos, Dri
107
105
public void onNodeSuccess (Request request , long latencyNanos , DriverExecutionProfile executionProfile , Node node ,
108
106
String requestLogPrefix ) {
109
107
110
- if (request instanceof CassandraObservationSupplier ) {
111
-
112
- Observation observation = ((CassandraObservationSupplier ) request ).getObservation ();
113
- Context context = observation .getContext ();
108
+ if (request instanceof CassandraObservationSupplier supplier ) {
114
109
115
- if (context instanceof CassandraObservationContext ) {
110
+ Observation observation = supplier .getObservation ();
111
+ ifContextPresent (observation , CassandraObservationContext .class , context -> context .setNode (node ));
116
112
117
- (( CassandraObservationContext ) context ). setNode ( node );
113
+ observation . event ( Event . of ( Events . NODE_SUCCESS . getValue ()) );
118
114
119
- observation .event (Event .of (Events .NODE_SUCCESS .getValue ()));
120
-
121
- if (log .isDebugEnabled ()) {
122
- log .debug ("Marking node success for [" + observation + "]" );
123
- }
115
+ if (log .isDebugEnabled ()) {
116
+ log .debug ("Marking node success for [" + observation + "]" );
124
117
}
125
118
}
126
119
}
@@ -130,4 +123,21 @@ public void close() throws Exception {
130
123
131
124
}
132
125
126
+ /**
127
+ * If the {@link Observation} is a real observation (i.e. not no-op) and the context is of the given type, apply the
128
+ * consumer function to the context.
129
+ */
130
+ static <T extends Context > void ifContextPresent (Observation observation , Class <T > contextType ,
131
+ Consumer <T > contextConsumer ) {
132
+
133
+ if (observation .isNoop ()) {
134
+ return ;
135
+ }
136
+
137
+ Context context = observation .getContext ();
138
+ if (contextType .isInstance (context )) {
139
+ contextConsumer .accept (contextType .cast (context ));
140
+ }
141
+ }
142
+
133
143
}
0 commit comments