-
Notifications
You must be signed in to change notification settings - Fork 867
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
Add instrumentation for Quartz 2.0 #4017
Merged
anuraaga
merged 5 commits into
open-telemetry:main
from
anuraaga:quartz-instrumentation
Aug 31, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("org.quartz-scheduler") | ||
module.set("quartz") | ||
versions.set("[2.0.0,)") | ||
assertInverse.set(true) | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(project(":instrumentation:quartz-2.0:library")) | ||
|
||
library("org.quartz-scheduler:quartz:2.0.0") | ||
|
||
testImplementation(project(":instrumentation:quartz-2.0:testing")) | ||
} |
22 changes: 22 additions & 0 deletions
22
.../io/opentelemetry/javaagent/instrumentation/quartz/v2_0/QuartzIgnoredTypesConfigurer.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,22 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.quartz.v2_0; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.instrumentation.api.config.Config; | ||
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesBuilder; | ||
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesConfigurer; | ||
|
||
@AutoService(IgnoredTypesConfigurer.class) | ||
public class QuartzIgnoredTypesConfigurer implements IgnoredTypesConfigurer { | ||
|
||
@Override | ||
public void configure(Config config, IgnoredTypesBuilder builder) { | ||
// Quartz executes jobs themselves in a synchronous way, there's no reason to propagate context | ||
// between its scheduler threads. | ||
builder.ignoreTaskClass("org.quartz"); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...in/java/io/opentelemetry/javaagent/instrumentation/quartz/v2_0/QuartzInstrumentation.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,58 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.quartz.v2_0; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; | ||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import io.opentelemetry.javaagent.instrumentation.api.CallDepth; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.quartz.Scheduler; | ||
|
||
public class QuartzInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderOptimization() { | ||
return hasClassesNamed("org.quartz.Scheduler"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return hasSuperType(named("org.quartz.Scheduler")); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isConstructor(), this.getClass().getName() + "$ConstructorAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ConstructorAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) { | ||
callDepth = CallDepth.forClass(Scheduler.class); | ||
callDepth.getAndIncrement(); | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void addTracingInterceptor( | ||
@Advice.This Scheduler scheduler, @Advice.Local("otelCallDepth") CallDepth callDepth) { | ||
// No-args constructor is automatically called by constructors with args, but we only want to | ||
// run once from the constructor with args because that is where the dedupe needs to happen. | ||
if (callDepth.decrementAndGet() > 0) { | ||
return; | ||
} | ||
QuartzSingletons.TRACING.configure(scheduler); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...a/io/opentelemetry/javaagent/instrumentation/quartz/v2_0/QuartzInstrumentationModule.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,25 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.quartz.v2_0; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class QuartzInstrumentationModule extends InstrumentationModule { | ||
|
||
public QuartzInstrumentationModule() { | ||
super("quartz", "quartz-2.0"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Collections.singletonList(new QuartzInstrumentation()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...rc/main/java/io/opentelemetry/javaagent/instrumentation/quartz/v2_0/QuartzSingletons.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 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.quartz.v2_0; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.instrumentation.quartz.v2_0.QuartzTracing; | ||
|
||
public final class QuartzSingletons { | ||
|
||
public static final QuartzTracing TRACING = QuartzTracing.create(GlobalOpenTelemetry.get()); | ||
|
||
private QuartzSingletons() {} | ||
} |
25 changes: 25 additions & 0 deletions
25
...gent/src/test/java/io/opentelemetry/javaagent/instrumentation/quartz/v2_0/QuartzTest.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,25 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.quartz.v2_0; | ||
|
||
import io.opentelemetry.instrumentation.quartz.v2_0.AbstractQuartzTest; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.quartz.Scheduler; | ||
|
||
class QuartzTest extends AbstractQuartzTest { | ||
|
||
@RegisterExtension InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
@Override | ||
protected void configureScheduler(Scheduler scheduler) {} | ||
|
||
@Override | ||
protected InstrumentationExtension getTesting() { | ||
return testing; | ||
} | ||
} |
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,10 @@ | ||
plugins { | ||
id("otel.library-instrumentation") | ||
id("otel.nullaway-conventions") | ||
} | ||
|
||
dependencies { | ||
library("org.quartz-scheduler:quartz:2.0.0") | ||
|
||
testImplementation(project(":instrumentation:quartz-2.0:testing")) | ||
} |
20 changes: 20 additions & 0 deletions
20
...src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/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,20 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.quartz.v2_0; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.ErrorCauseExtractor; | ||
import org.quartz.SchedulerException; | ||
|
||
final class QuartzErrorCauseExtractor implements ErrorCauseExtractor { | ||
@Override | ||
public Throwable extractCause(Throwable error) { | ||
Throwable userError = error; | ||
while (userError instanceof SchedulerException) { | ||
userError = ((SchedulerException) userError).getUnderlyingException(); | ||
} | ||
return userError; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...y/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/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,18 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.quartz.v2_0; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobKey; | ||
|
||
final class QuartzSpanNameExtractor implements SpanNameExtractor<JobExecutionContext> { | ||
@Override | ||
public String extract(JobExecutionContext job) { | ||
JobKey key = job.getJobDetail().getKey(); | ||
return key.getGroup() + '.' + key.getName(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...2.0/library/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzTracing.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,59 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.quartz.v2_0; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import org.quartz.JobListener; | ||
import org.quartz.Scheduler; | ||
import org.quartz.SchedulerException; | ||
import org.quartz.impl.matchers.EverythingMatcher; | ||
|
||
/** Entrypoint for tracing execution of Quartz jobs. */ | ||
public final class QuartzTracing { | ||
|
||
/** Returns a new {@link QuartzTracing} configured with the given {@link OpenTelemetry}. */ | ||
public static QuartzTracing create(OpenTelemetry openTelemetry) { | ||
return builder(openTelemetry).build(); | ||
} | ||
|
||
/** Returns a new {@link QuartzTracingBuilder} configured with the given {@link OpenTelemetry}. */ | ||
public static QuartzTracingBuilder builder(OpenTelemetry openTelemetry) { | ||
return new QuartzTracingBuilder(openTelemetry); | ||
} | ||
|
||
QuartzTracing(JobListener jobListener) { | ||
this.jobListener = jobListener; | ||
} | ||
|
||
private final JobListener jobListener; | ||
|
||
/** | ||
* Configures the {@link Scheduler} to enable tracing of jobs. | ||
* | ||
* <p><strong>NOTE:</strong> If there are job listeners already registered on the Scheduler that | ||
* may throw exceptions, tracing will be broken. It's important to call this as soon as possible | ||
* to avoid being affected by other bad listeners, or otherwise ensure listeners you register do | ||
* not throw exceptions. | ||
*/ | ||
public void configure(Scheduler scheduler) { | ||
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, EverythingMatcher.allJobs()); | ||
} catch (SchedulerException e) { | ||
throw new IllegalStateException("Could not add JobListener to Scheduler", e); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...rary/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzTracingBuilder.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 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.quartz.v2_0; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.quartz.JobExecutionContext; | ||
|
||
/** A builder of {@link QuartzTracing}. */ | ||
public final class QuartzTracingBuilder { | ||
|
||
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.quartz-1.7"; | ||
|
||
private final OpenTelemetry openTelemetry; | ||
|
||
private final List<AttributesExtractor<? super JobExecutionContext, ? super Void>> | ||
additionalExtractors = new ArrayList<>(); | ||
|
||
QuartzTracingBuilder(OpenTelemetry openTelemetry) { | ||
this.openTelemetry = openTelemetry; | ||
} | ||
|
||
/** | ||
* Adds an additional {@link AttributesExtractor} to invoke to set attributes to instrumented | ||
* items. The {@link AttributesExtractor} will be executed after all default extractors. | ||
*/ | ||
public QuartzTracingBuilder addAttributeExtractor( | ||
AttributesExtractor<? super JobExecutionContext, ? super Void> attributesExtractor) { | ||
additionalExtractors.add(attributesExtractor); | ||
return this; | ||
} | ||
|
||
/** Returns a new {@link QuartzTracing} with the settings of this {@link QuartzTracingBuilder}. */ | ||
public QuartzTracing build() { | ||
InstrumenterBuilder<JobExecutionContext, Void> instrumenter = | ||
Instrumenter.newBuilder(openTelemetry, INSTRUMENTATION_NAME, new QuartzSpanNameExtractor()); | ||
|
||
instrumenter.setErrorCauseExtractor(new QuartzErrorCauseExtractor()); | ||
instrumenter.addAttributesExtractors(additionalExtractors); | ||
|
||
return new QuartzTracing(new TracingJobListener(instrumenter.newInstrumenter())); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that unlike something like AWS SDK instrumentation, where a bad executioninterceptor doesn't kill the entire SDK experience, a job listener that throws in the begin listener will cause the job itself to not run. Presumably quartz users or implementations of quartz listeners do know that they cannot throw exceptions or have the scheduler completely broken, not just tracing.