Skip to content

Commit

Permalink
feat: ActiveSupport user specified span kind
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 open-telemetry#957
  • Loading branch information
arielvalentin committed Jun 16, 2024
1 parent 2230403 commit c7a5829
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 c7a5829

Please sign in to comment.