简体   繁体   English

如何在 Rails 应用程序中将法拉第请求正文添加到 Datadog 跟踪中

[英]How to add Faraday request body into Datadog tracing in a Rails app

I'm trying to configure Faraday tracing in aRails application using Datadog.我正在尝试使用 Datadog 在 aRails 应用程序中配置法拉第跟踪。

I've set up the Faraday -> Datadog connection:我已经建立了 Faraday -> Datadog 连接:

require 'faraday'
require 'ddtrace'

Datadog.configure do |c|
  c.use :faraday
end

Faraday.post("http://httpstat.us/200", {foo: 1, bar: 2}.to_json)
Faraday.get("http://httpstat.us/201?foo=1&bar=2")

It works well, the requests are being logged to Datadog.它运行良好,请求被记录到 Datadog。

But those logs do not contain any request parameters, nevertheless GET or POST.但是这些日志不包含任何请求参数,但 GET 或 POST。

GET 请求日志POST 请求日志

Any adviсe on how to get the request params/body logged to Datadog?关于如何将请求参数/正文记录到 Datadog 的任何建议?

So, by default the only things sent to Datadog from Faraday as part of the span in terms of the HTTP request are are:因此,默认情况下,就 HTTP 请求而言,作为跨度的一部分,从 Faraday 发送到 Datadog 的唯一内容是:

      span.set_tag(Datadog::Ext::HTTP::URL, env[:url].path)
      span.set_tag(Datadog::Ext::HTTP::METHOD, env[:method].to_s.upcase)
      span.set_tag(Datadog::Ext::NET::TARGET_HOST, env[:url].host)
      span.set_tag(Datadog::Ext::NET::TARGET_PORT, env[:url].port)

Source: https://github.com/DataDog/dd-trace-rb/blob/e391d2eb64d3c6151a4bdd2710c6a8c7c1d57457/lib/ddtrace/contrib/faraday/middleware.rb#L54资料来源: https://github.com/DataDog/dd-trace-rb/blob/e391d2eb64d3c6151a4bdd2710c6a8c7c1d57457/lib/ddtrace/contrib/faraday/middleware.rb#L54

The body of the request is not set in the http part of the span by default, only the URL, HTTP method, host and port are.请求的body默认没有设置在span的http部分,只有URL,HTTP方法,host和port是。

However, with manual instrumentation you can add anything you want to the span, so you could write an extension or monkey-patch to the Faraday middleware to add the body and parameters to the span:但是,通过手动检测,您可以将任何您想要的内容添加到跨度,因此您可以为法拉第中间件编写扩展或猴子补丁,以将主体和参数添加到跨度:

span.set_tag("http.body", env[:body])
span.set_tag("http.params", env[:params])

An example monkey patch:猴子补丁示例:

require 'faraday'
require 'ddtrace'
require 'ddtrace/contrib/faraday/middleware'

module FaradayExtension
  private
  def annotate!(span, env, options)
    # monkey patch to add body to span
    span.set_tag("http.body", env[:body]) unless env[:body].to_s.strip.empty?
    span.set_tag("http.query", env[:url].query) if env[:url].query
    super
  end
end

Datadog::Contrib::Faraday::Middleware.prepend(FaradayExtension)

Datadog.configure do |c|
  c.use :faraday
end

Faraday.post("http://httpstat.us/200", {foo: 1, bar: 2}.to_json)
Faraday.get("http://httpstat.us/201?foo=1&bar=2")

This worked for me in my testing:这在我的测试中对我有用:

邮政 得到

NB: I am a Datadog employee, but not on the engineering team, just wanted to be transparent!注意:我是 Datadog 的员工,但不在工程团队,只是想保持透明!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM