CallId

note

CallId - 图1

This help topic is in development and will be updated in the future.

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. }

7. // If can't retrieve a callId from the ApplicationCall, it will try the generate blocks coalescing until one of them is not null.
8. val counter = atomic(0)
9. generate { "generated-call-id-${counter.getAndIncrement()}" }

11. // Once a callId is generated, this optional function is called to verify if the retrieved or generated callId String is valid.
12. verify { callId: String ->
13. callId.isNotEmpty()
14. }

16. // Allows to process the call to modify headers or generate a request from the callId
17. reply { call: ApplicationCall, callId: String ->

19. }

21. // Retrieve the callId from a headerName
22. retrieveFromHeader(headerName: String)

24. // Automatically updates the response with the callId in the specified headerName
25. replyToHeader(headerName: String)

27. // Combines both: retrieveFromHeader and replyToHeader in one single call
28. header(headerName: String)
29. }

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. }