-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finagle/finagle-core: VerboseRequestTracer filter that adds tracing t…
…hrough the finagle stack Problem We want to be able to trace requests throughout the finagle stack during testing. Solution Add a filter, VerboseRequestTracer, that adds this functionality for traced requests. It is disabled by default and can be enabled via flag `com.twitter.finagle.filter.verboseRequestTracing`. Differential Revision: https://phabricator.twitter.biz/D1182361
- Loading branch information
Showing
2 changed files
with
85 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
77 changes: 77 additions & 0 deletions
77
finagle-core/src/main/scala/com/twitter/finagle/filter/VerboseRequestTracer.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,77 @@ | ||
package com.twitter.finagle.filter | ||
|
||
import com.twitter.app.GlobalFlag | ||
import com.twitter.finagle.ClientConnection | ||
import com.twitter.finagle.Service | ||
import com.twitter.finagle.ServiceFactory | ||
import com.twitter.finagle.ServiceFactoryProxy | ||
import com.twitter.finagle.ServiceProxy | ||
import com.twitter.finagle.Stack | ||
import com.twitter.finagle.tracing.Trace | ||
import com.twitter.util.Future | ||
|
||
object verboseRequestTracing | ||
extends GlobalFlag[Boolean]( | ||
"""Experimental flag. Enables verbose request tracing, which includes tracing though the finagle stack""".stripMargin | ||
) | ||
|
||
private[twitter] object VerboseRequestTracer { | ||
|
||
sealed trait Param { | ||
def mk(): (Param, Stack.Param[Param]) = (this, Param.param) | ||
} | ||
|
||
private[finagle] object Param { | ||
case object Disabled extends Param | ||
case object Enabled extends Param | ||
|
||
implicit val param: Stack.Param[Param] = new Stack.Param[Param] { | ||
lazy val default: Param = { | ||
verboseRequestTracing.get match { | ||
case Some(value) if value => Enabled | ||
case _ => Disabled | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Enables the [[VerboseRequestTracer]]. | ||
*/ | ||
val Enabled: Param = Param.Enabled | ||
|
||
/** | ||
* Disables the [[VerboseRequestTracer]] (disabled by default). | ||
*/ | ||
val Disabled: Param = Param.Disabled | ||
|
||
private[finagle] val stackTransformer: Stack.Transformer = | ||
new Stack.Transformer { | ||
def apply[Req, Rep](stack: Stack[ServiceFactory[Req, Rep]]): Stack[ServiceFactory[Req, Rep]] = | ||
stack.map((hd, sf) => withRequestTracing(hd.role, sf)) | ||
} | ||
|
||
private[this] def withRequestTracing[Req, Rep]( | ||
role: Stack.Role, | ||
svcFac: ServiceFactory[Req, Rep] | ||
): ServiceFactory[Req, Rep] = | ||
new ServiceFactoryProxy[Req, Rep](svcFac) { | ||
override def apply(conn: ClientConnection): Future[Service[Req, Rep]] = { | ||
super.apply(conn).map { svc => | ||
new ServiceProxy[Req, Rep](svc) { | ||
override def apply(request: Req): Future[Rep] = { | ||
if (!Trace.isActivelyTracing) { | ||
super.apply(request) | ||
} else { | ||
Trace.traceLocalFuture(role.name + "_async") { | ||
Trace.traceLocal(role.name + "_sync") { | ||
super.apply(request) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |