Skip to content

Commit

Permalink
Add code attributes to quartz spans (#4332)
Browse files Browse the repository at this point in the history
* Add code attributes to quartz spans

* Use constants!
  • Loading branch information
trask authored Oct 9, 2021
1 parent 344c74a commit 8f29dfc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.quartz.v2_0;

import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesExtractor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.quartz.JobExecutionContext;

public class QuartzCodeAttributesExtractor
extends CodeAttributesExtractor<JobExecutionContext, Void> {
@Override
protected Class<?> codeClass(JobExecutionContext jobExecutionContext) {
return jobExecutionContext.getJobDetail().getJobClass();
}

@Override
protected String methodName(JobExecutionContext jobExecutionContext) {
return "execute";
}

@Override
@Nullable
protected String filePath(JobExecutionContext jobExecutionContext) {
return null;
}

@Override
@Nullable
protected Long lineNumber(JobExecutionContext jobExecutionContext) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public QuartzTracing build() {
Instrumenter.newBuilder(openTelemetry, INSTRUMENTATION_NAME, new QuartzSpanNameExtractor());

instrumenter.setErrorCauseExtractor(new QuartzErrorCauseExtractor());
instrumenter.addAttributesExtractor(new QuartzCodeAttributesExtractor());
instrumenter.addAttributesExtractors(additionalExtractors);

return new QuartzTracing(new TracingJobListener(instrumenter.newInstrumenter()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

package io.opentelemetry.instrumentation.quartz.v2_0;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.Properties;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -63,7 +64,14 @@ void successfulJob() throws Exception {
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasStatus(StatusData.unset())
.hasAttributes(Attributes.empty()),
.hasAttributesSatisfying(
attrs ->
assertThat(attrs)
.containsEntry(
SemanticAttributes.CODE_NAMESPACE,
SuccessfulJob.class.getName())
.containsEntry(
SemanticAttributes.CODE_FUNCTION, "execute")),
span ->
span.hasName("child")
.hasKind(SpanKind.INTERNAL)
Expand All @@ -88,7 +96,14 @@ void failingJob() throws Exception {
.hasNoParent()
.hasStatus(StatusData.error())
.hasException(new IllegalStateException("Bad job"))
.hasAttributes(Attributes.empty())));
.hasAttributesSatisfying(
attrs ->
assertThat(attrs)
.containsEntry(
SemanticAttributes.CODE_NAMESPACE,
FailingJob.class.getName())
.containsEntry(
SemanticAttributes.CODE_FUNCTION, "execute"))));
}

private static Scheduler createScheduler(String name) throws Exception {
Expand Down

0 comments on commit 8f29dfc

Please sign in to comment.