1919import  io .micrometer .observation .Observation .Context ;
2020import  io .micrometer .observation .Observation .Event ;
2121
22+ import  java .util .function .Consumer ;
23+ 
2224import  org .apache .commons .logging .Log ;
2325import  org .apache .commons .logging .LogFactory ;
26+ 
2427import  org .springframework .data .cassandra .observability .CassandraObservation .Events ;
2528import  org .springframework .data .cassandra .observability .CassandraObservation .HighCardinalityKeyNames ;
2629import  org .springframework .lang .Nullable ;
@@ -50,9 +53,9 @@ public enum ObservationRequestTracker implements RequestTracker {
5053	public  void  onSuccess (Request  request , long  latencyNanos , DriverExecutionProfile  executionProfile , Node  node ,
5154			String  requestLogPrefix ) {
5255
53- 		if  (request  instanceof  CassandraObservationSupplier ) {
56+ 		if  (request  instanceof  CassandraObservationSupplier   supplier ) {
5457
55- 			Observation  observation  = (( CassandraObservationSupplier )  request ) .getObservation ();
58+ 			Observation  observation  = supplier .getObservation ();
5659
5760			if  (log .isDebugEnabled ()) {
5861				log .debug ("Closing observation ["  + observation  + "]" );
@@ -66,9 +69,9 @@ public void onSuccess(Request request, long latencyNanos, DriverExecutionProfile
6669	public  void  onError (Request  request , Throwable  error , long  latencyNanos , DriverExecutionProfile  executionProfile ,
6770			@ Nullable  Node  node , String  requestLogPrefix ) {
6871
69- 		if  (request  instanceof  CassandraObservationSupplier ) {
72+ 		if  (request  instanceof  CassandraObservationSupplier   supplier ) {
7073
71- 			Observation  observation  = (( CassandraObservationSupplier )  request ) .getObservation ();
74+ 			Observation  observation  = supplier .getObservation ();
7275			observation .error (error );
7376
7477			if  (log .isDebugEnabled ()) {
@@ -83,22 +86,17 @@ public void onError(Request request, Throwable error, long latencyNanos, DriverE
8386	public  void  onNodeError (Request  request , Throwable  error , long  latencyNanos , DriverExecutionProfile  executionProfile ,
8487			Node  node , String  requestLogPrefix ) {
8588
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 ) {
9290
93- 				((CassandraObservationContext ) context ).setNode (node );
91+ 			Observation  observation  = supplier .getObservation ();
92+ 			ifContextPresent (observation , CassandraObservationContext .class , context  -> context .setNode (node ));
9493
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 ()));
9897
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  + "]" );
102100			}
103101		}
104102	}
@@ -107,20 +105,15 @@ public void onNodeError(Request request, Throwable error, long latencyNanos, Dri
107105	public  void  onNodeSuccess (Request  request , long  latencyNanos , DriverExecutionProfile  executionProfile , Node  node ,
108106			String  requestLogPrefix ) {
109107
110- 		if  (request  instanceof  CassandraObservationSupplier ) {
111- 
112- 			Observation  observation  = ((CassandraObservationSupplier ) request ).getObservation ();
113- 			Context  context  = observation .getContext ();
108+ 		if  (request  instanceof  CassandraObservationSupplier  supplier ) {
114109
115- 			if  (context  instanceof  CassandraObservationContext ) {
110+ 			Observation  observation  = supplier .getObservation ();
111+ 			ifContextPresent (observation , CassandraObservationContext .class , context  -> context .setNode (node ));
116112
117- 				(( CassandraObservationContext )  context ). setNode ( node );
113+ 			observation . event ( Event . of ( Events . NODE_SUCCESS . getValue ()) );
118114
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  + "]" );
124117			}
125118		}
126119	}
@@ -130,4 +123,21 @@ public void close() throws Exception {
130123
131124	}
132125
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+ 
133143}
0 commit comments