-
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.
Added OTel Instrumentation feature for both SimpleScheduler and Quart…
…z Scheduler
- Loading branch information
Showing
11 changed files
with
234 additions
and
14 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
18 changes: 18 additions & 0 deletions
18
...z/runtime/src/main/java/io/quarkus/quartz/runtime/tracing/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.quartz.runtime.tracing; | ||
|
||
import org.quartz.JobExecutionContext; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesGetter; | ||
|
||
public 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
...tz/runtime/src/main/java/io/quarkus/quartz/runtime/tracing/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.quartz.runtime.tracing; | ||
|
||
import org.quartz.SchedulerException; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.ErrorCauseExtractor; | ||
|
||
public 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
...artz/runtime/src/main/java/io/quarkus/quartz/runtime/tracing/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.quartz.runtime.tracing; | ||
|
||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobKey; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
|
||
public final class QuartzSpanNameExtractor implements SpanNameExtractor<JobExecutionContext> { | ||
@Override | ||
public String extract(JobExecutionContext job) { | ||
JobKey key = job.getJobDetail().getKey(); | ||
return key.getGroup() + '.' + key.getName(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/tracing/WithSpanJob.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,53 @@ | ||
package io.quarkus.quartz.runtime.tracing; | ||
|
||
import jakarta.enterprise.inject.spi.CDI; | ||
|
||
import org.quartz.Job; | ||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobExecutionException; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
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 class WithSpanJob implements Job { | ||
private final Job delegate; | ||
|
||
private final Instrumenter<JobExecutionContext, Void> instrumenter; | ||
|
||
public WithSpanJob(Job delegate, boolean captureExperimentalSpanAttributes) { | ||
this.delegate = delegate; | ||
InstrumenterBuilder<JobExecutionContext, Void> jobInstrumenterBuilder = Instrumenter.builder( | ||
CDI.current().select(OpenTelemetry.class).get(), "io.quarkus.opentelemetry", | ||
new QuartzSpanNameExtractor()); | ||
|
||
if (!captureExperimentalSpanAttributes) { | ||
jobInstrumenterBuilder.addAttributesExtractor( | ||
AttributesExtractor.constant(AttributeKey.stringKey("job.system"), "quartz")); | ||
} | ||
jobInstrumenterBuilder.setErrorCauseExtractor(new QuartzErrorCauseExtractor()); | ||
jobInstrumenterBuilder.addAttributesExtractor( | ||
CodeAttributesExtractor.create(new QuartzCodeAttributesGetter())); | ||
this.instrumenter = jobInstrumenterBuilder.buildInstrumenter(); | ||
} | ||
|
||
@Override | ||
public void execute(JobExecutionContext job) throws JobExecutionException { | ||
Context parentCtx = Context.current(); | ||
Context context = instrumenter.start(parentCtx, job); | ||
Throwable t = null; | ||
try (Scope scope = context.makeCurrent()) { | ||
delegate.execute(job); | ||
} catch (Throwable throwable) { | ||
t = throwable; | ||
throw throwable; | ||
} finally { | ||
instrumenter.end(context, job, null, t); | ||
} | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
...s/scheduler/common/src/main/java/io/quarkus/scheduler/common/runtime/WithSpanInvoker.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,50 @@ | ||
package io.quarkus.scheduler.common.runtime; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import jakarta.enterprise.inject.spi.CDI; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.instrumenter.ErrorCauseExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
import io.quarkus.scheduler.ScheduledExecution; | ||
|
||
public class WithSpanInvoker extends DelegateInvoker { | ||
private final Instrumenter<ScheduledExecution, Void> instrumenter; | ||
|
||
public WithSpanInvoker(ScheduledInvoker delegate) { | ||
super(delegate); | ||
InstrumenterBuilder<ScheduledExecution, Void> instrumenterBuilder = Instrumenter.builder( | ||
CDI.current().select(OpenTelemetry.class).get(), "io.quarkus.opentelemetry", | ||
new SpanNameExtractor<ScheduledExecution>() { | ||
@Override | ||
public String extract(ScheduledExecution scheduledExecution) { | ||
return scheduledExecution.getTrigger().getId(); | ||
} | ||
}); | ||
instrumenterBuilder.setErrorCauseExtractor(new ErrorCauseExtractor() { | ||
@Override | ||
public Throwable extract(Throwable throwable) { | ||
return throwable; | ||
} | ||
}); | ||
|
||
this.instrumenter = instrumenterBuilder.buildInstrumenter(); | ||
} | ||
|
||
@Override | ||
public CompletionStage<Void> invoke(ScheduledExecution execution) throws Exception { | ||
Context parentCtx = Context.current(); | ||
Context context = instrumenter.start(parentCtx, execution); | ||
try (Scope scope = context.makeCurrent()) { | ||
return delegate | ||
.invoke(execution) | ||
.whenComplete( | ||
(result, throwable) -> instrumenter.end(context, execution, null, throwable)); | ||
} | ||
} | ||
} |
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.