Skip to content

Commit

Permalink
Add http tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
th3n3rd committed Mar 25, 2024
1 parent 14a5338 commit 712d7f7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/main/kotlin/com/example/App.kt
Expand Up @@ -3,6 +3,8 @@ package com.example
import com.example.common.infra.AppRequestContext
import com.example.common.infra.AppRequestContext.authenticatedPlayerIdLens
import com.example.common.infra.DatabaseContext
import com.example.common.infra.NameEvents
import com.example.common.infra.ServerTracing
import com.example.gameplay.Games
import com.example.gameplay.Secrets
import com.example.gameplay.StartNewGame
Expand All @@ -22,6 +24,7 @@ import com.example.player.infra.RegisterNewPlayerApi
import org.http4k.cloudnative.env.Environment.Companion.ENV
import org.http4k.core.HttpHandler
import org.http4k.core.then
import org.http4k.events.Events
import org.http4k.filter.DebuggingFilters.PrintRequest
import org.http4k.routing.routes
import org.http4k.server.SunHttp
Expand All @@ -32,12 +35,14 @@ object App {
players: RegisteredPlayers,
games: Games,
secrets: Secrets,
passwordEncoder: PasswordEncoder
passwordEncoder: PasswordEncoder,
events: Events = {}
): HttpHandler {
val authenticatedPlayerId = authenticatedPlayerIdLens()
val authenticatePlayer = AuthenticatePlayer(players, passwordEncoder, authenticatedPlayerId)

return AppRequestContext()
return ServerTracing(NameEvents("app", events))
.then(AppRequestContext())
.then(
routes(
RegisterNewPlayerApi(RegisterNewPlayer(players, passwordEncoder)),
Expand Down
28 changes: 28 additions & 0 deletions src/main/kotlin/com/example/common/infra/Tracing.kt
@@ -0,0 +1,28 @@
package com.example.common.infra

import org.http4k.core.then
import org.http4k.events.EventFilters.AddServiceName
import org.http4k.events.EventFilters.AddZipkinTraces
import org.http4k.events.Events
import org.http4k.events.HttpEvent.Incoming
import org.http4k.events.HttpEvent.Outgoing
import org.http4k.events.then
import org.http4k.filter.ClientFilters
import org.http4k.filter.ResponseFilters.ReportHttpTransaction
import org.http4k.filter.ServerFilters

object NameEvents {
operator fun invoke(actorName: String, events: Events) = AddZipkinTraces()
.then(AddServiceName(actorName))
.then(events)
}

object ClientTracing {
operator fun invoke(events: Events) = ClientFilters.RequestTracing()
.then(ReportHttpTransaction { events(Outgoing(it)) })
}

object ServerTracing {
operator fun invoke(events: Events) = ServerFilters.RequestTracing()
.then(ReportHttpTransaction { events(Incoming(it)) })
}
9 changes: 6 additions & 3 deletions src/test/kotlin/com/example/JourneyTests.kt
@@ -1,6 +1,7 @@
package com.example

import com.example.common.infra.DatabaseContext
import com.example.common.infra.RecordTraces
import com.example.common.infra.RunDatabaseMigrations
import com.example.gameplay.Games
import com.example.gameplay.Secrets
Expand All @@ -17,7 +18,7 @@ import org.http4k.server.SunHttp
import org.http4k.server.asServer
import org.junit.jupiter.api.Test

class JourneyTests {
class JourneyTests: RecordTraces() {

init {
RunDatabaseMigrations(ENV)
Expand All @@ -29,15 +30,17 @@ class JourneyTests {
players = RegisteredPlayers.Database(database),
games = Games.Database(database),
secrets = Secrets.Rotating(listOf("correct")),
passwordEncoder = PasswordEncoder.Argon2()
passwordEncoder = PasswordEncoder.Argon2(),
events = events
).debug()

private val appServer = app.asServer(SunHttp(0)).start()

private val player = Player(
baseUri = Uri.of("http://localhost:${appServer.port()}"),
username = "player-1",
password = "player-1"
password = "player-1",
events = events
)

@Test
Expand Down
11 changes: 8 additions & 3 deletions src/test/kotlin/com/example/Player.kt
@@ -1,8 +1,9 @@
package com.example

import com.example.common.infra.ClientTracing
import com.example.common.infra.NameEvents
import com.example.gameplay.GameId
import io.kotest.matchers.shouldBe
import java.util.*
import org.http4k.client.JavaHttpClient
import org.http4k.core.Body
import org.http4k.core.Method.GET
Expand All @@ -11,22 +12,26 @@ import org.http4k.core.Request
import org.http4k.core.Uri
import org.http4k.core.then
import org.http4k.core.with
import org.http4k.events.Events
import org.http4k.filter.ClientFilters.BasicAuth
import org.http4k.filter.ClientFilters.SetBaseUriFrom
import org.http4k.filter.debug
import org.http4k.format.Jackson.auto
import java.util.*

class Player(
baseUri: Uri,
username: String = "",
password: String = ""
password: String = "",
events: Events
) {
private val credentialsLens = Body.auto<Credentials>().toLens()
private val gameStartedLens = Body.auto<GameStarted>().toLens()
private val gameDetailsLens = Body.auto<GameDetails>().toLens()
private val guessLens = Body.auto<Guess>().toLens()

private val client = SetBaseUriFrom(baseUri)
private val client = ClientTracing(NameEvents("player", events))
.then(SetBaseUriFrom(baseUri))
.then(BasicAuth(user = username, password = password))
.then(JavaHttpClient())
.debug()
Expand Down
27 changes: 27 additions & 0 deletions src/test/kotlin/com/example/common/infra/RecordTraces.kt
@@ -0,0 +1,27 @@
package com.example.common.infra

import org.http4k.tracing.Actor
import org.http4k.tracing.ActorResolver
import org.http4k.tracing.ActorType
import org.http4k.tracing.TraceRenderPersistence
import org.http4k.tracing.junit.ReportingMode.Companion.Always
import org.http4k.tracing.junit.TracerBulletEvents
import org.http4k.tracing.persistence.FileSystem
import org.http4k.tracing.renderer.PumlSequenceDiagram
import org.http4k.tracing.tracer.HttpTracer
import org.junit.jupiter.api.extension.RegisterExtension
import java.io.File

private val actor = ActorResolver {
Actor(it.metadata["service"].toString(), ActorType.System)
}

abstract class RecordTraces {
@RegisterExtension
val events = TracerBulletEvents(
tracers = listOf(HttpTracer(actor)),
renderers = listOf(PumlSequenceDiagram),
traceRenderPersistence = TraceRenderPersistence.FileSystem(File("target/tracing")),
reportingMode = Always
)
}

0 comments on commit 712d7f7

Please sign in to comment.