|
16 | 16 | package org.springframework.data.mongodb.observability;
|
17 | 17 |
|
18 | 18 | import static io.micrometer.core.tck.MeterRegistryAssert.*;
|
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import io.micrometer.common.KeyValues; |
| 22 | +import io.micrometer.core.instrument.MeterRegistry; |
| 23 | +import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler; |
| 24 | +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
| 25 | +import io.micrometer.observation.Observation; |
| 26 | +import io.micrometer.observation.ObservationRegistry; |
| 27 | +import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; |
19 | 28 |
|
20 | 29 | import org.bson.BsonDocument;
|
21 | 30 | import org.bson.BsonString;
|
|
33 | 42 | import com.mongodb.event.CommandStartedEvent;
|
34 | 43 | import com.mongodb.event.CommandSucceededEvent;
|
35 | 44 |
|
36 |
| -import io.micrometer.common.KeyValues; |
37 |
| -import io.micrometer.core.instrument.MeterRegistry; |
38 |
| -import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler; |
39 |
| -import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
40 |
| -import io.micrometer.observation.Observation; |
41 |
| -import io.micrometer.observation.ObservationRegistry; |
42 |
| - |
43 | 45 | /**
|
44 | 46 | * Series of test cases exercising {@link MongoObservationCommandListener}.
|
45 | 47 | *
|
46 | 48 | * @author Marcin Grzejszczak
|
47 | 49 | * @author Greg Turnquist
|
| 50 | + * @author Mark Paluch |
48 | 51 | */
|
49 | 52 | class MongoObservationCommandListenerTests {
|
50 | 53 |
|
@@ -176,6 +179,38 @@ void commandWithErrorShouldCreateTimerWhenParentSampleInRequestContext() {
|
176 | 179 | assertThatTimerRegisteredWithTags();
|
177 | 180 | }
|
178 | 181 |
|
| 182 | + @Test // GH-4481 |
| 183 | + void completionShouldIgnoreIncompatibleObservationContext() { |
| 184 | + |
| 185 | + // given |
| 186 | + RequestContext traceRequestContext = getContext(); |
| 187 | + |
| 188 | + Observation observation = mock(Observation.class); |
| 189 | + traceRequestContext.put(ObservationThreadLocalAccessor.KEY, observation); |
| 190 | + |
| 191 | + // when |
| 192 | + listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, null, "insert", null, 0)); |
| 193 | + |
| 194 | + verify(observation).getContext(); |
| 195 | + verifyNoMoreInteractions(observation); |
| 196 | + } |
| 197 | + |
| 198 | + @Test // GH-4481 |
| 199 | + void failureShouldIgnoreIncompatibleObservationContext() { |
| 200 | + |
| 201 | + // given |
| 202 | + RequestContext traceRequestContext = getContext(); |
| 203 | + |
| 204 | + Observation observation = mock(Observation.class); |
| 205 | + traceRequestContext.put(ObservationThreadLocalAccessor.KEY, observation); |
| 206 | + |
| 207 | + // when |
| 208 | + listener.commandFailed(new CommandFailedEvent(traceRequestContext, 0, null, "insert", 0, null)); |
| 209 | + |
| 210 | + verify(observation).getContext(); |
| 211 | + verifyNoMoreInteractions(observation); |
| 212 | + } |
| 213 | + |
179 | 214 | private RequestContext getContext() {
|
180 | 215 | return ((SynchronousContextProvider) ContextProviderFactory.create(observationRegistry)).getContext();
|
181 | 216 | }
|
|
0 commit comments