ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
12.71k stars 1.04k forks source link

CallLogging MDC with sessions: Application feature Sessions is not installed #1368

Closed AndreasVolkmann closed 1 year ago

AndreasVolkmann commented 4 years ago

Ktor Version and Engine Used (client or server and name) 1.2.4, server, [cio, serialization, auth, locations, client-cio]

Describe the bug Just tried out the CallLogging MDC feature, but as soon as I try to get a value from sessions, my server becomes unresponsive. The call just takes forever and doesn't return. No log shows up. Then I tried the CallId feature with the same logic and this time it throws: io.ktor.application.MissingApplicationFeatureException: Application feature Sessions is not installed.

Once I switched to netty, it shows the same exception when using the CallLogging feature.

To Reproduce Steps to reproduce the behavior:

  1. Write the following
    
    class OAuthSession(val userId: Int)

fun main() { embeddedServer(CIO, 5000) { install(CallLogging) { mdc("test") { it.sessions.get()?.userId?.toString() } } install(Sessions) { cookie("oauthSampleSessionId") { val secretSignKey = hex("000102030405060708090a0b0c0d0e0f") transform(SessionTransportTransformerMessageAuthentication(secretSignKey)) } } install(Routing) { get { call.respond("Ok") } } }.start(true) }



2. Start the server and call it via `localhost:5000`.
3. In CIO, no error is shown. With netty, exception is thrown. 

**Expected behavior**
The session should be retrieved and used for mdc / call id. 
cy6erGn0m commented 4 years ago

The first issue is that it reports the missing feature that is not true. This is just wrong diagnostics. However, the root cause of your issue is that MDC captures too early so sessions are not yet configured for this call.

And even worse, we can't fix that easily because it's not clear what should be first. Let me explain the problem: what should happen if a session extraction is failed? Should error message be logged with MDC? How do we get MDC with no sessions?

AndreasVolkmann commented 4 years ago

@cy6erGn0m Thanks for the explanation. For me this functionality is not a must. Whatever the outcome, it should probably be reflected in the docs.

oleg-larshin commented 4 years ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.