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

fix(grpc): allow custom error_handler for client interceptor #3095

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/datadog/tracing/contrib/grpc/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ class Settings < Contrib::Configuration::Settings
option :error_handler do |o|
o.type :proc
o.default_proc(&Tracing::SpanOperation::Events::DEFAULT_ON_ERROR)
o.on_set do |value|
if value != Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
Datadog.logger.warn(
'The gRPC `error_handler` setting has been deprecated for removal. Please replace ' \
'it with `server_error_handler` which is explicit about only handling errors from ' \
'server interceptors. Alternatively, to handle errors from client interceptors use ' \
'the `client_error_handler` setting instead.'
)
end
end
end

option :server_error_handler do |o|
o.type :proc
o.default_proc(&Tracing::SpanOperation::Events::DEFAULT_ON_ERROR)
end

option :client_error_handler do |o|
o.type :proc
o.default_proc(&Tracing::SpanOperation::Events::DEFAULT_ON_ERROR)
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions lib/datadog/tracing/contrib/grpc/datadog_interceptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ def analytics_sample_rate
datadog_configuration[:analytics_sample_rate]
end

def error_handler
Datadog.configuration_for(self, :error_handler) || datadog_configuration[:error_handler]
end

# Allows interceptors to define settings using methods instead of `[]`
class PinAdapter
OPTIONS = Configuration::Settings.instance_methods(false).freeze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def find_host_port(call)
Datadog.logger.debug { "Could not parse host:port from #{call}: #{e}" }
nil
end

def error_handler
Datadog.configuration_for(self, :error_handler) || datadog_configuration[:client_error_handler]
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/datadog/tracing/contrib/grpc/datadog_interceptor/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ def annotate!(span, metadata, formatter)
rescue StandardError => e
Datadog.logger.debug("GRPC server trace failed: #{e}")
end

def error_handler
self_handler = Datadog.configuration_for(self, :error_handler)
return self_handler if self_handler

unless datadog_configuration.using_default?(:server_error_handler)
return datadog_configuration[:server_error_handler]
end

# As a last resort, fallback to the deprecated error_handler
# configuration option.
datadog_configuration[:error_handler]
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require 'datadog/tracing/contrib/span_attribute_schema_examples'
require 'datadog/tracing/contrib/peer_service_configuration_examples'

require_relative 'shared_examples'

require 'grpc'
require 'ddtrace'

Expand Down Expand Up @@ -150,6 +152,7 @@
end

let(:error_class) { stub_const('TestError', Class.new(StandardError)) }
let(:span_kind) { 'client' }

context 'without an error handler' do
it do
Expand All @@ -165,20 +168,25 @@
end

context 'with an error handler' do
let(:configuration_options) { { service_name: 'rspec', error_handler: error_handler } }
subject(:server) do
Datadog::Tracing::Contrib::GRPC::DatadogInterceptor::Client.new { |c| c.error_handler = error_handler }
end

let(:error_handler) do
->(span, error) { span.set_tag('custom.handler', "Got error #{error}, but ignored it") }
->(span, error) { span.set_tag('custom.handler', "Got error #{error}, but ignored it from interceptor") }
end

it do
expect { request_response }.to raise_error('test error')
it_behaves_like 'it handles the error', 'Got error test error, but ignored it from interceptor'
end

expect(span).not_to have_error
expect(span.get_tag('custom.handler')).to eq('Got error test error, but ignored it')
expect(span.get_tag('rpc.system')).to eq('grpc')
expect(span.get_tag('span.kind')).to eq('client')
context 'with an error handler defined in the configuration options' do
let(:configuration_options) { { service_name: 'rspec', client_error_handler: error_handler } }

let(:error_handler) do
->(span, error) { span.set_tag('custom.handler', "Got error #{error}, but ignored it from configuration") }
end

it_behaves_like 'it handles the error', 'Got error test error, but ignored it from configuration'
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'datadog/tracing/contrib/environment_service_name_examples'
require 'datadog/tracing/contrib/span_attribute_schema_examples'

require_relative 'shared_examples'

require 'grpc'
require 'ddtrace'

Expand Down Expand Up @@ -70,11 +72,12 @@
end

context 'with an error' do
subject(:request_response) do
let(:request_response) do
TonyCTHsu marked this conversation as resolved.
Show resolved Hide resolved
server.request_response(**keywords) { raise error_class, 'test error' }
end

let(:error_class) { stub_const('TestError', Class.new(StandardError)) }
let(:span_kind) { 'server' }

context 'without an error handler' do
it do
Expand All @@ -90,21 +93,35 @@
end

context 'with an error handler' do
let(:configuration_options) { { service_name: 'rspec', error_handler: error_handler } }
subject(:server) do
Datadog::Tracing::Contrib::GRPC::DatadogInterceptor::Server.new { |c| c.error_handler = error_handler }
end

let(:error_handler) do
lambda do |span, error|
span.set_tag('custom.handler', "Got error #{error}, but ignored it")
end
->(span, error) { span.set_tag('custom.handler', "Got error #{error}, but ignored it from interceptor") }
end

it do
expect { request_response }.to raise_error('test error')
it_behaves_like 'it handles the error', 'Got error test error, but ignored it from interceptor'
end

expect(span).to_not have_error
expect(span.get_tag('custom.handler')).to eq('Got error test error, but ignored it')
expect(span.get_tag('rpc.system')).to eq('grpc')
expect(span.get_tag('span.kind')).to eq('server')
context 'with an error handler defined in the configuration_options' do
let(:configuration_options) { { service_name: 'rspec', server_error_handler: error_handler } }

let(:error_handler) do
->(span, error) { span.set_tag('custom.handler', "Got error #{error}, but ignored it from configuration") }
end

it_behaves_like 'it handles the error', 'Got error test error, but ignored it from configuration'
end

context 'with an error handler defined with the deprecated error_handler option' do
let(:configuration_options) { { service_name: 'rspec', error_handler: error_handler } }

let(:error_handler) do
->(span, error) { span.set_tag('custom.handler', "Got error #{error}, but ignored it from deprecated config") }
end

it_behaves_like 'it handles the error', 'Got error test error, but ignored it from deprecated config'
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RSpec.shared_examples_for 'it handles the error' do |expected_error|
it do
expect { request_response }.to raise_error('test error')

expect(span).to_not have_error
expect(span.get_tag('custom.handler')).to eq(expected_error)
expect(span.get_tag('rpc.system')).to eq('grpc')
expect(span.get_tag('span.kind')).to eq(span_kind)
end
end
Loading