Skip to content

Commit

Permalink
Merge pull request #25 from lucidsoftware/fix-minor-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sriraamas authored Sep 9, 2021
2 parents 74298b1 + c04446f commit 9d58365
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
12 changes: 6 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ def play = Project("play", file("play")).cross.cross(playAxis)
def `play-active` = Project("play-active", file("play-active"))
.cross.cross(playAxis).dependsOn(CrossableProject.toDependency(play))

lazy val `play_2.13_2.8` = play("2.8.2")("2.13.2")
lazy val `play_2.13_2.8` = play("2.8.8")("2.13.6")

lazy val `play_2.13_2.7` = play("2.7.5")("2.13.2")
lazy val `play_2.13_2.7` = play("2.7.9")("2.13.6")

lazy val `play_2.12_2.8` = play("2.7.5")("2.12.12")
lazy val `play_2.12_2.8` = play("2.8.8")("2.12.14")

lazy val `play_2.12_2.7` = play("2.7.5")("2.12.12")
lazy val `play_2.12_2.7` = play("2.7.9")("2.12.12")

lazy val `play_2.12_2.6` = play("2.6.25")("2.12.12")
lazy val `play_2.12_2.6` = play("2.6.25")("2.12.14")

lazy val `play_2.11_2.7` = play("2.7.5")("2.11.12")
lazy val `play_2.11_2.7` = play("2.7.9")("2.11.12")

lazy val `play_2.11_2.6` = play("2.6.25")("2.11.12")

Expand Down
5 changes: 3 additions & 2 deletions play/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ description := "Opentracing for Play"

libraryDependencies ++= Seq(
"com.lucidchart" % "opentracing-thread-context" % "0.5",
"com.google.guava" % "guava" % "21.0",
"com.typesafe.play" %% "play" % SettingKey[String]("playVersion").value,
"io.opentracing" % "opentracing-api" % "0.31.0",
"io.opentracing" % "opentracing-util" % "0.31.0"
"io.opentracing" % "opentracing-util" % "0.31.0",
"org.scala-lang.modules" %% "scala-collection-compat" % "2.5.0"
)

moduleName := "opentracing-play"
publishTo := sonatypePublishToBundle.value
// This is needed because the play axis changes the version
sonatypeBundleDirectory := (ThisBuild / baseDirectory).value / "target" / "sonatype-staging" / (Global / version).value
scalafmtOnCompile := true
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature", "-Xfatal-warnings")
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.opentracing.play
import com.google.common.collect.Maps
import io.opentracing.propagation.TextMap
import play.api.mvc.Headers
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

class HeadersTextMap(headers: Headers) extends TextMap {
def iterator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import play.api.libs.typedmap.TypedKey
import play.api.mvc.{RequestHeader, Result}

/**
* Applies attributes from [[RequestHeader#attrs]].
* Applies attributes from [[https://www.playframework.com/documentation/2.8.x/api/scala/play/api/mvc/RequestHeader.html RequestHeader#attrs]].
*
* @param attributeKeys The set of attributes to read from the [[RequestHeader]]. Must be explicitly stated because
* [[RequestHeader#attrs]] does not support iterating over all attributes on a request.
* @param attributeKeys The set of attributes to read from the [[https://www.playframework.com/documentation/2.8.x/api/scala/play/api/mvc/RequestHeader.html RequestHeader]]. Must be explicitly stated because
* [[https://www.playframework.com/documentation/2.8.x/api/scala/play/api/mvc/RequestHeader.html RequestHeader#attrs]] does not support iterating over all attributes on a request.
*
* Attributes may or may not have a displayName. In order to ensure that the tags set on the Span are sensible, only
* Attributes with a displayName set will be used to tag the Span.
*/
class RequestAttributesSpanTagger(attributeKeys: Set[TypedKey[String]]) extends SpanTagger {

def tag(span: Span, request: RequestHeader, result: Option[Result]) =
def tag(span: Span, request: RequestHeader, result: Option[Result]): Unit =
attributeKeys.foreach { key =>
key.displayName.foreach { name =>
request.attrs
Expand Down
2 changes: 1 addition & 1 deletion play/src/main/scala/io/opentracing/play/SpanTagger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import io.opentracing.Span
import play.api.mvc.{RequestHeader, Result}

abstract class SpanTagger {
def tag(span: Span, request: RequestHeader, result: Option[Result])
def tag(span: Span, request: RequestHeader, result: Option[Result]): Unit
}
42 changes: 27 additions & 15 deletions play/src/main/scala/io/opentracing/play/TracingAction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import io.opentracing.tag.Tags
import io.opentracing.threadcontext.ContextSpan
import java.util.concurrent.Callable

import akka.stream.ActorMaterializer
import akka.stream.Materializer
import play.api.mvc._

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}

private object RequestSpan {
def apply(tracer: Tracer, request: RequestHeader): Span =
Expand All @@ -35,7 +36,7 @@ class TracingRequest[+A](val span: Span, request: Request[A]) extends WrappedReq
class TracingActionBuilder(
protected[this] val tracer: Tracer,
protected[this] val contextSpan: ContextSpan,
taggers: Traversable[SpanTagger]
taggers: Iterable[SpanTagger]
)(val parser: BodyParser[AnyContent])(implicit ec: ExecutionContext)
extends ActionBuilder[TracingRequest, AnyContent] {

Expand All @@ -53,18 +54,29 @@ class TracingActionBuilder(
contextSpan
.set(span)
.call(new Callable[Future[Result]] {

private def failureHandler(exception: Throwable): Future[Result] = {
Tags.ERROR.set(tracingRequest.span, true)
finishSpan(tracingRequest, None)
Future.failed(exception)
}

def call() =
block(tracingRequest)
.map { result =>
finishSpan(tracingRequest, Some(result))
result
}
.recoverWith {
case e =>
Tags.ERROR.set(tracingRequest.span, true)
finishSpan(tracingRequest, None)
Future.failed(e)
}
Try(block(tracingRequest)) match {
case Failure(exception) =>
failureHandler(exception)
case Success(resultFuture) =>
resultFuture
.map { result =>
finishSpan(tracingRequest, Some(result))
result
}
.recoverWith {
case exception =>
failureHandler(exception)
}
}

})
}

Expand All @@ -74,7 +86,7 @@ class TracingActionBuilder(
/**
* Like TracingRequest but uses ContextSpan.DEFAULT and GlobalTracer for the context and tracer.
*/
class DefaultTracingActionBuilder(taggers: Traversable[SpanTagger])(
class DefaultTracingActionBuilder(taggers: Iterable[SpanTagger])(
implicit ec: ExecutionContext,
mat: ActorMaterializer
mat: Materializer
) extends TracingActionBuilder(GlobalTracer.get, ContextSpan.DEFAULT, taggers)(new BodyParsers.Default)

0 comments on commit 9d58365

Please sign in to comment.