|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 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.mongodb.observability; |
| 17 | + |
| 18 | +import com.mongodb.ServerAddress; |
| 19 | +import com.mongodb.connection.ClusterId; |
| 20 | +import com.mongodb.connection.ConnectionDescription; |
| 21 | +import com.mongodb.connection.ServerId; |
| 22 | +import com.mongodb.event.CommandFailedEvent; |
| 23 | +import com.mongodb.event.CommandStartedEvent; |
| 24 | +import com.mongodb.event.CommandSucceededEvent; |
| 25 | +import io.micrometer.core.instrument.Tag; |
| 26 | +import io.micrometer.core.instrument.Tags; |
| 27 | +import io.micrometer.core.instrument.Timer; |
| 28 | +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
| 29 | +import io.micrometer.core.tck.MeterRegistryAssert; |
| 30 | +import org.bson.BsonDocument; |
| 31 | +import org.bson.BsonString; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import static io.micrometer.core.tck.MeterRegistryAssert.assertThat; |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +class MicrometerMongoCommandListenerTests { |
| 38 | + |
| 39 | + SimpleMeterRegistry registry = new SimpleMeterRegistry(); |
| 40 | + |
| 41 | + MicrometerMongoCommandListener listener = new MicrometerMongoCommandListener(registry); |
| 42 | + |
| 43 | + @Test void commandStartedShouldNotInstrumentWhenAdminDatabase() { |
| 44 | + listener.commandStarted(new CommandStartedEvent(null, 0, null, "admin", "", null)); |
| 45 | + |
| 46 | + // TODO: Move this to MeterAssert#doesNotHaveMeterWithName or MeterAssert#isEmpty |
| 47 | + assertThat(registry.getMeters()).isEmpty(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test void commandStartedShouldNotInstrumentWhenNoRequestContext() { |
| 51 | + listener.commandStarted(new CommandStartedEvent(null, 0, null, "some name", "", null)); |
| 52 | + |
| 53 | + // TODO: Move this to MeterAssert#doesNotHaveMeterWithName or MeterAssert#isEmpty |
| 54 | + assertThat(registry.getMeters()).isEmpty(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test void commandStartedShouldNotInstrumentWhenNoParentSampleInRequestContext() { |
| 58 | + listener.commandStarted(new CommandStartedEvent(new TestRequestContext(), 0, null, "some name", "", null)); |
| 59 | + |
| 60 | + // TODO: Move this to MeterAssert#doesNotHaveMeterWithName or MeterAssert#isEmpty |
| 61 | + assertThat(registry.getMeters()).isEmpty(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test void successfullyCompletedCommandShouldCreateTimerWhenParentSampleInRequestContext() { |
| 65 | + Timer.Sample parent = Timer.start(registry); |
| 66 | + TestRequestContext testRequestContext = TestRequestContext.withSample(parent); |
| 67 | + |
| 68 | + listener.commandStarted(new CommandStartedEvent(testRequestContext, 0, new ConnectionDescription(new ServerId(new ClusterId("description"), new ServerAddress("localhost", 1234))), "database", "insert", new BsonDocument("collection", new BsonString("user")))); |
| 69 | + listener.commandSucceeded(new CommandSucceededEvent(testRequestContext, 0, null, "insert", null, 0)); |
| 70 | + |
| 71 | + assertThatTimerRegisteredWithTags(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test void successfullyCompletedCommandWithCollectionHavingCommandNameShouldCreateTimerWhenParentSampleInRequestContext() { |
| 75 | + Timer.Sample parent = Timer.start(registry); |
| 76 | + TestRequestContext testRequestContext = TestRequestContext.withSample(parent); |
| 77 | + |
| 78 | + listener.commandStarted(new CommandStartedEvent(testRequestContext, 0, new ConnectionDescription(new ServerId(new ClusterId("description"), new ServerAddress("localhost", 1234))), "database", "aggregate", new BsonDocument("aggregate", new BsonString("user")))); |
| 79 | + listener.commandSucceeded(new CommandSucceededEvent(testRequestContext, 0, null, "aggregate", null, 0)); |
| 80 | + |
| 81 | + assertThatTimerRegisteredWithTags(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test void successfullyCompletedCommandWithoutClusterInformationShouldCreateTimerWhenParentSampleInRequestContext() { |
| 85 | + Timer.Sample parent = Timer.start(registry); |
| 86 | + TestRequestContext testRequestContext = TestRequestContext.withSample(parent); |
| 87 | + |
| 88 | + listener.commandStarted(new CommandStartedEvent(testRequestContext, 0, null, "database", "insert", new BsonDocument("collection", new BsonString("user")))); |
| 89 | + listener.commandSucceeded(new CommandSucceededEvent(testRequestContext, 0, null, "insert", null, 0)); |
| 90 | + |
| 91 | + assertThat(registry) |
| 92 | + .hasTimerWithNameAndTags("mongodb.command", Tags.of(Tag.of("mongodb.collection", "user"))); |
| 93 | + } |
| 94 | + |
| 95 | + @Test void commandWithErrorShouldCreateTimerWhenParentSampleInRequestContext() { |
| 96 | + Timer.Sample parent = Timer.start(registry); |
| 97 | + TestRequestContext testRequestContext = TestRequestContext.withSample(parent); |
| 98 | + |
| 99 | + listener.commandStarted(new CommandStartedEvent(testRequestContext, 0, new ConnectionDescription(new ServerId(new ClusterId("description"), new ServerAddress("localhost", 1234))), "database", "insert", new BsonDocument("collection", new BsonString("user")))); |
| 100 | + listener.commandFailed(new CommandFailedEvent(testRequestContext, 0, null, "insert", 0, new IllegalAccessException())); |
| 101 | + |
| 102 | + assertThatTimerRegisteredWithTags(); |
| 103 | + } |
| 104 | + |
| 105 | + private void assertThatTimerRegisteredWithTags() { |
| 106 | + assertThat(registry) |
| 107 | + .hasTimerWithNameAndTags("mongodb.command", Tags.of(Tag.of("mongodb.collection", "user"))) |
| 108 | + .hasTimerWithNameAndTagKeys("mongodb.command", "mongodb.cluster_id"); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments