Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization fo quartz JobExecutionContext #5263

Merged
merged 2 commits into from
Jan 28, 2022
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 @@ -7,13 +7,17 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.field.VirtualField;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

final class TracingJobListener implements JobListener {

private static final VirtualField<JobExecutionContext, ContextAndScope> contextVirtualField =
VirtualField.find(JobExecutionContext.class, ContextAndScope.class);

private final Instrumenter<JobExecutionContext, Void> instrumenter;

TracingJobListener(Instrumenter<JobExecutionContext, Void> instrumenter) {
Expand All @@ -38,31 +42,44 @@ public void jobToBeExecuted(JobExecutionContext job) {
}

Context context = instrumenter.start(parentCtx, job);
job.put(Context.class, context);

// Listeners are executed synchronously on the same thread starting here.
// https://github.com/quartz-scheduler/quartz/blob/quartz-2.0.x/quartz/src/main/java/org/quartz/core/JobRunShell.java#L180
// However, if a listener before this one throws an exception in wasExecuted, we won't be
// executed. Library instrumentation users need to make sure other listeners don't throw
// exceptions.
Scope scope = context.makeCurrent();
job.put(Scope.class, scope);
contextVirtualField.set(job, new ContextAndScope(context, scope));
}

@Override
public void jobWasExecuted(JobExecutionContext job, JobExecutionException error) {
Scope scope = (Scope) job.get(Scope.class);
if (scope != null) {
scope.close();
}

Context context = (Context) job.get(Context.class);
if (context == null) {
ContextAndScope contextAndScope = contextVirtualField.get(job);
if (contextAndScope == null) {
// Would only happen if we didn't start a span (maybe a previous joblistener threw an
// exception before ours could process the start event).
return;
}

instrumenter.end(context, job, null, error);
contextAndScope.closeScope();
instrumenter.end(contextAndScope.getContext(), job, null, error);
}

private static final class ContextAndScope {
private final Context context;
private final Scope scope;

ContextAndScope(Context context, Scope scope) {
this.context = context;
this.scope = scope;
}

Context getContext() {
return context;
}

void closeScope() {
scope.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Properties;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -119,6 +122,12 @@ public static class SuccessfulJob implements Job {
@Override
public void execute(JobExecutionContext context) {
GlobalOpenTelemetry.getTracer("jobtracer").spanBuilder("child").startSpan().end();
// ensure that JobExecutionContext is serializable
try {
new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(context);
Comment on lines +125 to +127
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}

Expand Down