Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class KafkaConnectInstrumentationModule extends InstrumentationModule {
public class KafkaConnectInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public KafkaConnectInstrumentationModule() {
super("kafka-connect", "kafka-connect-2.6");
Expand All @@ -31,4 +33,9 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
// class added in 2.6.0
return hasClassesNamed("org.apache.kafka.connect.sink.SinkConnectorContext");
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.Collection;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand All @@ -39,36 +39,49 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class SinkTaskPutAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) Collection<SinkRecord> records,
@Advice.Local("otelTask") KafkaConnectTask task,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
public static class AdviceScope {
private final KafkaConnectTask task;
private final Context context;
private final Scope scope;

private AdviceScope(KafkaConnectTask task, Context context, Scope scope) {
this.task = task;
this.context = context;
this.scope = scope;
}

@Nullable
public static AdviceScope start(Collection<SinkRecord> records) {
Context parentContext = Context.current();

Context parentContext = Java8BytecodeBridge.currentContext();
KafkaConnectTask task = new KafkaConnectTask(records);
if (!instrumenter().shouldStart(parentContext, task)) {
return null;
}

task = new KafkaConnectTask(records);
if (!instrumenter().shouldStart(parentContext, task)) {
return;
Context context = instrumenter().start(parentContext, task);
return new AdviceScope(task, context, context.makeCurrent());
}

context = instrumenter().start(parentContext, task);
scope = context.makeCurrent();
public void end(Throwable throwable) {
scope.close();
instrumenter().end(context, task, null, throwable);
}
}

@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope onEnter(@Advice.Argument(0) Collection<SinkRecord> records) {
return AdviceScope.start(records);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelTask") KafkaConnectTask task,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {

if (scope == null) {
return;
if (adviceScope != null) {
adviceScope.end(throwable);
}
scope.close();
instrumenter().end(context, task, null, throwable);
}
}
}
Loading