Skip to content

Commit a9c45e7

Browse files
feat: ActiveSupport user specified span kind (#1016)
This will allow users to subscribe to notifications for Server Ingress, Messaging, or clients See #957 Co-authored-by: Xuan <[email protected]>
1 parent 4ba96a5 commit a9c45e7

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/span_subscriber.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ def self.subscribe(
1919
tracer,
2020
pattern,
2121
notification_payload_transform = nil,
22-
disallowed_notification_payload_keys = []
22+
disallowed_notification_payload_keys = [],
23+
kind = nil
2324
)
2425
subscriber = OpenTelemetry::Instrumentation::ActiveSupport::SpanSubscriber.new(
2526
name: pattern,
2627
tracer: tracer,
2728
notification_payload_transform: notification_payload_transform,
28-
disallowed_notification_payload_keys: disallowed_notification_payload_keys
29+
disallowed_notification_payload_keys: disallowed_notification_payload_keys,
30+
kind: nil
2931
)
3032

3133
subscriber_object = ::ActiveSupport::Notifications.subscribe(pattern, subscriber)
@@ -55,15 +57,16 @@ def self.subscribe(
5557
class SpanSubscriber
5658
ALWAYS_VALID_PAYLOAD_TYPES = [TrueClass, FalseClass, String, Numeric, Symbol].freeze
5759

58-
def initialize(name:, tracer:, notification_payload_transform: nil, disallowed_notification_payload_keys: [])
60+
def initialize(name:, tracer:, notification_payload_transform: nil, disallowed_notification_payload_keys: [], kind: nil)
5961
@span_name = name.split('.')[0..1].reverse.join(' ').freeze
6062
@tracer = tracer
6163
@notification_payload_transform = notification_payload_transform
6264
@disallowed_notification_payload_keys = disallowed_notification_payload_keys
65+
@kind = kind || :internal
6366
end
6467

6568
def start(name, id, payload)
66-
span = @tracer.start_span(@span_name, kind: :internal)
69+
span = @tracer.start_span(@span_name, kind: @kind)
6770
token = OpenTelemetry::Context.attach(
6871
OpenTelemetry::Trace.context_with_span(span)
6972
)

instrumentation/active_support/test/opentelemetry/instrumentation/active_support/span_subscriber_test.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
let(:tracer) { instrumentation.tracer }
1212
let(:exporter) { EXPORTER }
1313
let(:last_span) { exporter.finished_spans.last }
14+
let(:span_kind) { nil }
1415
let(:subscriber) do
1516
OpenTelemetry::Instrumentation::ActiveSupport::SpanSubscriber.new(
1617
name: 'bar.foo',
17-
tracer: tracer
18+
tracer: tracer,
19+
kind: span_kind
1820
)
1921
end
2022

@@ -76,6 +78,7 @@ def finish(name, id, payload)
7678
)
7779

7880
_(last_span).wont_be_nil
81+
_(last_span.kind).must_equal(:internal)
7982
_(last_span.attributes['string']).must_equal('keys_are_present')
8083
_(last_span.attributes['numeric_is_fine']).must_equal(1)
8184
_(last_span.attributes['boolean_okay?']).must_equal(true)
@@ -182,6 +185,24 @@ def finish(name, id, payload)
182185
end
183186
end
184187

188+
describe 'given a span kind' do
189+
let(:span_kind) { :client }
190+
191+
it 'sets the kind on the span' do
192+
span, token = subscriber.start('hai', 'abc', {})
193+
# We only use the finished attributes - could change in the future, perhaps.
194+
subscriber.finish(
195+
'hai',
196+
'abc',
197+
__opentelemetry_span: span,
198+
__opentelemetry_ctx_token: token
199+
)
200+
201+
_(last_span).wont_be_nil
202+
_(last_span.kind).must_equal(:client)
203+
end
204+
end
205+
185206
describe 'instrument' do
186207
before do
187208
ActiveSupport::Notifications.unsubscribe('bar.foo')

0 commit comments

Comments
 (0)