Skip to content

Commit

Permalink
Merge pull request #32 from ant8e/options
Browse files Browse the repository at this point in the history
Add span name and span options parameters
  • Loading branch information
mpilquist authored Apr 24, 2023
2 parents c23b5ac + 932cb0d commit 300082d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ThisBuild / tlBaseVersion := "0.5"
ThisBuild / tlBaseVersion := "0.6"

val http4sVersion = "0.23.17"
val natchezVersion = "0.3.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@ import org.typelevel.ci.CIString
* are passed to Kernel by default.
*
* @define isKernelHeader should an HTTP header be passed to Kernel or not
*
* @define spanName compute the span name from the request
*
* @define spanOptions options used in span creation
*/
trait EntryPointOps[F[_]] { outer =>

def self: EntryPoint[F]

/**
* Given an entry point and HTTP Routes in Kleisli[F, Span[F], *] return routes in F. A new span
* is created with the URI path as the name, either as a continuation of the incoming trace, if
* is created with by default the URI path as the name, either as a continuation of the incoming trace, if
* any, or as a new root.
*
* @note $excludedHeaders
*
* @param isKernelHeader $isKernelHeader
* @param spanName $spanName
* @param spanOptions $spanOptions
*/
def liftT(
routes: HttpRoutes[Kleisli[F, Span[F], *]],
isKernelHeader: CIString => Boolean = name => !EntryPointOps.ExcludedHeaders.contains(name)
isKernelHeader: CIString => Boolean = name => !EntryPointOps.ExcludedHeaders.contains(name),
spanName: org.http4s.Request[F] => String = _.uri.path.toString,
spanOptions: Span.Options = Span.Options.Defaults,
)(implicit ev: MonadCancel[F, Throwable]): HttpRoutes[F] =
Kleisli { req =>
val kernelHeaders = req.headers.headers
Expand All @@ -47,7 +55,7 @@ trait EntryPointOps[F[_]] { outer =>
.toMap

val kernel = Kernel(kernelHeaders)
val spanR = self.continueOrElseRoot(req.uri.path.toString, kernel)
val spanR = self.continueOrElseRoot(spanName(req), kernel, spanOptions)
OptionT {
spanR.use { span =>
routes.run(req.mapK(lift)).mapK(applyK(span)).map(_.mapK(applyK(span))).value
Expand Down Expand Up @@ -83,9 +91,11 @@ trait EntryPointOps[F[_]] { outer =>
*/
def wsLiftT(
routes: WebSocketBuilder2[Kleisli[F, Span[F], *]] => HttpRoutes[Kleisli[F, Span[F], *]],
isKernelHeader: CIString => Boolean = name => !EntryPointOps.ExcludedHeaders.contains(name)
)(implicit ev: MonadCancel[F, Throwable]): WebSocketBuilder2[F] => HttpRoutes[F] = wsb =>
liftT(routes(wsb.imapK(lift)(Span.dropTracing)), isKernelHeader)
isKernelHeader: CIString => Boolean = name => !EntryPointOps.ExcludedHeaders.contains(name),
spanName: org.http4s.Request[F] => String = _.uri.path.toString,
spanOptions: Span.Options = Span.Options.Defaults
)(implicit ev: MonadCancel[F, Throwable]): WebSocketBuilder2[F] => HttpRoutes[F] = wsb =>
liftT(routes(wsb.imapK(lift)(Span.dropTracing)), isKernelHeader, spanName, spanOptions)

/**
* Lift a `WebSocketBuilder2 => HttpRoutes`-yielding resource that consumes `Span`s into the bare
Expand All @@ -99,9 +109,11 @@ trait EntryPointOps[F[_]] { outer =>
*/
def wsLiftR(
routes: Resource[Kleisli[F, Span[F], *], WebSocketBuilder2[Kleisli[F, Span[F], *]] => HttpRoutes[Kleisli[F, Span[F], *]]],
isKernelHeader: CIString => Boolean = name => !EntryPointOps.ExcludedHeaders.contains(name)
isKernelHeader: CIString => Boolean = name => !EntryPointOps.ExcludedHeaders.contains(name),
spanName: org.http4s.Request[F] => String = _.uri.path.toString,
spanOptions: Span.Options = Span.Options.Defaults
)(implicit ev: MonadCancel[F, Throwable]): Resource[F, WebSocketBuilder2[F] => HttpRoutes[F]] =
routes.map(wsLiftT(_, isKernelHeader)).mapK(Span.dropTracing)
routes.map(wsLiftT(_, isKernelHeader, spanName, spanOptions)).mapK(Span.dropTracing)

private val lift: F ~> Kleisli[F, Span[F], *] =
Kleisli.liftK
Expand Down

0 comments on commit 300082d

Please sign in to comment.