How to filter Request with ddtrace on Rails?
Many Web Application are use monitoring Apps like datadog, CloudWatch, Mackerel and so on. When using a monitoring application, there may be cases where you want to exclude specific HTTP requests from being monitored. So I try to describe how to filter Request with ddtrace on Rails in this article.
Environments
Rails 6.1.4
Ruby 2.7.6
ddtrace 1.4.1
Implement
You need Edit config/initializers/datadog.rb.
Datadog::Pipeline.before_flush(
Datadog::Pipeline::SpanFilter.new { |span| span.resource =~ /HogeController/ },
)
For example, If You want to remove Healthcheck API…
Datadog::Pipeline.before_flush(
Datadog::Pipeline::SpanFilter.new { |span| span.resource =~ /HealthcheckController/ },
)
Several Request Filtering
If you want to remove Several Request, You edit this
Datadog::Pipeline.before_flush(
Datadog::Pipeline::SpanFilter.new { |span| span.resource =~ /HealthcheckController|PingController/ },
)
Check this code in Regular expressions.
# If match return 0
"HealthcheckController" =~ /HealthcheckController|PingController/
=> 0
# If not match return nil
"HealthcheckController" =~ /PingController/
=> nil
"HealthcheckController" =~ /HealthcheckController/
=> 0
Referenced Articles
Thank you for writing!