CallId

The CallId feature allows to identify a request/call and can work along the CallLogging feature.

Generating Call IDs

  1. install(CallId) {
  2. // 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.
  3. retrieve { // call: ApplicationCall ->
  4. call.request.header(HttpHeaders.XRequestId)
  5. }
  6. // If can't retrieve a callId from the ApplicationCall, it will try the generate blocks coalescing until one of them is not null.
  7. val counter = atomic(0)
  8. generate { "generated-call-id-${counter.getAndIncrement()}" }
  9. // Once a callId is generated, this optional function is called to verify if the retrieved or generated callId String is valid.
  10. verify { callId: String ->
  11. callId.isNotEmpty()
  12. }
  13. // Allows to process the call to modify headers or generate a request from the callId
  14. reply { call: ApplicationCall, callId: String ->
  15. }
  16. // Retrieve the callId from a headerName
  17. retrieveFromHeader(headerName: String)
  18. // Automatically updates the response with the callId in the specified headerName
  19. replyToHeader(headerName: String)
  20. // Combines both: retrieveFromHeader and replyToHeader in one single call
  21. header(headerName: String)
  22. }

Extending CallLogging

The CallId feature includes a callIdMdc extension method to be used when configuring the CallLogging.It allows to associate the callId to the specified key to be put in the MDC context.

  1. install(CallLogging) {
  2. callIdMdc("mdc-call-id")
  3. }