https://kotlinlang.org logo
Title
l

Luke Rohde

01/27/2020, 6:59 PM
it’s a bit annoying though that it has to be inserted in this way, since the AuthenticatePhase only exists within routes of the application that are authenticated, so you end up repeating yourself
h

hdarritchon

01/27/2020, 7:22 PM
Hi, may be you can try something like `
val phase = PipelinePhase("doSomeLogging")
pipeline.insertPhaseAfter(ApplicationCallPipeline.Features,phase)
` With a phase linked after Features, I guess Authentication is done so you can have all you need ? Feel free to have a look at documentation for more details pipeline phase sequence (https://ktor.io/advanced/pipeline.html#ApplicationCallPipeline)
l

Luke Rohde

01/27/2020, 7:23 PM
the authentication phase does the same- this does not give the desired behavior
that is, the authentication phase also binds to just after Features
so the authenticated principal is not present when you try to get it with `
call.authentication.principal<Principal>()
h

hdarritchon

01/27/2020, 7:25 PM
Try
val phase = PipelinePhase("doSomeLogging")    pipeline.insertPhaseBefore(ApplicationCallPipeline.Call,phase)
May be I don’t understand your point, sorry 😞
l

Luke Rohde

01/27/2020, 7:25 PM
this also did not seem to produce the desired behavior
h

hdarritchon

01/27/2020, 7:30 PM
What I did myself, it’s to use a method around my routes such as : `
fun Route.routeEnrichedWithLogData(callback: Route.() -> Unit): Route =
    routeWithAction(callback) {
        proceed()
        MDC.put("principal", this.principal<UserIdPrincipal>()?.name ?: "unknown")
    }
` This way I can log my Principal.
The process() call do the job and after I put all my logging.
install(Routing) {
    routeEnrichedWithLogData {
                val registry = feature(DropwizardMetrics).registry
                healthEndPoints()
                storageHealthEndPoints()
                metricsEndPoints(registry)
...
Does it fit to your need ?
l

Luke Rohde

01/27/2020, 7:32 PM
that’s essentially what i’ve got, yeah
thanks!
👍 1