Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zackangelo
Created December 6, 2016 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackangelo/e5db4415bd2e2e3e82750fc6f5f5eaab to your computer and use it in GitHub Desktop.
Save zackangelo/e5db4415bd2e2e3e82750fc6f5f5eaab to your computer and use it in GitHub Desktop.
Executor that propagates Brave tracing context
package com.bigcommerce.core.metrics.tracing
import com.github.kristofa.brave.{ServerSpan, ServerSpanThreadBinder}
import scala.concurrent.ExecutionContext
/**
* Created by zack.angelo on 12/5/16.
*/
class TracingExecutionContext(wrapped:ExecutionContext,
serverSpanThreadBinder: ServerSpanThreadBinder)
extends ExecutionContext { self =>
override def execute(runnable: Runnable): Unit =
wrapped.execute(runnable)
override def reportFailure(cause: Throwable): Unit =
wrapped.reportFailure(cause)
protected def prepareWithSpanState(spanState: ServerSpan): ExecutionContext = {
new ExecutionContext {
override def reportFailure(cause: Throwable): Unit =
self.reportFailure(cause)
override def execute(runnable: Runnable): Unit = {
val tracedRunnable = new Runnable {
override def run(): Unit = {
val oldState = serverSpanThreadBinder.getCurrentServerSpan
//Propagate tracing context
serverSpanThreadBinder.setCurrentSpan(spanState)
runnable.run()
serverSpanThreadBinder.setCurrentSpan(oldState)
}
}
self.execute(tracedRunnable)
}
override def prepare(): ExecutionContext =
self.prepareWithSpanState(serverSpanThreadBinder.getCurrentServerSpan)
}
}
override def prepare(): ExecutionContext =
self.prepareWithSpanState(serverSpanThreadBinder.getCurrentServerSpan)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment