
io.opentelemetry.javaagent.instrumentation.mongo.v3_7.MongoClientSettingsBuilderInstrumentation Maven / Gradle / Ivy
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.mongo.v3_7;
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import com.mongodb.MongoClientSettings;
import com.mongodb.event.CommandListener;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.List;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
final class MongoClientSettingsBuilderInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher typeMatcher() {
return named("com.mongodb.MongoClientSettings$Builder")
.and(
declaresMethod(
named("addCommandListener")
.and(isPublic())
.and(
takesArguments(1)
.and(takesArgument(0, named("com.mongodb.event.CommandListener"))))));
}
@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod().and(isPublic()).and(named("build")).and(takesArguments(0)),
this.getClass().getName() + "$AddCommandListenerAdvice");
}
@SuppressWarnings("unused")
public static class AddCommandListenerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void injectTraceListener(
@Advice.This MongoClientSettings.Builder builder,
@Advice.FieldValue("commandListeners") List commandListeners) {
for (CommandListener commandListener : commandListeners) {
if (MongoInstrumentationSingletons.isTracingListener(commandListener)) {
return;
}
}
builder.addCommandListener(MongoInstrumentationSingletons.LISTENER);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy