Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -5,7 +5,6 @@

package io.opentelemetry.javaagent.instrumentation.powerjob.v4_0;

import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.powerjob.v4_0.PowerJobSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
Expand All @@ -17,6 +16,7 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -45,43 +45,58 @@ public void transform(TypeTransformer transformer) {

public static class ProcessAdvice {

@SuppressWarnings("unused")
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onSchedule(
@Advice.This BasicProcessor handler,
@Advice.Argument(0) TaskContext taskContext,
@Advice.Local("otelRequest") PowerJobProcessRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
request =
PowerJobProcessRequest.createRequest(
taskContext.getJobId(),
handler,
"process",
taskContext.getJobParams(),
taskContext.getInstanceParams());
public static class AdviceScope {
private final PowerJobProcessRequest request;
private final Context context;
private final Scope scope;

private AdviceScope(PowerJobProcessRequest request, Context context, Scope scope) {
this.request = request;
this.context = context;
this.scope = scope;
}

@Nullable
public static AdviceScope start(BasicProcessor handler, TaskContext taskContext) {
Context parentContext = Context.current();
PowerJobProcessRequest request =
PowerJobProcessRequest.createRequest(
taskContext.getJobId(),
handler,
"process",
taskContext.getJobParams(),
taskContext.getInstanceParams());

if (!instrumenter().shouldStart(parentContext, request)) {
return null;
}

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

public void end(ProcessResult result, Throwable throwable) {
scope.close();
instrumenter().end(context, request, result, throwable);
}
}

@SuppressWarnings("unused")
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope onSchedule(
@Advice.This BasicProcessor handler, @Advice.Argument(0) TaskContext taskContext) {
return AdviceScope.start(handler, taskContext);
}

@SuppressWarnings("unused")
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Return ProcessResult result,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") PowerJobProcessRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {
if (adviceScope != null) {
adviceScope.end(result, throwable);
}
scope.close();
instrumenter().end(context, request, result, throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,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.Collections;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class PowerJobInstrumentationModule extends InstrumentationModule {
public class PowerJobInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {
public PowerJobInstrumentationModule() {
super("powerjob", "powerjob-4.0");
}
Expand All @@ -21,4 +23,9 @@ public PowerJobInstrumentationModule() {
public List<TypeInstrumentation> typeInstrumentations() {
return Collections.singletonList(new BasicProcessorInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Loading