Skip to content

Commit

Permalink
feat: Enable support for Regexp patterns when subscribing to Active…
Browse files Browse the repository at this point in the history
… Support's instrumentation Events (#1296)
  • Loading branch information
erichmachado authored Dec 19, 2024
1 parent e3975ae commit e745deb
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.subscribe(
span_name_formatter: nil
)
subscriber = OpenTelemetry::Instrumentation::ActiveSupport::SpanSubscriber.new(
name: pattern,
pattern: pattern,
tracer: tracer,
notification_payload_transform: notification_payload_transform,
disallowed_notification_payload_keys: disallowed_notification_payload_keys,
Expand Down Expand Up @@ -64,17 +64,18 @@ class SpanSubscriber
ALWAYS_VALID_PAYLOAD_TYPES = [TrueClass, FalseClass, String, Numeric, Symbol].freeze

# rubocop:disable Metrics/ParameterLists
def initialize(name:, tracer:, notification_payload_transform: nil, disallowed_notification_payload_keys: nil, kind: nil, span_name_formatter: nil)
@span_name = safe_span_name_for(span_name_formatter, name).dup.freeze
def initialize(pattern:, tracer:, notification_payload_transform: nil, disallowed_notification_payload_keys: nil, kind: nil, span_name_formatter: nil)
@pattern = pattern
@tracer = tracer
@notification_payload_transform = notification_payload_transform
@disallowed_notification_payload_keys = Array(disallowed_notification_payload_keys)
@kind = kind || :internal
@span_name_formatter = span_name_formatter
end
# rubocop:enable Metrics/ParameterLists

def start(name, id, payload)
span = @tracer.start_span(@span_name, kind: @kind)
span = @tracer.start_span(safe_span_name_for(name), kind: @kind)
token = OpenTelemetry::Context.attach(
OpenTelemetry::Trace.context_with_span(span)
)
Expand Down Expand Up @@ -140,8 +141,8 @@ def sanitized_value(value)
# Helper method to try an shield the span name formatter from errors
#
# It wraps the user supplied formatter in a rescue block and returns the original name if a StandardError is raised by the formatter
def safe_span_name_for(span_name_formatter, name)
span_name_formatter&.call(name) || name
def safe_span_name_for(name)
@span_name_formatter&.call(name) || name
rescue StandardError => e
OpenTelemetry.handle_error(exception: e, message: 'Error calling span_name_formatter. Using default span name.')
name
Expand Down
Loading

0 comments on commit e745deb

Please sign in to comment.