Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ActiveSupport user specified span kind #1016

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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