Skip to content

Commit

Permalink
Merge pull request #38 from Dwolla/updates
Browse files Browse the repository at this point in the history
Use natchez-testkit instead of our own InMemory implementation
  • Loading branch information
armanbilge authored Aug 29, 2024
2 parents bf3e615 + dd1cc4e commit 48b0898
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 158 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.12.17, 2.13.10, 3.2.1]
scala: [2.12.17, 2.13.10, 3.3.3]
java: [temurin@17]
project: [rootJS, rootJVM, rootNative]
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -211,32 +211,32 @@ jobs:
tar xf targets.tar
rm targets.tar
- name: Download target directories (3.2.1, rootJS)
- name: Download target directories (3.3.3, rootJS)
uses: actions/download-artifact@v3
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3.2.1-rootJS
name: target-${{ matrix.os }}-${{ matrix.java }}-3.3.3-rootJS

- name: Inflate target directories (3.2.1, rootJS)
- name: Inflate target directories (3.3.3, rootJS)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (3.2.1, rootJVM)
- name: Download target directories (3.3.3, rootJVM)
uses: actions/download-artifact@v3
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3.2.1-rootJVM
name: target-${{ matrix.os }}-${{ matrix.java }}-3.3.3-rootJVM

- name: Inflate target directories (3.2.1, rootJVM)
- name: Inflate target directories (3.3.3, rootJVM)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (3.2.1, rootNative)
- name: Download target directories (3.3.3, rootNative)
uses: actions/download-artifact@v3
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3.2.1-rootNative
name: target-${{ matrix.os }}-${{ matrix.java }}-3.3.3-rootNative

- name: Inflate target directories (3.2.1, rootNative)
- name: Inflate target directories (3.3.3, rootNative)
run: |
tar xf targets.tar
rm targets.tar
Expand Down
14 changes: 7 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ThisBuild / tlBaseVersion := "0.6"

val http4sVersion = "0.23.17"
val natchezVersion = "0.3.0"
val natchezVersion = "0.3.5"
val scala212Version = "2.12.17"
val scala213Version = "2.13.10"
val scala3Version = "3.2.1"
val scala3Version = "3.3.3"
val slf4jVersion = "2.0.4"
val munitCEVersion = "2.0.0-M3"

Expand Down Expand Up @@ -52,10 +52,11 @@ lazy val http4s = crossProject(JSPlatform, JVMPlatform, NativePlatform)
name := "natchez-http4s",
description := "Natchez middleware for http4s.",
libraryDependencies ++= Seq(
"org.tpolecat" %%% "natchez-core" % natchezVersion,
"org.http4s" %%% "http4s-core" % http4sVersion,
"org.http4s" %%% "http4s-client" % http4sVersion,
"org.http4s" %%% "http4s-server" % http4sVersion,
"org.tpolecat" %%% "natchez-core" % natchezVersion,
"org.http4s" %%% "http4s-core" % http4sVersion,
"org.http4s" %%% "http4s-client" % http4sVersion,
"org.http4s" %%% "http4s-server" % http4sVersion,
"org.tpolecat" %%% "natchez-testkit" % natchezVersion % Test,
)
)

Expand Down Expand Up @@ -102,4 +103,3 @@ lazy val docs = project
makeSite := makeSite.dependsOn(mdoc.toTask("")).value,
mdocExtraArguments := Seq("--no-link-hygiene"), // paradox handles this
)

127 changes: 3 additions & 124 deletions modules/http4s/src/test/scala/natchez/http4s/InMemory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,130 +5,10 @@
package natchez
package http4s

import java.net.URI

import cats.data.{Chain, Kleisli}
import cats.effect.{IO, MonadCancelThrow, Ref, Resource}

import natchez.Span.Options
import cats.data.Kleisli
import cats.effect.{IO, MonadCancelThrow}
import munit.CatsEffectSuite

object InMemory {

class Span(
lineage: Lineage,
k: Kernel,
ref: Ref[IO, Chain[(Lineage, NatchezCommand)]],
val options: Options
) extends natchez.Span.Default[IO] {
override protected val spanCreationPolicyOverride: Options.SpanCreationPolicy =
options.spanCreationPolicy

def put(fields: (String, natchez.TraceValue)*): IO[Unit] =
ref.update(_.append(lineage -> NatchezCommand.Put(fields.toList)))

def attachError(err: Throwable, fields: (String, TraceValue)*): IO[Unit] =
ref.update(_.append(lineage -> NatchezCommand.AttachError(err, fields.toList)))

def log(event: String): IO[Unit] =
ref.update(_.append(lineage -> NatchezCommand.LogEvent(event)))

def log(fields: (String, TraceValue)*): IO[Unit] =
ref.update(_.append(lineage -> NatchezCommand.LogFields(fields.toList)))

def kernel: IO[Kernel] =
ref.update(_.append(lineage -> NatchezCommand.AskKernel(k))).as(k)

def makeSpan(name: String, options: Options): Resource[IO, natchez.Span[IO]] = {
val acquire = ref
.update(_.append(lineage -> NatchezCommand.CreateSpan(name, options.parentKernel, options)))
.as(new Span(lineage / name, k, ref, options))

val release = ref.update(_.append(lineage -> NatchezCommand.ReleaseSpan(name)))

Resource.make(acquire)(_ => release)
}

def traceId: IO[Option[String]] =
ref.update(_.append(lineage -> NatchezCommand.AskTraceId)).as(None)

def spanId: IO[Option[String]] =
ref.update(_.append(lineage -> NatchezCommand.AskSpanId)).as(None)

def traceUri: IO[Option[URI]] =
ref.update(_.append(lineage -> NatchezCommand.AskTraceUri)).as(None)
}

class EntryPoint(val ref: Ref[IO, Chain[(Lineage, NatchezCommand)]])
extends natchez.EntryPoint[IO] {

override def root(name: String, options: natchez.Span.Options): Resource[IO, Span] =
newSpan(name, Kernel(Map.empty), options)

override def continue(
name: String,
kernel: Kernel,
options: natchez.Span.Options
): Resource[IO, Span] =
newSpan(name, kernel, options)

override def continueOrElseRoot(
name: String,
kernel: Kernel,
options: natchez.Span.Options
): Resource[IO, Span] =
newSpan(name, kernel, options)

private def newSpan(
name: String,
kernel: Kernel,
options: natchez.Span.Options
): Resource[IO, Span] = {
val acquire = ref
.update(_.append(Lineage.Root -> NatchezCommand.CreateRootSpan(name, kernel, options)))
.as(new Span(Lineage.Root, kernel, ref, options))

val release = ref.update(_.append(Lineage.Root -> NatchezCommand.ReleaseRootSpan(name)))

Resource.make(acquire)(_ => release)
}
}

object EntryPoint {
def create: IO[EntryPoint] =
Ref.of[IO, Chain[(Lineage, NatchezCommand)]](Chain.empty).map(log => new EntryPoint(log))
}

sealed trait Lineage {
def /(name: String): Lineage.Child = Lineage.Child(name, this)
}
object Lineage {
case object Root extends Lineage
final case class Child(name: String, parent: Lineage) extends Lineage
}

sealed trait NatchezCommand
object NatchezCommand {
case class AskKernel(kernel: Kernel) extends NatchezCommand
case object AskSpanId extends NatchezCommand
case object AskTraceId extends NatchezCommand
case object AskTraceUri extends NatchezCommand
case class Put(fields: List[(String, natchez.TraceValue)]) extends NatchezCommand
case class CreateSpan(name: String, kernel: Option[Kernel], options: natchez.Span.Options)
extends NatchezCommand
case class ReleaseSpan(name: String) extends NatchezCommand
case class AttachError(err: Throwable, fields: List[(String, TraceValue)])
extends NatchezCommand
case class LogEvent(event: String) extends NatchezCommand
case class LogFields(fields: List[(String, TraceValue)]) extends NatchezCommand
// entry point
case class CreateRootSpan(name: String, kernel: Kernel, options: natchez.Span.Options)
extends NatchezCommand
case class ReleaseRootSpan(name: String) extends NatchezCommand
}

}

trait InMemorySuite extends CatsEffectSuite {
type Lineage = InMemory.Lineage
val Lineage = InMemory.Lineage
Expand Down Expand Up @@ -166,7 +46,7 @@ trait InMemorySuite extends CatsEffectSuite {
makeTraceAndResolver: Span[IO] => IO[(Trace[F], F[Unit] => IO[Unit])],
expectedHistory: List[(Lineage, NatchezCommand)]
) =
InMemory.EntryPoint.create.flatMap { ep =>
InMemory.EntryPoint.create[IO].flatMap { ep =>
val traced = ep.root("root").use { r =>
makeTraceAndResolver(r).flatMap { case (traceInstance, resolve) =>
resolve(traceProgram(traceInstance))
Expand All @@ -177,4 +57,3 @@ trait InMemorySuite extends CatsEffectSuite {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package natchez.http4s

import cats.Monad
import cats.data.{ Chain, Kleisli }
import cats.effect.{ IO, MonadCancelThrow, Resource }
import natchez.{Kernel, Span, Trace}
import cats.data.{Chain, Kleisli}
import cats.effect.{IO, MonadCancelThrow, Resource}
import natchez.{InMemory, Kernel, Span, Trace}
import natchez.TraceValue.StringValue
import natchez.http4s.syntax.entrypoint._
import org.http4s._
Expand Down Expand Up @@ -96,22 +96,22 @@ class NatchezMiddlewareSuite extends InMemorySuite {
)

List(
(Lineage.Root, NatchezCommand.CreateRootSpan("/hello/some-name", requestKernel, Span.Options.Defaults)),
(Lineage.Root, NatchezCommand.CreateSpan("call-proxy", None, Span.Options.Defaults)),
(Lineage.Root / "call-proxy", NatchezCommand.CreateSpan("http4s-client-request", None, Span.Options.Defaults)),
(Lineage.Root / "call-proxy" / "http4s-client-request", NatchezCommand.AskKernel(requestKernel)),
(Lineage.Root / "call-proxy" / "http4s-client-request", NatchezCommand.Put(clientRequestTags)),
(Lineage.Root / "call-proxy" / "http4s-client-request", NatchezCommand.Put(clientResponseTags)),
(Lineage.Root / "call-proxy", NatchezCommand.ReleaseSpan("http4s-client-request")),
(Lineage.Root, NatchezCommand.ReleaseSpan("call-proxy")),
(Lineage.Root, NatchezCommand.Put(requestTags)),
(Lineage.Root, NatchezCommand.Put(responseTags)),
(Lineage.Root, NatchezCommand.ReleaseRootSpan("/hello/some-name"))
(Lineage.Root, NatchezCommand.CreateRootSpan("/hello/some-name", requestKernel, Span.Options.Defaults)),
(Lineage.Root("/hello/some-name"), NatchezCommand.CreateSpan("call-proxy", None, Span.Options.Defaults)),
(Lineage.Root("/hello/some-name") / "call-proxy", NatchezCommand.CreateSpan("http4s-client-request", None, Span.Options.Defaults)),
(Lineage.Root("/hello/some-name") / "call-proxy" / "http4s-client-request", NatchezCommand.AskKernel(requestKernel)),
(Lineage.Root("/hello/some-name") / "call-proxy" / "http4s-client-request", NatchezCommand.Put(clientRequestTags)),
(Lineage.Root("/hello/some-name") / "call-proxy" / "http4s-client-request", NatchezCommand.Put(clientResponseTags)),
(Lineage.Root("/hello/some-name") / "call-proxy", NatchezCommand.ReleaseSpan("http4s-client-request")),
(Lineage.Root("/hello/some-name"), NatchezCommand.ReleaseSpan("call-proxy")),
(Lineage.Root("/hello/some-name"), NatchezCommand.Put(requestTags)),
(Lineage.Root("/hello/some-name"), NatchezCommand.Put(responseTags)),
(Lineage.Root, NatchezCommand.ReleaseRootSpan("/hello/some-name"))
)
}

for {
ep <- InMemory.EntryPoint.create
ep <- InMemory.EntryPoint.create[IO]
routes <- IO.pure(ep.liftT(httpRoutes[Kleisli[IO, natchez.Span[IO], *]]))
_ <- routes.orNotFound.run(request)
history <- ep.ref.get
Expand Down
4 changes: 2 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.10.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.4.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.3")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.6.4")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.12.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.9")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.17")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.2.0")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.6")

0 comments on commit 48b0898

Please sign in to comment.