-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try to handle issues in Pekko tracing when the scheduler is used (#12359
- Loading branch information
Showing
3 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...opentelemetry/javaagent/instrumentation/pekkoactor/v1_0/PekkoScheduleInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.pekkoactor.v1_0; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class PekkoScheduleInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.apache.pekko.actor.LightArrayRevolverScheduler"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("schedule") | ||
.and(takesArgument(0, named("scala.concurrent.duration.FiniteDuration"))) | ||
.and(takesArgument(1, named("scala.concurrent.duration.FiniteDuration"))) | ||
.and(takesArgument(2, named("java.lang.Runnable"))) | ||
.and(takesArgument(3, named("scala.concurrent.ExecutionContext"))), | ||
PekkoScheduleInstrumentation.class.getName() + "$ScheduleAdvice"); | ||
transformer.applyAdviceToMethod( | ||
named("scheduleOnce") | ||
.and(takesArgument(0, named("scala.concurrent.duration.FiniteDuration"))) | ||
.and(takesArgument(1, named("java.lang.Runnable"))) | ||
.and(takesArgument(2, named("scala.concurrent.ExecutionContext"))), | ||
PekkoScheduleInstrumentation.class.getName() + "$ScheduleOnceAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ScheduleAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void enterSchedule( | ||
@Advice.Argument(value = 2, readOnly = false) Runnable runnable) { | ||
Context context = Java8BytecodeBridge.currentContext(); | ||
runnable = context.wrap(runnable); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ScheduleOnceAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void enterScheduleOnce( | ||
@Advice.Argument(value = 1, readOnly = false) Runnable runnable) { | ||
Context context = Java8BytecodeBridge.currentContext(); | ||
runnable = context.wrap(runnable); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...scala/io/opentelemetry/javaagent/instrumentation/pekkoactor/v1_0/PekkoSchedulerTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.pekkoactor.v1_0 | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry | ||
import io.opentelemetry.api.trace.Span | ||
import org.apache.pekko.actor.ActorSystem | ||
import org.apache.pekko.pattern.after | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import scala.concurrent.{Await, Future} | ||
import scala.concurrent.duration.DurationInt | ||
|
||
class PekkoSchedulerTest { | ||
|
||
@Test | ||
def checkThatSpanWorksWithPekkoScheduledEvents(): Unit = { | ||
val system = ActorSystem("my-system") | ||
implicit val executionContext = system.dispatcher | ||
val tracer = GlobalOpenTelemetry.get.getTracer("test-tracer") | ||
val initialSpan = tracer.spanBuilder("test").startSpan() | ||
val scope = initialSpan.makeCurrent() | ||
try { | ||
val futureResult = for { | ||
result1 <- Future { | ||
compareSpanContexts(Span.current(), initialSpan) | ||
1 | ||
} | ||
_ = compareSpanContexts(Span.current(), initialSpan) | ||
result2 <- after(200.millis, system.scheduler)(Future.successful(2)) | ||
_ = compareSpanContexts(Span.current(), initialSpan) | ||
} yield result1 + result2 | ||
assertThat(Await.result(futureResult, 5.seconds)).isEqualTo(3) | ||
} finally { | ||
system.terminate() | ||
scope.close() | ||
initialSpan.end() | ||
} | ||
} | ||
|
||
private def compareSpanContexts(span1: Span, span2: Span): Unit = { | ||
assertThat(span1.getSpanContext().getTraceId()) | ||
.isEqualTo(span2.getSpanContext().getTraceId()) | ||
assertThat(span1.getSpanContext().getSpanId()) | ||
.isEqualTo(span2.getSpanContext().getSpanId()) | ||
} | ||
} |