- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1k
 
add Akka Scheduler context propagation #12373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 
        
  ...ava/io/opentelemetry/javaagent/instrumentation/akkaactor/AkkaScheduleInstrumentation.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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.akkaactor; | ||
| 
     | 
||
| 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 AkkaScheduleInstrumentation implements TypeInstrumentation { | ||
| 
     | 
||
| @Override | ||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||
| return named("akka.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"))), | ||
| AkkaScheduleInstrumentation.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"))), | ||
| AkkaScheduleInstrumentation.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); | ||
| } | ||
| } | ||
| } | ||
        
          
  
    
      
          
            48 changes: 48 additions & 0 deletions
          
          48 
        
  ...vaagent/src/test/scala/io/opentelemetry/instrumentation/akkaactor/AkkaSchedulerTest.scala
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,48 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package io.opentelemetry.instrumentation.akkaactor | ||
| 
     | 
||
| import akka.pattern.after | ||
| import io.opentelemetry.api.GlobalOpenTelemetry | ||
| import io.opentelemetry.api.trace.Span | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.jupiter.api.Test | ||
| import scala.concurrent.{Await, Future} | ||
| import scala.concurrent.duration.DurationInt | ||
| 
     | 
||
| class AkkaSchedulerTest { | ||
| 
     | 
||
| @Test | ||
| def checkThatSpanWorksWithAkkaScheduledEvents(): Unit = { | ||
| val system = AkkaActors.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 { | ||
| 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()) | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just for my own curiosity: why getting the current context from Java8BytecodeBride?
why not 'Context.current()' directly?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what most of the code does so I did it too. Are you a regular contributor to this lib and if so, can you explain why I should change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opentelemetry-java-instrumentation/docs/contributing/javaagent-structure.md
Line 43 in b184587
I assume this includes here but I am a first time contributor so I will change this if any of the regular contributors say so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's needed when the Advice is inlined into bytecode that targets a version of Java prior to Java 8
interestingly, I think it will no longer be needed after #11457 (since we will no longer be inlining our advice code)
the javadoc explains a bit more:
opentelemetry-java-instrumentation/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/Java8BytecodeBridge.java
Lines 13 to 17 in b184587
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i was not asking for a change.. just curious to know the reason behind. @trask explained it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trask The existing opentelemetry-java-instrumentation code for Akka and Pekko uses Java8BytecodeBridge. Is it ok if I stick this 'style' and that all the code can be changed to stop using Java8BytecodeBridge in a future commit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, definitely, it's totally fine to use
Java8BytecodeBridge