-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Added OTel Instrumentation feature for scheduling (quartz)
- Loading branch information
Showing
24 changed files
with
493 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...arkus/opentelemetry/runtime/tracing/intrumentation/quartz/QuartzCodeAttributesGetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.quarkus.opentelemetry.runtime.tracing.intrumentation.quartz; | ||
|
||
import org.quartz.JobExecutionContext; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesGetter; | ||
|
||
final class QuartzCodeAttributesGetter implements CodeAttributesGetter<JobExecutionContext> { | ||
|
||
@Override | ||
public Class<?> getCodeClass(JobExecutionContext jobExecutionContext) { | ||
return jobExecutionContext.getJobDetail().getJobClass(); | ||
} | ||
|
||
@Override | ||
public String getMethodName(JobExecutionContext jobExecutionContext) { | ||
return "execute"; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...uarkus/opentelemetry/runtime/tracing/intrumentation/quartz/QuartzErrorCauseExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.quarkus.opentelemetry.runtime.tracing.intrumentation.quartz; | ||
|
||
import org.quartz.SchedulerException; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.ErrorCauseExtractor; | ||
|
||
final class QuartzErrorCauseExtractor implements ErrorCauseExtractor { | ||
@Override | ||
public Throwable extract(Throwable error) { | ||
Throwable userError = error; | ||
while (userError instanceof SchedulerException) { | ||
userError = ((SchedulerException) userError).getUnderlyingException(); | ||
} | ||
return userError; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../quarkus/opentelemetry/runtime/tracing/intrumentation/quartz/QuartzSpanNameExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.quarkus.opentelemetry.runtime.tracing.intrumentation.quartz; | ||
|
||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobKey; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
|
||
final class QuartzSpanNameExtractor implements SpanNameExtractor<JobExecutionContext> { | ||
@Override | ||
public String extract(JobExecutionContext job) { | ||
JobKey key = job.getJobDetail().getKey(); | ||
return key.getGroup() + '.' + key.getName(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
.../java/io/quarkus/opentelemetry/runtime/tracing/intrumentation/quartz/QuartzTelemetry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.quarkus.opentelemetry.runtime.tracing.intrumentation.quartz; | ||
|
||
import static io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig.INSTRUMENTATION_NAME; | ||
import static org.quartz.impl.matchers.EverythingMatcher.allJobs; | ||
|
||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobListener; | ||
import org.quartz.Scheduler; | ||
import org.quartz.SchedulerException; | ||
import org.quartz.Trigger; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; | ||
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesExtractor; | ||
|
||
public final class QuartzTelemetry { | ||
|
||
public QuartzTelemetry(OpenTelemetry openTelemetry, Scheduler scheduler, boolean captureExperimentalSpanAttributes) { | ||
InstrumenterBuilder<JobExecutionContext, Void> instrumenter = Instrumenter.builder(openTelemetry, INSTRUMENTATION_NAME, | ||
new QuartzSpanNameExtractor()); | ||
|
||
// TODO: make property for experimental data, by default ON | ||
if (captureExperimentalSpanAttributes) { | ||
instrumenter.addAttributesExtractor( | ||
AttributesExtractor.constant(AttributeKey.stringKey("job.system"), "quartz")); | ||
} | ||
instrumenter.setErrorCauseExtractor(new QuartzErrorCauseExtractor()); | ||
instrumenter.addAttributesExtractor( | ||
CodeAttributesExtractor.create(new QuartzCodeAttributesGetter())); | ||
|
||
configure(scheduler, new TracingJobListener(instrumenter.buildInstrumenter())); | ||
} | ||
|
||
private void configure(Scheduler scheduler, JobListener jobListener) { | ||
Trigger x = null; | ||
try { | ||
for (JobListener listener : scheduler.getListenerManager().getJobListeners()) { | ||
if (listener instanceof TracingJobListener) { | ||
return; | ||
} | ||
} | ||
} catch (SchedulerException e) { | ||
// Ignore | ||
} | ||
try { | ||
// We must pass a matcher to work around a bug in Quartz 2.0.0. It's unlikely anyone uses | ||
// a version before 2.0.2, but it makes muzzle simple. | ||
scheduler.getListenerManager().addJobListener(jobListener, allJobs()); | ||
} catch (SchedulerException e) { | ||
throw new IllegalStateException("Could not add JobListener to Scheduler", e); | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...va/io/quarkus/opentelemetry/runtime/tracing/intrumentation/quartz/TracingJobListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package io.quarkus.opentelemetry.runtime.tracing.intrumentation.quartz; | ||
|
||
import jakarta.enterprise.inject.spi.CDI; | ||
|
||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobExecutionException; | ||
import org.quartz.JobListener; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.util.VirtualField; | ||
import io.quarkus.quartz.QuartzScheduler; | ||
|
||
public 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) { | ||
this.instrumenter = instrumenter; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return TracingJobListener.class.getName(); | ||
} | ||
|
||
@Override | ||
public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) { | ||
// TODO: Consider adding an attribute for vetoed jobs. | ||
} | ||
|
||
@Override | ||
public void jobToBeExecuted(JobExecutionContext job) { | ||
// if (!getScheduler().isTriggerProgrammatic(job.getTrigger().getKey())) { | ||
// return; | ||
// } | ||
System.err.println("START: " + System.nanoTime()); | ||
|
||
Context parentCtx = Context.current(); | ||
if (!instrumenter.shouldStart(parentCtx, job)) { | ||
return; | ||
} | ||
|
||
Context context = instrumenter.start(parentCtx, job); | ||
|
||
// 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(); | ||
contextVirtualField.set(job, new ContextAndScope(context, scope)); | ||
} | ||
|
||
@Override | ||
public void jobWasExecuted(JobExecutionContext job, JobExecutionException error) { | ||
// if (!getScheduler().isTriggerProgrammatic(job.getTrigger().getKey())) { | ||
// System.err.println("ANOTACE"); | ||
// return; | ||
// } | ||
System.err.println("END: " + System.nanoTime()); | ||
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; | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
|
||
private QuartzScheduler getScheduler() { | ||
return CDI.current().select(QuartzScheduler.class).get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.