Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Request-Id, X-Request-Id or X-Correlation-Id in the request header?

Tags:

asp.net-core

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?

like image 725
kklo Avatar asked May 09 '19 23:05

kklo


People also ask

How do you pass a correlation ID in header?

Request and Response Client Application/User can pass the correlation ID for each RESTful API call through the HTTP header name: X-Correlation-ID. This Correlation ID will get returned in the response header, irrespective of the response status (Success or Fail).

What is request ID and correlation ID?

When the caller processes the reply, it uses the request identifier to correlate the request with the reply. This is called a correlation identifier because of the way the caller uses the identifier to correlate each reply with the request. A correlation ID is usually put in the header of a message.

What is the X-Request-ID HTTP header?

All POST, PUT, and PATCH HTTP requests should contain a unique X-Request-Id header which is used to ensure idempotent message processing in case of a retry. If you make it a random string, unique per request, it won't infringe on your privacy, nor enable tracking.

What is correlation ID in HTTP header?

A Correlation ID can be defined as an 'identifier value attached to messages and request headers which allows referencing a particular transaction or event'.


1 Answers

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.

  1. Install Microsoft.ApplicationInsights.AspNetCore
  2. Configure WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().UseApplicationInsights()
  3. Use like var activity = Activity.Current;
like image 166
Edward Avatar answered Oct 04 '22 17:10

Edward