Skip to content

Commit a42bc73

Browse files
gregturnmp911de
authored andcommitted
Provide better support for activating observability with Spring Data Cassandra.
Resolves #1321. Original pull request: #1322
1 parent ddfff47 commit a42bc73

File tree

9 files changed

+204
-32
lines changed

9 files changed

+204
-32
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/CassandraObservation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import io.micrometer.observation.docs.ObservationDocumentation;
2020

2121
/**
22-
* Cassandra-based implementation of {@link DocumentedObservation}.
22+
* Cassandra-based implementation of {@link ObservationDocumentation}.
2323
*
2424
* @author Mark Paluch
2525
* @author Marcin Grzejszczak
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.cassandra.observability;
17+
18+
import io.micrometer.observation.ObservationRegistry;
19+
import io.micrometer.tracing.Tracer;
20+
21+
import java.util.Optional;
22+
23+
import org.springframework.context.annotation.Bean;
24+
25+
/**
26+
* Set of beans enable observability with Spring Data Cassandra.
27+
*
28+
* @author Greg Turnquist
29+
* @since 4.0.0
30+
*/
31+
public class CassandraObservationConfiguration {
32+
33+
@Bean
34+
CqlSessionObservationConvention observationConvention() {
35+
return new DefaultCassandraObservationConvention();
36+
}
37+
38+
@Bean
39+
CqlSessionTracingObservationHandler cqlSessionTracingObservationHandler(
40+
Optional<ObservationRegistry> observationRegistry, Tracer tracer) {
41+
42+
CqlSessionTracingObservationHandler observationHandler = new CqlSessionTracingObservationHandler(tracer);
43+
observationRegistry.ifPresent(registry -> registry.observationConfig().observationHandler(observationHandler));
44+
return observationHandler;
45+
}
46+
47+
@Bean
48+
CqlSessionTracingBeanPostProcessor traceCqlSessionBeanPostProcessor(ObservationRegistry observationRegistry,
49+
CqlSessionObservationConvention observationConvention) {
50+
return new CqlSessionTracingBeanPostProcessor(observationRegistry, observationConvention);
51+
}
52+
}

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/CqlSessionTracingInterceptor.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private Object tracedCall(Statement<?> statement, String methodName,
102102
Function<Statement<?>, Object> statementExecutor) {
103103

104104
if (this.observationRegistry.getCurrentObservation() == null) {
105-
return null;
105+
return statementExecutor.apply(statement);
106106
}
107107

108108
Observation observation = childObservation(statement, methodName, this.delegateSession);
@@ -111,14 +111,7 @@ private Object tracedCall(Statement<?> statement, String methodName,
111111
log.debug("Created a new child observation before query [" + observation + "]");
112112
}
113113

114-
try (Observation.Scope scope = observation.openScope()) {
115-
return statementExecutor.apply(statement);
116-
} catch (Exception e) {
117-
observation.error(e);
118-
throw e;
119-
} finally {
120-
observation.stop();
121-
}
114+
return observation.observe(() -> statementExecutor.apply(statement));
122115
}
123116

124117
/**
@@ -154,7 +147,7 @@ private Observation childObservation(Statement<?> statement, String methodName,
154147
.observation(this.observationRegistry, () -> observationContext) //
155148
.contextualName(CassandraObservation.CASSANDRA_QUERY_OBSERVATION.getContextualName()) //
156149
.highCardinalityKeyValues(this.observationConvention.getHighCardinalityKeyValues(observationContext)) //
157-
.lowCardinalityKeyValues(this.observationConvention.getLowCardinalityKeyValues(observationContext)) //
158-
.start();
150+
.lowCardinalityKeyValues(this.observationConvention.getLowCardinalityKeyValues(observationContext));
151+
// .start();
159152
}
160153
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@
3232
* @author Greg Turnquist
3333
* @since 4.0.0
3434
*/
35-
public class DefaultCassandraObservationContention implements CqlSessionObservationConvention {
35+
public class DefaultCassandraObservationConvention implements CqlSessionObservationConvention {
3636

3737
@Override
3838
public KeyValues getLowCardinalityKeyValues(CqlSessionContext context) {
3939

4040
KeyValues keyValues = KeyValues.of(
4141
LowCardinalityKeyNames.SESSION_NAME
4242
.withValue(Optional.ofNullable(context.getDelegateSession().getName()).orElse("unknown")),
43-
LowCardinalityKeyNames.KEYSPACE_NAME.withValue(
44-
Optional.ofNullable(context.getStatement().getKeyspace()).map(CqlIdentifier::asInternal).orElse("unknown")),
43+
LowCardinalityKeyNames.KEYSPACE_NAME
44+
.withValue(context.getDelegateSession().getKeyspace().map(CqlIdentifier::asInternal).orElse("unknown")),
4545
LowCardinalityKeyNames.METHOD_NAME.withValue(context.getMethodName()));
4646

4747
if (context.getStatement().getNode() != null) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.cassandra.observability;
17+
18+
import java.lang.annotation.*;
19+
20+
import org.springframework.context.annotation.Import;
21+
22+
/**
23+
* Annotation to enable Cassandra observability.
24+
*
25+
* @author Greg Turnquist
26+
* @since 4.0.0
27+
*/
28+
@Inherited
29+
@Documented
30+
@Target(ElementType.TYPE)
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Import(CassandraObservationConfiguration.class)
33+
public @interface EnableCassandraObservability {
34+
}

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/observability/CqlSessionTracingBeanPostProcessorTests.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.mockito.Mockito.mock;
2020

21+
import io.micrometer.observation.ObservationHandler;
2122
import io.micrometer.observation.ObservationRegistry;
23+
import io.micrometer.tracing.Tracer;
24+
import io.micrometer.tracing.test.simple.SimpleTracer;
25+
26+
import java.lang.reflect.Method;
27+
import java.util.Collection;
2228

2329
import org.junit.jupiter.api.Test;
2430
import org.junit.jupiter.api.extension.ExtendWith;
@@ -29,6 +35,7 @@
2935
import org.springframework.context.annotation.Configuration;
3036
import org.springframework.test.context.ContextConfiguration;
3137
import org.springframework.test.context.junit.jupiter.SpringExtension;
38+
import org.springframework.util.ReflectionUtils;
3239

3340
import com.datastax.oss.driver.api.core.CqlSession;
3441

@@ -44,6 +51,8 @@
4451
public class CqlSessionTracingBeanPostProcessorTests {
4552

4653
@Autowired CqlSession session;
54+
@Autowired ObservationRegistry registry;
55+
@Autowired CqlSessionTracingObservationHandler handler;
4756

4857
@Test
4958
void injectedCqlSessionShouldBeWrapped() throws Exception {
@@ -58,7 +67,22 @@ void injectedCqlSessionShouldBeWrapped() throws Exception {
5867
assertThat(advised.getTargetSource().getTarget()).isEqualTo(TestConfig.originalSession);
5968
}
6069

70+
@Test
71+
void injectedObservationHandlerIsRegisteredWithRegistry() {
72+
73+
ObservationRegistry.ObservationConfig config = registry.observationConfig();
74+
75+
Method getObservationHandlers = ReflectionUtils.findMethod(ObservationRegistry.ObservationConfig.class,
76+
"getObservationHandlers");
77+
ReflectionUtils.makeAccessible(getObservationHandlers);
78+
Collection<ObservationHandler<?>> handlers = (Collection<ObservationHandler<?>>) ReflectionUtils
79+
.invokeMethod(getObservationHandlers, config);
80+
81+
assertThat(handlers).contains(handler);
82+
}
83+
6184
@Configuration
85+
@EnableCassandraObservability
6286
static class TestConfig {
6387

6488
static CqlSession originalSession = mock(CqlSession.class);
@@ -74,14 +98,8 @@ ObservationRegistry meterRegistry() {
7498
}
7599

76100
@Bean
77-
CqlSessionObservationConvention observationConvention() {
78-
return new DefaultCassandraObservationContention();
79-
}
80-
81-
@Bean
82-
CqlSessionTracingBeanPostProcessor traceCqlSessionBeanPostProcessor(ObservationRegistry observationRegistry,
83-
CqlSessionObservationConvention tagsProvider) {
84-
return new CqlSessionTracingBeanPostProcessor(observationRegistry, tagsProvider);
101+
Tracer tracer() {
102+
return new SimpleTracer();
85103
}
86104
}
87105
}

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/observability/CqlSessionTracingTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void tracingNoStatementsShouldProduceNoMetrics() {
9292
ObservationRegistry observationRegistry = ObservationRegistry.create();
9393
observationRegistry.observationConfig().observationHandler(new DefaultMeterObservationHandler(meterRegistry));
9494

95-
CqlSessionObservationConvention observationContention = new DefaultCassandraObservationContention();
95+
CqlSessionObservationConvention observationContention = new DefaultCassandraObservationConvention();
9696

9797
SimpleTracer tracer = new SimpleTracer();
9898
observationRegistry.observationConfig().observationHandler(new CqlSessionTracingObservationHandler(tracer));
@@ -109,7 +109,7 @@ void shouldCreateObservationForCqlSessionOperations() {
109109
ObservationRegistry observationRegistry = ObservationRegistry.create();
110110
observationRegistry.observationConfig().observationHandler(new DefaultMeterObservationHandler(meterRegistry));
111111

112-
CqlSessionObservationConvention observationContention = new DefaultCassandraObservationContention();
112+
CqlSessionObservationConvention observationContention = new DefaultCassandraObservationConvention();
113113
SimpleTracer tracer = new SimpleTracer();
114114
observationRegistry.observationConfig().observationHandler(new CqlSessionTracingObservationHandler(tracer));
115115

@@ -125,7 +125,7 @@ void shouldCreateObservationForCqlSessionOperations() {
125125

126126
MeterRegistryAssert.then(meterRegistry).hasTimerWithNameAndTags(CASSANDRA_QUERY_OBSERVATION.getName(), KeyValues.of( //
127127
LowCardinalityKeyNames.SESSION_NAME.withValue("s5"), //
128-
LowCardinalityKeyNames.KEYSPACE_NAME.withValue("unknown"), //
128+
LowCardinalityKeyNames.KEYSPACE_NAME.withValue("system"), //
129129
KeyValue.of("error", "none") //
130130
));
131131

@@ -140,7 +140,7 @@ void shouldCreateObservationForCqlSessionOperations() {
140140
.hasRemoteServiceNameEqualTo("cassandra-s5") //
141141
.hasNameEqualTo(CASSANDRA_QUERY_OBSERVATION.getContextualName()) //
142142
.hasTag(LowCardinalityKeyNames.SESSION_NAME, "s5") //
143-
.hasTag(LowCardinalityKeyNames.KEYSPACE_NAME, "unknown") //
143+
.hasTag(LowCardinalityKeyNames.KEYSPACE_NAME, "system") //
144144
.hasTag(HighCardinalityKeyNames.CQL_TAG, CREATE_KEYSPACE) //
145145
.hasIpThatIsBlank() //
146146
.hasPortEqualTo(0) //

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/observability/ZipkinIntegrationTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Deque;
2828
import java.util.function.BiConsumer;
2929

30-
import org.junit.jupiter.api.Disabled;
3130
import org.junit.jupiter.api.extension.ExtendWith;
3231
import org.springframework.beans.factory.annotation.Autowired;
3332
import org.springframework.context.annotation.Bean;
@@ -46,7 +45,6 @@
4645
* @author Greg Turnquist
4746
* @since 4.0.0
4847
*/
49-
@Disabled("Run this manually to visually test spans in Zipkin")
5048
@ExtendWith({ SpringExtension.class, CassandraExtension.class })
5149
@TestKeyspaceName
5250
public class ZipkinIntegrationTests extends SampleTestRunner {
@@ -91,6 +89,9 @@ public SampleTestRunnerConsumer yourCode() {
9189

9290
return (tracer, meterRegistry) -> {
9391

92+
OBSERVATION_REGISTRY.observationConfig()
93+
.observationHandler(new CqlSessionTracingObservationHandler(tracer.getTracer()));
94+
9495
session.execute("DROP KEYSPACE IF EXISTS ConfigTest");
9596
session.execute("CREATE KEYSPACE ConfigTest " + "WITH "
9697
+ "REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
@@ -117,13 +118,13 @@ ObservationRegistry registry() {
117118

118119
@Bean
119120
CqlSessionObservationConvention observationContention() {
120-
return new DefaultCassandraObservationContention();
121+
return new DefaultCassandraObservationConvention();
121122
}
122123

123124
@Bean
124125
CqlSessionTracingBeanPostProcessor traceCqlSessionBeanPostProcessor(ObservationRegistry observationRegistry,
125-
CqlSessionObservationConvention tagsProvider) {
126-
return new CqlSessionTracingBeanPostProcessor(observationRegistry, tagsProvider);
126+
CqlSessionObservationConvention observationConvention) {
127+
return new CqlSessionTracingBeanPostProcessor(observationRegistry, observationConvention);
127128
}
128129
}
129130
}

src/main/asciidoc/reference/observability.adoc

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,77 @@ include::{root-target}_conventions.adoc[]
88
include::{root-target}_metrics.adoc[]
99

1010
include::{root-target}_spans.adoc[]
11+
12+
[[observability.registration]]
13+
== Observability Registration
14+
15+
Spring Data Cassandra is not yet supported in Spring Boot to automatically enable observability.
16+
Don't worry, we've got you covered!
17+
Simply add the `@EnableCassandraObservability` annotation to your Spring Boot application, and you'll be all set.
18+
19+
.Activating observability for your Spring Boot application
20+
====
21+
[source,java]
22+
----
23+
@SpringBootApplication
24+
@EnableCassandraObservability // <1>
25+
public class SpringDataCassandraObservabilityApplication {
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(SpringDataCassandraObservabilityApplication.class, args);
29+
}
30+
}
31+
----
32+
<1> This annotation will activate the bits needed start wrapping CQL calls and register them with your tracer of choice.
33+
====
34+
35+
By the way, Spring Boot DOES have autoconfigured hooks into various parts of the system.
36+
For example, there is an observation filter that Spring Boot will apply to Spring MVC ensuring all your calls are wrapped properly.
37+
And if anywhere in the midst of that web call, you invoke Spring Data Cassandra (either through `CqlTemplate` or a custom repository), it will get captured properly.
38+
39+
Something that is NOT covered are situations where your code runs independently.
40+
For example, if you have some block that is run during startup inside a `CommandLineRunner`, there is no way for Spring Boot to know that this should observed.
41+
Check out the code block below:
42+
43+
.Loading data for a sample application
44+
====
45+
[source,java]
46+
----
47+
@Bean
48+
CommandLineRunner initData(EmployeeRepository repository) {
49+
return args -> {
50+
repository.save(new Employee("1", "Frodo", "ring bearer"));
51+
repository.save(new Employee("2", "Bilbo", "burglar"));
52+
};
53+
}
54+
----
55+
This tactic is used all the time in demos. These calls to a Spring Data Cassandra repository will NOT be observed.
56+
====
57+
58+
If you DO want to observe such a code block, you must wrap it yourself, as shown below:
59+
60+
.Observing the loading of data in a sample application
61+
====
62+
[source,java]
63+
----
64+
@Bean
65+
CommandLineRunner initData(EmployeeRepository repository, ObservationRegistry registry) { // <1>
66+
return args -> {
67+
Observation.createNotStarted("init-database", registry).observe(() -> { // <2>
68+
repository.save(new Employee("1", "Frodo", "ring bearer"));
69+
repository.save(new Employee("2", "Bilbo", "burglar"));
70+
});
71+
};
72+
}
73+
----
74+
<1> Your `CommandLineRunner` requires access to the app context's `ObservationRegistry`
75+
<2> You need to create your own `Observation` using the `createNotStarted()` method. Give it any contextual name you like, but be sure to include the `registry`.
76+
====
77+
78+
The `observe()` method takes a Java 8 lambda function which is invoked inside a common Micrometer pattern of:
79+
80+
* Starting the observation.
81+
* Invoking your callback.
82+
* Properly ending the observation by either reporting an error if it fails or stopping the observation if it succeeds.
83+
84+
This will allow you to observe chunks of code that may fall outside of currently autoconfigured operations.

0 commit comments

Comments
 (0)