CallId

Estimated reading time: 1 minute

CallId Featureを使うと、リクエスト/コールを識別ができるようになり、 CallLogging Featureと一緒に動作させることができます。

Call IDの生成

install(CallId) {
    // Tries to retrieve a callId from an ApplicationCall. You can add several retrievers and all will be executed coalescing until one of them is not null.  
    retrieve { // call: ApplicationCall ->
        call.request.header(HttpHeaders.XRequestId)
    }
    
    // If can't retrieve a callId from the ApplicationCall, it will try the generate blocks coalescing until one of them is not null.
    val counter = atomic(0)
    generate { "generated-call-id-${counter.getAndIncrement()}" }
    
    // Once a callId is generated, this optional function is called to verify if the retrieved or generated callId String is valid. 
    verify { callId: String ->
        callId.isNotEmpty()
    }
    
    // Allows to process the call to modify headers or generate a request from the callId
    reply { call: ApplicationCall, callId: String ->
    
    }

    // Retrieve the callId from a headerName
    retrieveFromHeader(headerName: String)
    
    // Automatically updates the response with the callId in the specified headerName
    replyToHeader(headerName: String)
    
    // Combines both: retrieveFromHeader and replyToHeader in one single call
    header(headerName: String)
}

CallLoggingの拡張

CallId FeatureはcallIdMdc拡張関数を含んでおり、この関数はCallLoggingの設定を行う際に利用されます。 この関数はcallIdを指定したキーに紐付けMDCコンテキストにputします。

install(CallLogging) {
    callIdMdc("mdc-call-id")
}