Skip to content

Commit

Permalink
Add code attributes to spring-scheduling spans (#5306)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Feb 8, 2022
1 parent 0b60069 commit a0f3ad7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.spring.scheduling;

import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesExtractor;
import javax.annotation.Nullable;
import org.springframework.scheduling.support.ScheduledMethodRunnable;

public class SpringSchedulingCodeAttributesExtractor
extends CodeAttributesExtractor<Runnable, Void> {

@Override
protected Class<?> codeClass(Runnable runnable) {
if (runnable instanceof ScheduledMethodRunnable) {
ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
return scheduledMethodRunnable.getTarget().getClass();
} else {
return runnable.getClass();
}
}

@Override
protected String methodName(Runnable runnable) {
if (runnable instanceof ScheduledMethodRunnable) {
ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
return scheduledMethodRunnable.getMethod().getName();
} else {
return "run";
}
}

@Override
@Nullable
protected String filePath(Runnable runnable) {
return null;
}

@Override
@Nullable
protected Long lineNumber(Runnable runnable) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class SpringSchedulingSingletons {
GlobalOpenTelemetry.get(),
"io.opentelemetry.spring-scheduling-3.1",
SpringSchedulingSingletons::extractSpanName)
.addAttributesExtractor(new SpringSchedulingCodeAttributesExtractor())
.newInstrumenter();

private static String extractSpanName(Runnable runnable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SpringSchedulingTest extends AgentInstrumentationSpecification {
name "TriggerTask.run"
hasNoParent()
attributes {
"code.namespace" "TriggerTask"
"code.function" "run"
}
}
}
Expand All @@ -46,6 +48,8 @@ class SpringSchedulingTest extends AgentInstrumentationSpecification {
name "IntervalTask.run"
hasNoParent()
attributes {
"code.namespace" "IntervalTask"
"code.function" "run"
}
}
}
Expand All @@ -67,6 +71,8 @@ class SpringSchedulingTest extends AgentInstrumentationSpecification {
nameContains "LambdaTaskConfigurer\$\$Lambda\$"
hasNoParent()
attributes {
"code.namespace" { it.contains("LambdaTaskConfigurer\$\$Lambda\$") }
"code.function" "run"
}
}
}
Expand Down

0 comments on commit a0f3ad7

Please sign in to comment.