-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an HTTP4S client integration (#52)
* Add an HTTP4s client integration * Add newline * Update docs
- Loading branch information
Showing
9 changed files
with
149 additions
and
16 deletions.
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
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
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
44 changes: 44 additions & 0 deletions
44
natchez-http4s/src/main/scala/com/ovoenergy/effect/natchez/http4s/client/TracedClient.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,44 @@ | ||
package com.ovoenergy.effect.natchez.http4s.client | ||
|
||
import cats.data.Kleisli | ||
import cats.effect.{Resource, Sync} | ||
import cats.syntax.functor._ | ||
import cats.~> | ||
import com.ovoenergy.effect.natchez.http4s.Configuration | ||
import com.ovoenergy.effect.natchez.http4s.server.TraceMiddleware.removeNumericPathSegments | ||
import natchez.{Span, Trace} | ||
import org.http4s._ | ||
import org.http4s.client.Client | ||
|
||
trait TracedClient[F[_]] { | ||
def named(s: String): Client[F] | ||
} | ||
|
||
object TracedClient { | ||
|
||
type Traced[F[_], A] = Kleisli[F, Span[F], A] | ||
|
||
private def dropTracing[F[_]](span: Span[F]): Traced[F, *] ~> F = | ||
Kleisli.applyK[F, Span[F]](span) | ||
|
||
private def trace[F[_]]: F ~> Traced[F, *] = | ||
Kleisli.liftK | ||
|
||
def apply[F[_]: Sync](client: Client[F], config: Configuration[F]): TracedClient[Traced[F, *]] = | ||
name => Client[Traced[F, *]] { req => | ||
Resource( | ||
Trace[Traced[F, *]].span(s"$name:http.request:${removeNumericPathSegments(req.uri)}") { | ||
for { | ||
span <- Kleisli.ask[F, Span[F]] | ||
headers <- trace(span.kernel.map(_.toHeaders.map { case (k, v) => Header(k, v) })) | ||
withHeader = req.putHeaders(headers.toSeq: _*).mapK(dropTracing(span)) | ||
reqTags <- trace(config.request.value.run(req.mapK(dropTracing(span)))) | ||
_ <- trace(span.put(reqTags.toSeq:_*)) | ||
(resp, rel) <- client.run(withHeader).mapK(trace[F]).map(_.mapK(trace)).allocated | ||
respTags <- trace(config.response.value.run(resp.mapK(dropTracing(span)))) | ||
_ <- trace(span.put(respTags.toSeq:_*)) | ||
} yield resp -> rel | ||
} | ||
) | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
natchez-http4s/src/test/scala/com/ovoenergy/effect/natchez/http4s/client/TestClient.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,26 @@ | ||
package com.ovoenergy.effect.natchez.http4s.client | ||
|
||
import cats.data.Kleisli | ||
import cats.effect.Sync | ||
import cats.effect.concurrent.Ref | ||
import org.http4s.{Request, Response} | ||
import org.http4s.client.Client | ||
import cats.syntax.functor._ | ||
|
||
trait TestClient[F[_]] { | ||
def requests: F[List[Request[F]]] | ||
def client: Client[F] | ||
} | ||
|
||
object TestClient { | ||
|
||
def apply[F[_]: Sync]: F[TestClient[F]] = | ||
Ref.of[F, List[Request[F]]](List.empty).map { ref => | ||
new TestClient[F] { | ||
def client: Client[F] = | ||
Client.fromHttpApp[F](Kleisli(r => ref.update(_ :+ r).as(Response[F]()))) | ||
def requests: F[List[Request[F]]] = | ||
ref.get | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...z-http4s/src/test/scala/com/ovoenergy/effect/natchez/http4s/client/TracedClientTest.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,56 @@ | ||
package com.ovoenergy.effect.natchez.http4s.client | ||
|
||
import cats.data.Kleisli | ||
import cats.effect.{IO, Timer} | ||
import com.ovoenergy.effect.natchez.TestEntryPoint | ||
import com.ovoenergy.effect.natchez.TestEntryPoint.TestSpan | ||
import com.ovoenergy.effect.natchez.http4s.Configuration | ||
import natchez.{Kernel, Span} | ||
import org.http4s.Request | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import scala.concurrent.ExecutionContext.global | ||
|
||
class TracedClientTest extends AnyWordSpec with Matchers { | ||
|
||
implicit val timer: Timer[IO] = IO.timer(global) | ||
val unit: Kleisli[IO, Span[IO], Unit] = Kleisli.pure(()) | ||
val config: Configuration[IO] = Configuration.default[IO]() | ||
type TraceIO[A] = Kleisli[IO, Span[IO], A] | ||
|
||
"TracedClient" should { | ||
|
||
"Add the kernel to requests" in { | ||
|
||
val requests: List[Request[IO]] = ( | ||
for { | ||
client <- TestClient[IO] | ||
ep <- TestEntryPoint[IO] | ||
http = TracedClient(client.client, config) | ||
kernel = Kernel(Map("X-Trace-Token" -> "token")) | ||
_ <- ep.continue("bar", kernel).use(http.named("foo").status(Request[TraceIO]()).run) | ||
reqs <- client.requests | ||
} yield reqs | ||
).unsafeRunSync | ||
|
||
requests.forall(_.headers.exists(_.name.value === "X-Trace-Token")) shouldBe true | ||
} | ||
|
||
"Create a new span for HTTP requests" in { | ||
|
||
val spans: List[TestSpan] = ( | ||
for { | ||
client <- TestClient[IO] | ||
ep <- TestEntryPoint[IO] | ||
http = TracedClient(client.client, config) | ||
_ <- ep.root("root").use(http.named("foo").status(Request[TraceIO]()).run) | ||
reqs <- ep.spans | ||
} yield reqs | ||
).unsafeRunSync | ||
|
||
spans.length shouldBe 2 | ||
spans.head.name shouldBe "foo:http.request:/" | ||
} | ||
} | ||
} |
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
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