From 4ef031b85534c1091a10bdfddf5ab434ea0b754a Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 Oct 2021 12:55:52 -0700 Subject: [PATCH 1/2] Add code attributes to quartz spans --- .../v2_0/QuartzCodeAttributesExtractor.java | 35 +++++++++++++++++++ .../quartz/v2_0/QuartzTracingBuilder.java | 1 + .../quartz/v2_0/AbstractQuartzTest.java | 15 ++++++-- 3 files changed, 48 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..72d1fbc47b9b 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,11 +5,11 @@ 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; @@ -63,7 +63,12 @@ void successfulJob() throws Exception { .hasKind(SpanKind.INTERNAL) .hasNoParent() .hasStatus(StatusData.unset()) - .hasAttributes(Attributes.empty()), + .hasAttributesSatisfying( + attrs -> + assertThat(attrs) + .containsEntry( + "code.namespace", SuccessfulJob.class.getName()) + .containsEntry("code.function", "execute")), span -> span.hasName("child") .hasKind(SpanKind.INTERNAL) @@ -88,7 +93,11 @@ void failingJob() throws Exception { .hasNoParent() .hasStatus(StatusData.error()) .hasException(new IllegalStateException("Bad job")) - .hasAttributes(Attributes.empty()))); + .hasAttributesSatisfying( + attrs -> + assertThat(attrs) + .containsEntry("code.namespace", FailingJob.class.getName()) + .containsEntry("code.function", "execute")))); } private static Scheduler createScheduler(String name) throws Exception { From 3025926c49c79bbc18db0f0c4e924292cff606cf Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 Oct 2021 13:37:34 -0700 Subject: [PATCH 2/2] Use constants! --- .../quartz/v2_0/AbstractQuartzTest.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 72d1fbc47b9b..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 @@ -13,6 +13,7 @@ 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; @@ -67,8 +68,10 @@ void successfulJob() throws Exception { attrs -> assertThat(attrs) .containsEntry( - "code.namespace", SuccessfulJob.class.getName()) - .containsEntry("code.function", "execute")), + SemanticAttributes.CODE_NAMESPACE, + SuccessfulJob.class.getName()) + .containsEntry( + SemanticAttributes.CODE_FUNCTION, "execute")), span -> span.hasName("child") .hasKind(SpanKind.INTERNAL) @@ -96,8 +99,11 @@ void failingJob() throws Exception { .hasAttributesSatisfying( attrs -> assertThat(attrs) - .containsEntry("code.namespace", FailingJob.class.getName()) - .containsEntry("code.function", "execute")))); + .containsEntry( + SemanticAttributes.CODE_NAMESPACE, + FailingJob.class.getName()) + .containsEntry( + SemanticAttributes.CODE_FUNCTION, "execute")))); } private static Scheduler createScheduler(String name) throws Exception {