From 05ba58c595e74655b8332f66c984b6852091bfbc Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Wed, 19 Aug 2020 11:02:22 +0200 Subject: [PATCH 1/2] Add a single top level span for Racecar consumers Right now, Racecar consumer processes use one of two top level span names based on the mode of operation: `racecar.message` or `racecar.batch`. Because of this, the Datadog interface cannot show single message consumers and batch consumers in the same list. This change subscribes to the `main_loop` event which wraps both of these more specific events. The new span is called `racecar.consume` since that is the logical operation. --- lib/ddtrace/contrib/racecar/events.rb | 2 ++ lib/ddtrace/contrib/racecar/events/consume.rb | 27 +++++++++++++++++++ lib/ddtrace/contrib/racecar/ext.rb | 1 + 3 files changed, 30 insertions(+) create mode 100644 lib/ddtrace/contrib/racecar/events/consume.rb diff --git a/lib/ddtrace/contrib/racecar/events.rb b/lib/ddtrace/contrib/racecar/events.rb index f9abc604d3..a0a18e4255 100644 --- a/lib/ddtrace/contrib/racecar/events.rb +++ b/lib/ddtrace/contrib/racecar/events.rb @@ -1,5 +1,6 @@ require 'ddtrace/contrib/racecar/events/batch' require 'ddtrace/contrib/racecar/events/message' +require 'ddtrace/contrib/racecar/events/consume' module Datadog module Contrib @@ -7,6 +8,7 @@ module Racecar # Defines collection of instrumented Racecar events module Events ALL = [ + Events::Consume, Events::Batch, Events::Message ].freeze diff --git a/lib/ddtrace/contrib/racecar/events/consume.rb b/lib/ddtrace/contrib/racecar/events/consume.rb new file mode 100644 index 0000000000..b488ca3eb2 --- /dev/null +++ b/lib/ddtrace/contrib/racecar/events/consume.rb @@ -0,0 +1,27 @@ +require 'ddtrace/contrib/racecar/ext' +require 'ddtrace/contrib/racecar/event' + +module Datadog + module Contrib + module Racecar + module Events + # Defines instrumentation for main_loop.racecar event + module Consume + include Racecar::Event + + EVENT_NAME = 'main_loop.racecar'.freeze + + module_function + + def event_name + self::EVENT_NAME + end + + def span_name + Ext::SPAN_CONSUME + end + end + end + end + end +end diff --git a/lib/ddtrace/contrib/racecar/ext.rb b/lib/ddtrace/contrib/racecar/ext.rb index 81f2a3a36b..44d6f6f451 100644 --- a/lib/ddtrace/contrib/racecar/ext.rb +++ b/lib/ddtrace/contrib/racecar/ext.rb @@ -10,6 +10,7 @@ module Ext ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_RACECAR_ANALYTICS_SAMPLE_RATE'.freeze ENV_ANALYTICS_SAMPLE_RATE_OLD = 'DD_RACECAR_ANALYTICS_SAMPLE_RATE'.freeze SERVICE_NAME = 'racecar'.freeze + SPAN_CONSUME = 'racecar.consume'.freeze SPAN_BATCH = 'racecar.batch'.freeze SPAN_MESSAGE = 'racecar.message'.freeze TAG_CONSUMER = 'kafka.consumer'.freeze From 3b16692c66340dc39b4199363413c88f9cb4b9c8 Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Fri, 21 Aug 2020 10:02:25 +0200 Subject: [PATCH 2/2] Add a spec --- spec/ddtrace/contrib/racecar/patcher_spec.rb | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/spec/ddtrace/contrib/racecar/patcher_spec.rb b/spec/ddtrace/contrib/racecar/patcher_spec.rb index e59e54503b..5890c0a609 100644 --- a/spec/ddtrace/contrib/racecar/patcher_spec.rb +++ b/spec/ddtrace/contrib/racecar/patcher_spec.rb @@ -21,6 +21,64 @@ Datadog.registry[:racecar].reset_configuration! end + describe 'for both single and batch message processing' do + let(:consumer) { 'DummyConsumer' } + let(:payload) { { consumer_class: consumer } } + + let(:span) do + spans.select { |s| s.name == Datadog::Contrib::Racecar::Ext::SPAN_CONSUME }.first + end + + context 'that doesn\'t raise an error' do + it 'is expected to send a span' do + ActiveSupport::Notifications.instrument('main_loop.racecar', payload) + + span.tap do |span| + expect(span).to_not be nil + expect(span.service).to eq('racecar') + expect(span.name).to eq('racecar.consume') + expect(span.resource).to eq(consumer) + expect(span.get_tag('kafka.consumer')).to eq(consumer) + expect(span).to_not have_error + end + end + end + + context 'that raises an error' do + let(:error_class) { Class.new(StandardError) } + + it 'is expected to send a span' do + # Emulate failure + begin + ActiveSupport::Notifications.instrument('main_loop.racecar', payload) do + raise error_class + end + rescue error_class + nil + end + + span.tap do |span| + expect(span).to_not be nil + expect(span.service).to eq('racecar') + expect(span.name).to eq('racecar.consume') + expect(span.resource).to eq(consumer) + expect(span.get_tag('kafka.consumer')).to eq(consumer) + expect(span).to have_error + end + end + end + + it_behaves_like 'analytics for integration' do + before { ActiveSupport::Notifications.instrument('main_loop.racecar', payload) } + let(:analytics_enabled_var) { Datadog::Contrib::Racecar::Ext::ENV_ANALYTICS_ENABLED } + let(:analytics_sample_rate_var) { Datadog::Contrib::Racecar::Ext::ENV_ANALYTICS_SAMPLE_RATE } + end + + it_behaves_like 'measured span for integration', true do + before { ActiveSupport::Notifications.instrument('main_loop.racecar', payload) } + end + end + describe 'for single message processing' do let(:topic) { 'dd_trace_test_dummy' } let(:consumer) { 'DummyConsumer' }