我应该在请求标头中使用Request-Id,X-Request-Id或X-Correlation-Id吗? [英] Should I use Request-Id, X-Request-Id or X-Correlation-Id in the request header?
问题描述
我不清楚出于关联性的目的应在请求和响应中放入哪个id头.
I am not clear which id header I should put in the request and response for the correlationship purpose.
"X-Correlation-ID"和"X-Request-ID"是已知的http标头.在请求和响应中使用哪一个无关紧要?
"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的System.Diagnostics.DiagnosticSource查找请求ID".这仅用于活动目的吗?为什么不使用"X-Request-ID"?
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还具有分层请求ID( https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/HierarchicalRequestId.md ),我喜欢这个主意.我可以做这样的事情
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()}";
或者使用活动"更好吗?我发现Activity.Current始终为null.是否需要启用它并且会影响性能?
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?
推荐答案
对于 Request-Id
,它唯一标识操作处理中涉及的每个HTTP请求,并在调用方生成并传递给被呼叫者.
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.
对于 X-Correlation-ID
,也称为传输ID",是唯一的标识符值,附加到允许引用特定事务或事件链的请求和消息上.
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.
对于每个请求,应使用 Request-Id
,对于请求事务,应使用 X-Correlation-ID
.
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?
通常,对于相关ID",您不必使用一个.但是,如果您要设计一个包含消息队列和异步处理的分布式系统,那么最好在消息中包含一个Correlation ID.
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.
我发现Activity.Current始终为null.是否需要启用它并且会影响性能?
I find the Activity.Current is always null. Does it need to be enabled and does it affect performance?
要使用 Activity.Current
,您需要能够 ApplicationInsights
,或实现自己的功能来管理活动.
For using Activity.Current
, you need to able ApplicationInsights
, or implement your own feature to manage activity.
- 安装
Microsoft.ApplicationInsights.AspNetCore
- 配置
WebHost.CreateDefaultBuilder(args).UseStartup< Startup>().UseApplicationInsights()
- 使用类似
var activity = Activity.Current;
这篇关于我应该在请求标头中使用Request-Id,X-Request-Id或X-Correlation-Id吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!