'Should I use Request-Id, X-Request-Id or X-Correlation-Id in the request header?
I am not clear which id header I should put in the request and response for the correlationship purpose.
"X-Correlation-ID" and the "X-Request-ID" are the known http header. Does it matter which one I use in the request and response?
ASP.NET Core's System.Diagnostics.DiagnosticSource looks for the "Request-Id". Is this for the Activity purpose only? Why doesn't it use the "X-Request-ID"?
If I don't use the Activity, I don't need to send that header, right?
ASP.NET Core also has the Hierarchical Request-Id (https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/HierarchicalRequestId.md ) that I like the idea. I can do something like this
var newRequestId = $"{context.Request.headers["X-Correlation-ID"]}:{CreateNewGuid()}";
OR is it better to use the Activity? I find the Activity.Current is always null. Does it need to be enabled and does it affect performance?
Solution 1:[1]
For Request-Id
, it's uniquely identifies every HTTP request involved in operation processing, and is generated on the caller side and passed to callee.
For X-Correlation-ID
, also known as a Transit ID, is a unique identifier value that is attached to requests and messages that allow reference to a particular transaction or event chain.
For every request, you should use Request-Id
, for request transaction, you should use X-Correlation-ID
.
If I don't use the Activity, I don't need to send that header, right?
For Correlation ID, in general, you don’t have to use one. But if you are designing a distributed system that incorporates message queues and asynchronous processing, you will do well to include a Correlation ID in your messages.
I find the Activity.Current is always null. Does it need to be enabled and does it affect performance?
For using Activity.Current
, you need to able ApplicationInsights
, or implement your own feature to manage activity.
- Install
Microsoft.ApplicationInsights.AspNetCore
- Configure
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().UseApplicationInsights()
- Use like
var activity = Activity.Current;
Solution 2:[2]
For X-Request-id, I simply used a unique id and boom, my issue was solved.
In Axios.
const options = {
method: 'POST',
url: 'myurl',
headers: {
'content-disposition': 'always',
'X-REQUEST-ID': Math.random(200)
},
data: {
amount: { currency: 'USD', number: 1 },
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Richard Fearn |
Solution 2 | Zia Ullah |