From 8f29dfc8f9225c692039dc74d09bc3ef7d6b2eaf Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Sat, 9 Oct 2021 09:54:38 -0700 Subject: [PATCH] Add code attributes to quartz spans (#4332) * Add code attributes to quartz spans * Use constants! --- .../v2_0/QuartzCodeAttributesExtractor.java | 35 +++++++++++++++++++ .../quartz/v2_0/QuartzTracingBuilder.java | 1 + .../quartz/v2_0/AbstractQuartzTest.java | 21 +++++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 instrumentation/quartz-2.0/library/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzCodeAttributesExtractor.java diff --git a/instrumentation/quartz-2.0/library/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzCodeAttributesExtractor.java b/instrumentation/quartz-2.0/library/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzCodeAttributesExtractor.java new file mode 100644 index 000000000000..d2c5c3997965 --- /dev/null +++ b/instrumentation/quartz-2.0/library/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzCodeAttributesExtractor.java @@ -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 { + @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; + } +} diff --git a/instrumentation/quartz-2.0/library/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzTracingBuilder.java b/instrumentation/quartz-2.0/library/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzTracingBuilder.java index e0a86d9e49da..27e218e1ef6c 100644 --- a/instrumentation/quartz-2.0/library/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzTracingBuilder.java +++ b/instrumentation/quartz-2.0/library/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/QuartzTracingBuilder.java @@ -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())); diff --git a/instrumentation/quartz-2.0/testing/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/AbstractQuartzTest.java b/instrumentation/quartz-2.0/testing/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/AbstractQuartzTest.java index b00040c4c9a3..0dc2c2520118 100644 --- a/instrumentation/quartz-2.0/testing/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/AbstractQuartzTest.java +++ b/instrumentation/quartz-2.0/testing/src/main/java/io/opentelemetry/instrumentation/quartz/v2_0/AbstractQuartzTest.java @@ -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; @@ -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) @@ -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 {