All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.tracing.tracer.HttpTracer.kt Maven / Gradle / Ivy

There is a newer version: 5.41.0.0
Show newest version
package org.http4k.tracing.tracer

import org.http4k.events.HttpEvent
import org.http4k.events.MetadataEvent
import org.http4k.tracing.Actor
import org.http4k.tracing.ActorResolver
import org.http4k.tracing.ActorType.System
import org.http4k.tracing.EventNode
import org.http4k.tracing.RequestResponse
import org.http4k.tracing.Trace
import org.http4k.tracing.Tracer
import org.http4k.tracing.X_HTTP4K_INCOMING_EVENT

fun HttpTracer(actorFrom: ActorResolver) = Tracer { eventNode, tracer ->
    eventNode
        .takeIf { it.event.event is HttpEvent.Outgoing }
        ?.toTrace(actorFrom, tracer)
        ?.let(::listOf) ?: emptyList()
}

private fun EventNode.toTrace(actorFrom: ActorResolver, tracer: Tracer): Trace {
    val parentEvent = event.event as HttpEvent.Outgoing

    return RequestResponse(
        actorFrom(event),
        (event.metadata[X_HTTP4K_INCOMING_EVENT] as? MetadataEvent)
            ?.let(actorFrom)
            ?: Actor(parentEvent.uri.host, System),
        parentEvent.method.name + " " + parentEvent.xUriTemplate,
        parentEvent.status.toString(),
        children.flatMap { tracer(it, tracer) }
    )
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy