Skip to content

Commit

Permalink
feat: ActiveSupport user specified span kind (#1016)
Browse files Browse the repository at this point in the history
This will allow users to subscribe to notifications for Server Ingress, Messaging, or clients

See #957

Co-authored-by: Xuan <[email protected]>
  • Loading branch information
arielvalentin and xuan-cao-swi authored Jun 18, 2024
1 parent 4ba96a5 commit a9c45e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit a9c45e7

Please sign in to comment.