Skip to content

Instantly share code, notes, and snippets.

class MDCExecutionContextExecutor(val context: java.util.Map[_, _], val delegate: ExecutionContext) extends ExecutionContextExecutor {
def execute(runnable: Runnable) {
delegate.execute(new Runnable {
def run() {
val oldContext = MDC.getCopyOfContextMap
setContextMap(context)
try {
runnable.run()
} finally {
setContextMap(oldContext)
@yanns
yanns / gist:b0af896bf342d07bb5c2
Created May 18, 2014 13:44
expire with scala macros
scala> import scala.reflect.macros.blackbox.Context
import scala.reflect.macros.blackbox.Context
scala> def expire_impl(c: Context)(): c.Expr[Unit] = {
| c.error(c.enclosingPosition, "has expired")
| c.literalUnit
| }
<console>:10: warning: method literalUnit in trait ExprUtils is deprecated: Use quasiquotes instead
c.literalUnit
^
@yanns
yanns / gist:f7da61c582ab1da3535d
Created October 9, 2014 13:23
SLF4J MDC propagation in Play
package monitoring
import java.util.concurrent.TimeUnit
import akka.dispatch._
import com.typesafe.config.Config
import org.slf4j.MDC
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.{Duration, FiniteDuration}
@yanns
yanns / gist:ac7eb02ac153ab92e2b6
Created October 13, 2014 06:50
MDCPropagatingDispatcher
/**
* A MDC propagating dispatcher.
*
* This dispatcher propagates the MDC current request context if it's set when it's executed.
*/
class MDCPropagatingDispatcher(_configurator: MessageDispatcherConfigurator,
id: String,
throughput: Int,
throughputDeadlineTime: Duration,
executorServiceFactoryProvider: ExecutorServiceFactoryProvider,
@yanns
yanns / gist:cfd27632976aa9f48b31
Created May 9, 2015 21:14
Netty handler for Kamon context
package server
import io.sphere.util.Logging
import kamon.Kamon
import kamon.trace.TraceContext
import org.jboss.netty.channel._
import org.jboss.netty.handler.codec.http.HttpRequest
import scala.util.control.NonFatal
@yanns
yanns / gist:e96893f6ed434555bd37
Created May 9, 2015 21:41
Trace context with scala Future
Tracer.withNewContext("ws", Some("blabla"), autoFinish = true) {
log.error("hello1 bla bla")
Future {
log.error("hello2 bla bla")
}
}
package controllers
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.iteratee.Iteratee
import play.api.mvc.MultipartFormData.FilePart
import play.api.mvc.{ MultipartFormData, BodyParser, BodyParsers }
import scala.collection.mutable.ArrayBuilder
case class MaxSizeExceeded(length: Long)
@yanns
yanns / NatN in scala type.scala
Created November 5, 2015 12:23
Natural numbers in scala types
sealed trait Nat {
type plus[That <: Nat] <: Nat
}
sealed trait Nat0 extends Nat {
type plus[That <: Nat] = That
}
sealed trait NatN[Prev <: Nat] extends Nat {
type plus[That <: Nat] = NatN[Prev#plus[That]]
}
object Nat {
@yanns
yanns / Boolean in scala type.scala
Created November 5, 2015 12:24
Boolean in scala type
sealed trait BoolType {
type Not <: BoolType
type Or[That <: BoolType] <: BoolType
}
sealed trait TrueType extends BoolType {
override type Not = FalseType
override type Or[That <: BoolType] = TrueType
}
sealed trait FalseType extends BoolType {
override type Not = TrueType
case class Pyramide(a1: Int, a2: Int, a3: Int, a4: Int) {
val sum11 = a1 + a2
val sum12 = a2 + a3
val sum13 = a3 + a4
val sum21 = sum11 + sum12
val sum22 = sum12 + sum13
val sum = sum21 + sum22
override def toString: String =
s"""