From 74fca38598dff31993a5296a70ef2a61b4e4dd66 Mon Sep 17 00:00:00 2001 From: cfredri4 <50839054+cfredri4@users.noreply.github.com> Date: Thu, 28 Nov 2024 11:15:07 +0100 Subject: [PATCH] Fix `ClassCastException` in `ObservationRequestTracker`. This fixes a ClassCastException for CassandraObservationContext when a NoopObservation is returned by the observation registry. Closes #1541 --- .../ObservationRequestTracker.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/ObservationRequestTracker.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/ObservationRequestTracker.java index a3d040097..79e2c4846 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/ObservationRequestTracker.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/ObservationRequestTracker.java @@ -16,6 +16,7 @@ package org.springframework.data.cassandra.observability; import io.micrometer.observation.Observation; +import io.micrometer.observation.Observation.Context; import io.micrometer.observation.Observation.Event; import org.apache.commons.logging.Log; @@ -85,15 +86,19 @@ public void onNodeError(Request request, Throwable error, long latencyNanos, Dri if (request instanceof CassandraObservationSupplier) { Observation observation = ((CassandraObservationSupplier) request).getObservation(); + Context context = observation.getContext(); - ((CassandraObservationContext) observation.getContext()).setNode(node); + if (context instanceof CassandraObservationContext) { - observation.highCardinalityKeyValue( - String.format(HighCardinalityKeyNames.NODE_ERROR_TAG.asString(), node.getEndPoint()), error.toString()); - observation.event(Event.of(Events.NODE_ERROR.getValue())); + ((CassandraObservationContext) context).setNode(node); - if (log.isDebugEnabled()) { - log.debug("Marking node error for [" + observation + "]"); + observation.highCardinalityKeyValue( + String.format(HighCardinalityKeyNames.NODE_ERROR_TAG.asString(), node.getEndPoint()), error.toString()); + observation.event(Event.of(Events.NODE_ERROR.getValue())); + + if (log.isDebugEnabled()) { + log.debug("Marking node error for [" + observation + "]"); + } } } } @@ -105,13 +110,17 @@ public void onNodeSuccess(Request request, long latencyNanos, DriverExecutionPro if (request instanceof CassandraObservationSupplier) { Observation observation = ((CassandraObservationSupplier) request).getObservation(); + Context context = observation.getContext(); - ((CassandraObservationContext) observation.getContext()).setNode(node); + if (context instanceof CassandraObservationContext) { - observation.event(Event.of(Events.NODE_SUCCESS.getValue())); + ((CassandraObservationContext) context).setNode(node); - if (log.isDebugEnabled()) { - log.debug("Marking node success for [" + observation + "]"); + observation.event(Event.of(Events.NODE_SUCCESS.getValue())); + + if (log.isDebugEnabled()) { + log.debug("Marking node success for [" + observation + "]"); + } } } }