Skip to content

How to use the same trace id in multiple api calls

I have an api that has several methods. The workflow of the api call is in general like this: login, callX, … and logout. Each api method is doing calls to subsystems. The trace id with sleuth works for e.g. the login call and all it’s subsystems. Now i would like to have a trace id that is the same for the hole workflow (like a session id). how do i achive this. does a predefined tag exists like the spanid and traceid?


Check the terminology in the docs.

A trace is just a tree of spans (there is a root) so theoretically you can have a “session span” that starts with login and ends with logout but you need to:

  • create it before you call the login endpoint
  • end it after you called logout
  • keep it somewhere in between
  • handle the case if logout is never called

As you can see, this can be very problematic, also, your trace won’t be completed until logout, what do you expect to see on the UI in the meantime?

Because of these, a trace usually (in case of HTTP) is in “request scope”. This means that a new “root span” is created for every requests that the client made and subsequent spans are created from the root for each call.

This does not mean that you can’t attach some sort of a “workflowId” to your spans that belong to the same workflow (please never attach the real sessionId because that should be kept as a secret).

Please check the Baggage and the Tag sections of the docs. You can create a “workflowId” that you can propagate to all of the services (using a baggage) and you can attach this as a tag to your spans so that you can query them on the ui and see all the traces that belong to the same workflow.