diff --git a/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/span_subscriber.rb b/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/span_subscriber.rb index 71692168a..41efeb94a 100644 --- a/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/span_subscriber.rb +++ b/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/span_subscriber.rb @@ -19,13 +19,15 @@ def self.subscribe( tracer, pattern, notification_payload_transform = nil, - disallowed_notification_payload_keys = [] + disallowed_notification_payload_keys = [], + kind = nil ) subscriber = OpenTelemetry::Instrumentation::ActiveSupport::SpanSubscriber.new( name: pattern, tracer: tracer, notification_payload_transform: notification_payload_transform, - disallowed_notification_payload_keys: disallowed_notification_payload_keys + disallowed_notification_payload_keys: disallowed_notification_payload_keys, + kind: nil ) subscriber_object = ::ActiveSupport::Notifications.subscribe(pattern, subscriber) @@ -55,15 +57,16 @@ def self.subscribe( class SpanSubscriber ALWAYS_VALID_PAYLOAD_TYPES = [TrueClass, FalseClass, String, Numeric, Symbol].freeze - def initialize(name:, tracer:, notification_payload_transform: nil, disallowed_notification_payload_keys: []) + def initialize(name:, tracer:, notification_payload_transform: nil, disallowed_notification_payload_keys: [], kind: nil) @span_name = name.split('.')[0..1].reverse.join(' ').freeze @tracer = tracer @notification_payload_transform = notification_payload_transform @disallowed_notification_payload_keys = disallowed_notification_payload_keys + @kind = kind || :internal end def start(name, id, payload) - span = @tracer.start_span(@span_name, kind: :internal) + span = @tracer.start_span(@span_name, kind: @kind) token = OpenTelemetry::Context.attach( OpenTelemetry::Trace.context_with_span(span) ) diff --git a/instrumentation/active_support/test/opentelemetry/instrumentation/active_support/span_subscriber_test.rb b/instrumentation/active_support/test/opentelemetry/instrumentation/active_support/span_subscriber_test.rb index b88b27a1a..4b6b596d7 100644 --- a/instrumentation/active_support/test/opentelemetry/instrumentation/active_support/span_subscriber_test.rb +++ b/instrumentation/active_support/test/opentelemetry/instrumentation/active_support/span_subscriber_test.rb @@ -11,10 +11,12 @@ let(:tracer) { instrumentation.tracer } let(:exporter) { EXPORTER } let(:last_span) { exporter.finished_spans.last } + let(:span_kind) { nil } let(:subscriber) do OpenTelemetry::Instrumentation::ActiveSupport::SpanSubscriber.new( name: 'bar.foo', - tracer: tracer + tracer: tracer, + kind: span_kind ) end @@ -76,6 +78,7 @@ def finish(name, id, payload) ) _(last_span).wont_be_nil + _(last_span.kind).must_equal(:internal) _(last_span.attributes['string']).must_equal('keys_are_present') _(last_span.attributes['numeric_is_fine']).must_equal(1) _(last_span.attributes['boolean_okay?']).must_equal(true) @@ -182,6 +185,24 @@ def finish(name, id, payload) end end + describe 'given a span kind' do + let(:span_kind) { :client } + + it 'sets the kind on the span' do + span, token = subscriber.start('hai', 'abc', {}) + # We only use the finished attributes - could change in the future, perhaps. + subscriber.finish( + 'hai', + 'abc', + __opentelemetry_span: span, + __opentelemetry_ctx_token: token + ) + + _(last_span).wont_be_nil + _(last_span.kind).must_equal(:client) + end + end + describe 'instrument' do before do ActiveSupport::Notifications.unsubscribe('bar.foo')