aboutsummaryrefslogblamecommitdiff
path: root/kamon-core/src/main/scala/kamon/TraceContextSwap.scala
blob: 470b2f3422c3c4c05e1b8c8d4f9ecaa00367eab9 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12

             

                    







                                                                                                    


                                   
                             
                                                                      
                           
                                
                      
                           




                           
                                        
 
     




                                                
package kamon

import org.slf4j.MDC

/**
 *  Provides support for making a TraceContext available as ThreadLocal and cleanning up afterwards.
 */
trait TraceContextSwap {

  def withContext[A](ctx: Option[TraceContext], body: => A): A = withContext(ctx, body, body)

  def withContext[A](ctx: Option[TraceContext], primary: => A, fallback: => A): A = {

    val previous = Tracer.context()
    val r = ctx match {
      case Some(context) => {
        //MDC.put("uow", context.userContext.get.asInstanceOf[String])
        Tracer.set(context)
        val bodyResult = primary
        //Tracer.clear
        //MDC.remove("uow")

        bodyResult
      }
      case None => fallback
    }
    previous.map(ctx => Tracer.set(ctx))

    r
  }

}

object TraceContextSwap extends TraceContextSwap