committed by
Christoph Strobl
parent
838ddb5d26
commit
1fe1c13531
@@ -26,12 +26,12 @@ import com.mongodb.connection.ConnectionId;
|
||||
import com.mongodb.event.CommandStartedEvent;
|
||||
|
||||
/**
|
||||
* Default {@link MongoHandlerKeyValuesProvider} implementation.
|
||||
* Default {@link MongoHandlerObservationConvention} implementation.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public class DefaultMongoHandlerKeyValuesProvider implements MongoHandlerKeyValuesProvider {
|
||||
public class DefaultMongoHandlerObservationConvention implements MongoHandlerObservationConvention {
|
||||
|
||||
@Override
|
||||
public KeyValues getLowCardinalityKeyValues(MongoHandlerContext context) {
|
||||
@@ -40,7 +40,7 @@ public class DefaultMongoHandlerKeyValuesProvider implements MongoHandlerKeyValu
|
||||
|
||||
if (context.getCollectionName() != null) {
|
||||
keyValues = keyValues
|
||||
.and(KeyValue.of(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.getKeyName(), context.getCollectionName()));
|
||||
.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(context.getCollectionName()));
|
||||
}
|
||||
|
||||
KeyValue connectionTag = connectionTag(context.getCommandStartedEvent());
|
||||
@@ -54,8 +54,8 @@ public class DefaultMongoHandlerKeyValuesProvider implements MongoHandlerKeyValu
|
||||
@Override
|
||||
public KeyValues getHighCardinalityKeyValues(MongoHandlerContext context) {
|
||||
|
||||
return KeyValues.of(KeyValue.of(HighCardinalityCommandKeyNames.MONGODB_COMMAND.getKeyName(),
|
||||
context.getCommandStartedEvent().getCommandName()));
|
||||
return KeyValues.of(
|
||||
HighCardinalityCommandKeyNames.MONGODB_COMMAND.withValue(context.getCommandStartedEvent().getCommandName()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,8 +72,8 @@ public class DefaultMongoHandlerKeyValuesProvider implements MongoHandlerKeyValu
|
||||
|
||||
ConnectionId connectionId = connectionDescription.getConnectionId();
|
||||
if (connectionId != null) {
|
||||
return KeyValue.of(LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID.getKeyName(),
|
||||
connectionId.getServerId().getClusterId().getValue());
|
||||
return LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID
|
||||
.withValue(connectionId.getServerId().getClusterId().getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,12 @@ package org.springframework.data.mongodb.observability;
|
||||
import io.micrometer.observation.Observation;
|
||||
|
||||
/**
|
||||
* {@link Observation.KeyValuesProvider} for {@link MongoHandlerContext}.
|
||||
* {@link Observation.ObservationConvention} for {@link MongoHandlerContext}.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public interface MongoHandlerKeyValuesProvider extends Observation.KeyValuesProvider<MongoHandlerContext> {
|
||||
public interface MongoHandlerObservationConvention extends Observation.ObservationConvention<MongoHandlerContext> {
|
||||
|
||||
@Override
|
||||
default boolean supportsContext(Observation.Context context) {
|
||||
@@ -63,7 +63,7 @@ enum MongoObservation implements DocumentedObservation {
|
||||
*/
|
||||
MONGODB_COLLECTION {
|
||||
@Override
|
||||
public String getKeyName() {
|
||||
public String asString() {
|
||||
return "spring.data.mongodb.collection";
|
||||
}
|
||||
},
|
||||
@@ -73,7 +73,7 @@ enum MongoObservation implements DocumentedObservation {
|
||||
*/
|
||||
MONGODB_CLUSTER_ID {
|
||||
@Override
|
||||
public String getKeyName() {
|
||||
public String asString() {
|
||||
return "spring.data.mongodb.cluster_id";
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ enum MongoObservation implements DocumentedObservation {
|
||||
*/
|
||||
MONGODB_COMMAND {
|
||||
@Override
|
||||
public String getKeyName() {
|
||||
public String asString() {
|
||||
return "spring.data.mongodb.command";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,19 +36,18 @@ import com.mongodb.event.CommandSucceededEvent;
|
||||
* @author Greg Turnquist
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public final class MongoObservationCommandListener
|
||||
implements CommandListener, Observation.KeyValuesProviderAware<MongoHandlerKeyValuesProvider> {
|
||||
public final class MongoObservationCommandListener implements CommandListener {
|
||||
|
||||
private static final Log log = LogFactory.getLog(MongoObservationCommandListener.class);
|
||||
|
||||
private final ObservationRegistry observationRegistry;
|
||||
|
||||
private MongoHandlerKeyValuesProvider keyValuesProvider;
|
||||
private MongoHandlerObservationConvention observationConvention;
|
||||
|
||||
public MongoObservationCommandListener(ObservationRegistry observationRegistry) {
|
||||
|
||||
this.observationRegistry = observationRegistry;
|
||||
this.keyValuesProvider = new DefaultMongoHandlerKeyValuesProvider();
|
||||
this.observationConvention = new DefaultMongoHandlerObservationConvention();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -130,7 +129,7 @@ public final class MongoObservationCommandListener
|
||||
|
||||
/**
|
||||
* Extract the {@link Observation} from MongoDB's {@link RequestContext}.
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
@@ -160,7 +159,7 @@ public final class MongoObservationCommandListener
|
||||
Observation observation = MongoObservation.MONGODB_COMMAND_OBSERVATION
|
||||
.observation(this.observationRegistry, observationContext) //
|
||||
.contextualName(observationContext.getContextualName()) //
|
||||
.keyValuesProvider(this.keyValuesProvider) //
|
||||
.observationConvention(this.observationConvention) //
|
||||
.start();
|
||||
|
||||
requestContext.put(Observation.class, observation);
|
||||
@@ -171,9 +170,4 @@ public final class MongoObservationCommandListener
|
||||
"Created a child observation [" + observation + "] for mongo instrumentation and put it in mongo context");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setKeyValuesProvider(MongoHandlerKeyValuesProvider mongoHandlerKeyValuesProvider) {
|
||||
this.keyValuesProvider = mongoHandlerKeyValuesProvider;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package org.springframework.data.mongodb.observability;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.observation.TimerObservationHandler;
|
||||
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import io.micrometer.observation.Observation;
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
@@ -67,7 +67,7 @@ class MongoObservationCommandListenerForTracingTests {
|
||||
|
||||
this.meterRegistry = new SimpleMeterRegistry();
|
||||
this.observationRegistry = ObservationRegistry.create();
|
||||
this.observationRegistry.observationConfig().observationHandler(new TimerObservationHandler(meterRegistry));
|
||||
this.observationRegistry.observationConfig().observationHandler(new DefaultMeterObservationHandler(meterRegistry));
|
||||
this.observationRegistry.observationConfig().observationHandler(handler);
|
||||
|
||||
this.listener = new MongoObservationCommandListener(observationRegistry);
|
||||
@@ -135,7 +135,7 @@ class MongoObservationCommandListenerForTracingTests {
|
||||
* Execute MongoDB's {@link com.mongodb.event.CommandListener#commandStarted(CommandStartedEvent)} and
|
||||
* {@link com.mongodb.event.CommandListener#commandSucceeded(CommandSucceededEvent)} operations against the
|
||||
* {@link TestRequestContext} in order to inject some test data.
|
||||
*
|
||||
*
|
||||
* @param testRequestContext
|
||||
*/
|
||||
private void commandStartedAndSucceeded(TestRequestContext testRequestContext) {
|
||||
@@ -163,8 +163,8 @@ class MongoObservationCommandListenerForTracingTests {
|
||||
.hasNameEqualTo("insert user") //
|
||||
.hasKindEqualTo(Span.Kind.CLIENT) //
|
||||
.hasRemoteServiceNameEqualTo("mongodb-database") //
|
||||
.hasTag(HighCardinalityCommandKeyNames.MONGODB_COMMAND.getKeyName(), "insert") //
|
||||
.hasTag(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.getKeyName(), "user") //
|
||||
.hasTagWithKey(LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID.getKeyName());
|
||||
.hasTag(HighCardinalityCommandKeyNames.MONGODB_COMMAND.asString(), "insert") //
|
||||
.hasTag(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.asString(), "user") //
|
||||
.hasTagWithKey(LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID.asString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.observability;
|
||||
|
||||
import static io.micrometer.core.tck.MeterRegistryAssert.*;
|
||||
import static io.micrometer.core.tck.MeterRegistryAssert.assertThat;
|
||||
|
||||
import io.micrometer.common.KeyValue;
|
||||
import io.micrometer.common.KeyValues;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.observation.TimerObservationHandler;
|
||||
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import io.micrometer.observation.Observation;
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
@@ -42,7 +41,7 @@ import com.mongodb.event.CommandSucceededEvent;
|
||||
|
||||
/**
|
||||
* Series of test cases exercising {@link MongoObservationCommandListener}.
|
||||
*
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @author Greg Turnquist
|
||||
* @since 4.0.0
|
||||
@@ -59,7 +58,7 @@ class MongoObservationCommandListenerTests {
|
||||
|
||||
this.meterRegistry = new SimpleMeterRegistry();
|
||||
this.observationRegistry = ObservationRegistry.create();
|
||||
this.observationRegistry.observationConfig().observationHandler(new TimerObservationHandler(meterRegistry));
|
||||
this.observationRegistry.observationConfig().observationHandler(new DefaultMeterObservationHandler(meterRegistry));
|
||||
|
||||
this.listener = new MongoObservationCommandListener(observationRegistry);
|
||||
}
|
||||
@@ -149,8 +148,8 @@ class MongoObservationCommandListenerTests {
|
||||
listener.commandSucceeded(new CommandSucceededEvent(testRequestContext, 0, null, "insert", null, 0));
|
||||
|
||||
// then
|
||||
assertThat(meterRegistry).hasTimerWithNameAndTags(HighCardinalityCommandKeyNames.MONGODB_COMMAND.getKeyName(),
|
||||
KeyValues.of(KeyValue.of(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.getKeyName(), "user")));
|
||||
assertThat(meterRegistry).hasTimerWithNameAndTags(HighCardinalityCommandKeyNames.MONGODB_COMMAND.asString(),
|
||||
KeyValues.of(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue("user")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -178,10 +177,10 @@ class MongoObservationCommandListenerTests {
|
||||
private void assertThatTimerRegisteredWithTags() {
|
||||
|
||||
assertThat(meterRegistry) //
|
||||
.hasTimerWithNameAndTags(HighCardinalityCommandKeyNames.MONGODB_COMMAND.getKeyName(),
|
||||
KeyValues.of(KeyValue.of(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.getKeyName(), "user"))) //
|
||||
.hasTimerWithNameAndTagKeys(HighCardinalityCommandKeyNames.MONGODB_COMMAND.getKeyName(),
|
||||
LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID.getKeyName());
|
||||
.hasTimerWithNameAndTags(HighCardinalityCommandKeyNames.MONGODB_COMMAND.asString(),
|
||||
KeyValues.of(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue("user"))) //
|
||||
.hasTimerWithNameAndTagKeys(HighCardinalityCommandKeyNames.MONGODB_COMMAND.asString(),
|
||||
LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID.asString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,10 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.observability;
|
||||
|
||||
import static org.springframework.data.mongodb.test.util.Assertions.*;
|
||||
import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.observation.TimerObservationHandler;
|
||||
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import io.micrometer.observation.Observation;
|
||||
import io.micrometer.observation.ObservationHandler;
|
||||
@@ -80,7 +80,7 @@ public class ZipkinIntegrationTests extends SampleTestRunner {
|
||||
private static final ObservationRegistry OBSERVATION_REGISTRY = ObservationRegistry.create();
|
||||
|
||||
static {
|
||||
OBSERVATION_REGISTRY.observationConfig().observationHandler(new TimerObservationHandler(METER_REGISTRY));
|
||||
OBSERVATION_REGISTRY.observationConfig().observationHandler(new DefaultMeterObservationHandler(METER_REGISTRY));
|
||||
}
|
||||
|
||||
@Autowired PersonRepository repository;
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
[[observability]]
|
||||
== Observability metadata
|
||||
|
||||
include::{root-target}_conventions.adoc[]
|
||||
|
||||
include::{root-target}_metrics.adoc[]
|
||||
|
||||
include::{root-target}_spans.adoc[]
|
||||
|
||||
Reference in New Issue
Block a user