Skip to content

Commit

Permalink
Add experimental span attribute job.system (#6586)
Browse files Browse the repository at this point in the history
* Add experimental span attribute job.system

* spring-batch
  • Loading branch information
trask committed Sep 13, 2022
1 parent 9081ced commit e8e573c
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 26 deletions.
5 changes: 5 additions & 0 deletions instrumentation/quartz-2.0/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ dependencies {

testImplementation(project(":instrumentation:quartz-2.0:testing"))
}

tasks.withType<Test>().configureEach {
// TODO run tests both with and without experimental span attributes
jvmArgs("-Dotel.instrumentation.quartz.experimental-span-attributes=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
package io.opentelemetry.javaagent.instrumentation.quartz.v2_0;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import io.opentelemetry.instrumentation.quartz.v2_0.QuartzTelemetry;

public final class QuartzSingletons {

public static final QuartzTelemetry TELEMETRY = QuartzTelemetry.create(GlobalOpenTelemetry.get());
public static final QuartzTelemetry TELEMETRY =
QuartzTelemetry.builder(GlobalOpenTelemetry.get())
.setCaptureExperimentalSpanAttributes(
ConfigPropertiesUtil.getBoolean(
"otel.instrumentation.quartz.experimental-span-attributes", false))
.build();

private QuartzSingletons() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.quartz.v2_0;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
Expand All @@ -24,6 +25,8 @@ public final class QuartzTelemetryBuilder {
private final List<AttributesExtractor<? super JobExecutionContext, ? super Void>>
additionalExtractors = new ArrayList<>();

private boolean captureExperimentalSpanAttributes;

QuartzTelemetryBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}
Expand All @@ -38,13 +41,28 @@ public QuartzTelemetryBuilder addAttributeExtractor(
return this;
}

/**
* Sets whether experimental attributes should be set to spans. These attributes may be changed or
* removed in the future, so only enable this if you know you do not require attributes filled by
* this instrumentation to be stable across versions
*/
public QuartzTelemetryBuilder setCaptureExperimentalSpanAttributes(
boolean captureExperimentalSpanAttributes) {
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes;
return this;
}

/**
* Returns a new {@link QuartzTelemetry} with the settings of this {@link QuartzTelemetryBuilder}.
*/
public QuartzTelemetry build() {
InstrumenterBuilder<JobExecutionContext, Void> instrumenter =
Instrumenter.builder(openTelemetry, INSTRUMENTATION_NAME, new QuartzSpanNameExtractor());

if (captureExperimentalSpanAttributes) {
instrumenter.addAttributesExtractor(
AttributesExtractor.constant(AttributeKey.stringKey("job.system"), "quartz"));
}
instrumenter.setErrorCauseExtractor(new QuartzErrorCauseExtractor());
instrumenter.addAttributesExtractor(
CodeAttributesExtractor.create(new QuartzCodeAttributesGetter()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ class QuartzTest extends AbstractQuartzTest {

@Override
protected void configureScheduler(Scheduler scheduler) {
QuartzTelemetry.create(testing.getOpenTelemetry()).configure(scheduler);
QuartzTelemetry.builder(testing.getOpenTelemetry())
// TODO run tests both with and without experimental span attributes
.setCaptureExperimentalSpanAttributes(true)
.build()
.configure(scheduler);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

package io.opentelemetry.instrumentation.quartz.v2_0;

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

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.trace.data.StatusData;
Expand Down Expand Up @@ -67,14 +68,12 @@ void successfulJob() throws Exception {
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasStatus(StatusData.unset())
.hasAttributesSatisfying(
attrs ->
assertThat(attrs)
.containsEntry(
SemanticAttributes.CODE_NAMESPACE,
SuccessfulJob.class.getName())
.containsEntry(
SemanticAttributes.CODE_FUNCTION, "execute")),
.hasAttributesSatisfyingExactly(
equalTo(AttributeKey.stringKey("job.system"), "quartz"),
equalTo(
SemanticAttributes.CODE_NAMESPACE,
SuccessfulJob.class.getName()),
equalTo(SemanticAttributes.CODE_FUNCTION, "execute")),
span ->
span.hasName("child")
.hasKind(SpanKind.INTERNAL)
Expand All @@ -99,14 +98,11 @@ void failingJob() throws Exception {
.hasNoParent()
.hasStatus(StatusData.error())
.hasException(new IllegalStateException("Bad job"))
.hasAttributesSatisfying(
attrs ->
assertThat(attrs)
.containsEntry(
SemanticAttributes.CODE_NAMESPACE,
FailingJob.class.getName())
.containsEntry(
SemanticAttributes.CODE_FUNCTION, "execute"))));
.hasAttributesSatisfyingExactly(
equalTo(AttributeKey.stringKey("job.system"), "quartz"),
equalTo(
SemanticAttributes.CODE_NAMESPACE, FailingJob.class.getName()),
equalTo(SemanticAttributes.CODE_FUNCTION, "execute"))));
}

private static Scheduler createScheduler(String name) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ tasks {
withType<Test>().configureEach {
systemProperty("testLatestDeps", findProperty("testLatestDeps"))
jvmArgs("-Dotel.instrumentation.spring-batch.enabled=true")
// TODO run tests both with and without experimental span attributes
jvmArgs("-Dotel.instrumentation.spring-batch.experimental-span-attributes=true")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,31 @@
import static io.opentelemetry.javaagent.instrumentation.spring.batch.SpringBatchInstrumentationConfig.instrumentationName;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
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.internal.ConfigPropertiesUtil;
import org.springframework.batch.core.JobExecution;

public class JobSingletons {

private static final Instrumenter<JobExecution, Void> INSTRUMENTER =
Instrumenter.<JobExecution, Void>builder(
GlobalOpenTelemetry.get(), instrumentationName(), JobSingletons::extractSpanName)
.buildInstrumenter();
private static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES =
ConfigPropertiesUtil.getBoolean(
"otel.instrumentation.spring-batch.experimental-span-attributes", false);

private static final Instrumenter<JobExecution, Void> INSTRUMENTER;

static {
InstrumenterBuilder<JobExecution, Void> instrumenter =
Instrumenter.builder(
GlobalOpenTelemetry.get(), instrumentationName(), JobSingletons::extractSpanName);
if (CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) {
instrumenter.addAttributesExtractor(
AttributesExtractor.constant(AttributeKey.stringKey("job.system"), "spring_batch"));
}
INSTRUMENTER = instrumenter.buildInstrumenter();
}

private static String extractSpanName(JobExecution jobExecution) {
return "BatchJob " + jobExecution.getJobInstance().getJobName();
Expand Down
Loading

0 comments on commit e8e573c

Please sign in to comment.