Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on_error settings for grape #3370

Merged
merged 13 commits into from
Jan 10, 2024
15 changes: 7 additions & 8 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ connection.get
| `distributed_tracing` | | Enables [distributed tracing](#distributed-tracing) | `true` |
| `split_by_domain` | | Uses the request domain as the service name when set to `true`. | `false` |
| `on_error` | Custom error handler invoked when a request raises an error. Provided `span` and `error` as arguments. Sets error on the span by deault. | `proc { \|span, error\| span.set_error(error) unless span.nil? }` |
| `error_status_codes` | `DD_TRACE_EXCON_ERROR_STATUS_CODES` | Range or Array of HTTP status codes that should be traced as errors.| `400...600` |
| `error_status_codes` | `DD_TRACE_EXCON_ERROR_STATUS_CODES` | Defines HTTP status codes that would be traced as errors. Value could be a range(`400...600`), or an array of ranges/integers `[403, 500...600]`. If configured with environment variable, use dash for range (`'400-599'`) and comma for adding element into an array (`'403,500-599'`) | `400...600` |
TonyCTHsu marked this conversation as resolved.
Show resolved Hide resolved


**Configuring connections to use different settings**
Expand Down Expand Up @@ -792,8 +792,8 @@ connection.get('/foo')
| `peer_service` | `DD_TRACE_FARADAY_PEER_SERVICE` | Name of external service the application connects to | `nil` |
| `distributed_tracing` | | Enables [distributed tracing](#distributed-tracing) | `true` |
| `split_by_domain` | | Uses the request domain as the service name when set to `true`. | `false` |
| `on_error` | Custom error handler invoked when a request raises an error. Provided `span` and `error` as arguments. Sets error on the span by deault. | `proc { \|span, error\| span.set_error(error) unless span.nil? }` |
| `error_status_codes` | `DD_TRACE_FARADAY_ERROR_STATUS_CODES` | Range or Array of HTTP status codes that should be traced as errors.| `400...600` |
| `on_error` | Custom error handler invoked when a request raises an error. Provided `span` and `error` as arguments. Sets error on the span by deault. | `proc { \|span, error\| span.set_error(error) unless span.nil? }` |**default: 400...600**
| `error_status_codes` | `DD_TRACE_FARADAY_ERROR_STATUS_CODES` | Defines HTTP status codes that would be traced as errors. Value could be a range(`400...600`), or an array of ranges/integers `[403, 500...600]`. If configured with environment variable, use dash for range (`'400-599'`) and comma for adding element into an array (`'403,500-599'`) | `400...600` |
TonyCTHsu marked this conversation as resolved.
Show resolved Hide resolved

### Grape

Expand Down Expand Up @@ -824,7 +824,7 @@ end
| Key | Env Var | Description | Default |
|------------------|--------------------------|-------------------------------------------------------------------------------------------------------------------------------|---------|
| `enabled` | `DD_TRACE_GRAPE_ENABLED` | Defines whether Grape should be traced. Useful for temporarily disabling tracing. `true` or `false` | `true` |
| `error_statuses` | | Defines a status code or range of status codes which should be marked as errors. `'404,405,500-599'` or `[404,405,'500-599']` | `nil` |
| `error_status_codes` | `DD_TRACE_GRAPE_ERROR_STATUS_CODES` | Defines HTTP status codes that would be traced as errors. Value could be a range(`400...600`), or an array of ranges/integers `[403, 500...600]`. If configured with environment variable, use dash for range (`'400-599'`) and comma for adding element into an array (`'403,500-599'`) | `500...600` |
TonyCTHsu marked this conversation as resolved.
Show resolved Hide resolved


### GraphQL
Expand Down Expand Up @@ -997,8 +997,7 @@ end
| `peer_service` | `DD_TRACE_HTTPRB_PEER_SERVICE` | Name of external service the application connects to | `nil` |
| `distributed_tracing` | | Enables [distributed tracing](#distributed-tracing) | `true` |
| `split_by_domain` | | Uses the request domain as the service name when set to `true`. | `false` |
| `error_status_codes` | `DD_TRACE_HTTPRB_ERROR_STATUS_CODES` | Range or Array of HTTP status codes that should be traced as errors. | `400...600` |

| `error_status_codes` | `DD_TRACE_HTTPRB_ERROR_STATUS_CODES` | Defines HTTP status codes that would be traced as errors. Value could be a range(`400...600`), or an array of ranges/integers `[403, 500...600]`. If configured with environment variable, use dash for range (`'400-599'`) and comma for adding element into an array (`'403,500-599'`) | `400...600` |
TonyCTHsu marked this conversation as resolved.
Show resolved Hide resolved

### httpclient

Expand All @@ -1025,7 +1024,7 @@ end
| `peer_service` | `DD_TRACE_HTTPCLIENT_PEER_SERVICE` | Name of external service the application connects to | `nil` |
| `distributed_tracing` | | Enables [distributed tracing](#distributed-tracing) | `true` |
| `split_by_domain` | | Uses the request domain as the service name when set to `true`. | `false` |
| `error_status_codes` | `DD_TRACE_HTTPCLIENT_ERROR_STATUS_CODES` | Range or Array of HTTP status codes that should be traced as errors. | `400...600` |
| `error_status_codes` | `DD_TRACE_HTTPCLIENT_ERROR_STATUS_CODES` | Defines HTTP status codes that would be traced as errors. Value could be a range(`400...600`), or an array of ranges/integers `[403, 500...600]`. If configured with environment variable, use dash for range (`'400-599'`) and comma for adding element into an array (`'403,500-599'`) | `400...600` |
TonyCTHsu marked this conversation as resolved.
Show resolved Hide resolved


### httpx
Expand Down Expand Up @@ -1181,7 +1180,7 @@ content = Net::HTTP.get(URI('http://127.0.0.1/index.html'))
| `peer_service` | `DD_TRACE_NET_HTTP_PEER_SERVICE` | Name of external service the application connects to | `nil` |
| `distributed_tracing` | | Enables [distributed tracing](#distributed-tracing) | `true` |
| `split_by_domain` | | Uses the request domain as the service name when set to `true`. | `false` |
| `error_status_codes` | `DD_TRACE_HTTP_ERROR_STATUS_CODES` | Range or Array of HTTP status codes that should be traced as errors. | `400...600` |
| `error_status_codes` | `DD_TRACE_HTTP_ERROR_STATUS_CODES` | Defines HTTP status codes that would be traced as errors. Value could be a range(`400...600`), or an array of ranges/integers `[403, 500...600]`. If configured with environment variable, use dash for range (`'400-599'`) and comma for adding element into an array (`'403,500-599'`) | `400...600` |
TonyCTHsu marked this conversation as resolved.
Show resolved Hide resolved

If you wish to configure each connection object individually, you may use the `Datadog.configure_onto` as it follows:

Expand Down
18 changes: 14 additions & 4 deletions lib/datadog/tracing/contrib/grape/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

require_relative '../../configuration/settings'
require_relative '../ext'
require_relative '../../status_code_matcher'
require_relative '../../status_range_matcher'
require_relative '../../status_range_env_parser'

module Datadog
module Tracing
Expand Down Expand Up @@ -31,9 +32,18 @@ class Settings < Contrib::Configuration::Settings

option :service_name

option :error_statuses, default: nil do |o|
o.setter do |new_value, _old_value|
Contrib::StatusCodeMatcher.new(new_value) unless new_value.nil?
option :on_error do |o|
o.type :proc, nilable: true
end

option :error_status_codes do |o|
o.env Ext::ENV_ERROR_STATUS_CODES
o.default 500...600
o.setter do |v|
Tracing::Contrib::StatusRangeMatcher.new(v) if v
end
o.env_parser do |v|
Tracing::Contrib::StatusRangeEnvParser.call(v) if v
end
end
end
Expand Down
32 changes: 19 additions & 13 deletions lib/datadog/tracing/contrib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def endpoint_run(name, start, finish, id, payload)
Contrib::Analytics.set_measured(span)

# catch thrown exceptions

span.set_error(payload[:exception_object]) if exception_is_error?(payload[:exception_object])
handle_error(span, payload[:exception_object]) if payload[:exception_object]

# override the current span with this notification values
span.set_tag(Ext::TAG_ROUTE_ENDPOINT, api_view) unless api_view.nil?
Expand Down Expand Up @@ -143,7 +142,7 @@ def endpoint_render(name, start, finish, id, payload)
# Measure service stats
Contrib::Analytics.set_measured(span)

span.set_error(payload[:exception_object]) if exception_is_error?(payload[:exception_object])
handle_error(span, payload[:exception_object]) if payload[:exception_object]
ensure
span.start(start)
span.finish(finish)
Expand Down Expand Up @@ -179,7 +178,7 @@ def endpoint_run_filters(name, start, finish, id, payload)
Contrib::Analytics.set_measured(span)

# catch thrown exceptions
span.set_error(payload[:exception_object]) if exception_is_error?(payload[:exception_object])
handle_error(span, payload[:exception_object]) if payload[:exception_object]

span.set_tag(Ext::TAG_FILTER_TYPE, type.to_s)
ensure
Expand All @@ -192,6 +191,22 @@ def endpoint_run_filters(name, start, finish, id, payload)

private

def handle_error(span, exception)
if exception.respond_to?('status')
span.set_error(exception) if error_status_codes.include?(exception.status)
else
on_error.call(span, exception)
end
end

def error_status_codes
datadog_configuration[:error_status_codes]
end

def on_error
datadog_configuration[:on_error] || Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
end

def api_view(api)
# If the API inherits from Grape::API in version >= 1.2.0
# then the API will be an instance and the name must be derived from the base.
Expand Down Expand Up @@ -223,15 +238,6 @@ def analytics_sample_rate
datadog_configuration[:analytics_sample_rate]
end

def exception_is_error?(exception)
matcher = datadog_configuration[:error_statuses]
return false unless exception
return true unless matcher
return true unless exception.respond_to?('status')

matcher.include?(exception.status)
end

def enabled?
Datadog.configuration.tracing.enabled && \
datadog_configuration[:enabled] == true
Expand Down
1 change: 1 addition & 0 deletions lib/datadog/tracing/contrib/grape/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Ext
ENV_ENABLED = 'DD_TRACE_GRAPE_ENABLED'
ENV_ANALYTICS_ENABLED = 'DD_TRACE_GRAPE_ANALYTICS_ENABLED'
ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_GRAPE_ANALYTICS_SAMPLE_RATE'
ENV_ERROR_STATUS_CODES = 'DD_TRACE_GRAPE_ERROR_STATUS_CODES'
SPAN_ENDPOINT_RENDER = 'grape.endpoint_render'
SPAN_ENDPOINT_RUN = 'grape.endpoint_run'
SPAN_ENDPOINT_RUN_FILTERS = 'grape.endpoint_run_filters'
Expand Down
72 changes: 0 additions & 72 deletions lib/datadog/tracing/contrib/status_code_matcher.rb

This file was deleted.

23 changes: 0 additions & 23 deletions sig/datadog/tracing/contrib/status_code_matcher.rbs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
RSpec.describe Datadog::Tracing::Contrib::Excon::Configuration::Settings do
it_behaves_like 'service name setting', 'excon'
it_behaves_like 'with on_error setting'
it_behaves_like 'with error_status_codes setting', env: 'DD_TRACE_EXCON_ERROR_STATUS_CODES'
it_behaves_like 'with error_status_codes setting', env: 'DD_TRACE_EXCON_ERROR_STATUS_CODES', default: 400...600
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
RSpec.describe Datadog::Tracing::Contrib::Faraday::Configuration::Settings do
it_behaves_like 'service name setting', 'faraday'
it_behaves_like 'with on_error setting'
it_behaves_like 'with error_status_codes setting', env: 'DD_TRACE_FARADAY_ERROR_STATUS_CODES'
it_behaves_like 'with error_status_codes setting', env: 'DD_TRACE_FARADAY_ERROR_STATUS_CODES', default: 400...600
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'datadog/tracing/contrib/grape/configuration/settings'
require 'datadog/tracing/contrib/shared_settings_examples'

RSpec.describe Datadog::Tracing::Contrib::Grape::Configuration::Settings do
it_behaves_like 'with on_error setting'
it_behaves_like 'with error_status_codes setting', env: 'DD_TRACE_GRAPE_ERROR_STATUS_CODES', default: 500...600
end
Loading
Loading