class NewRelic::Agent::Instrumentation::StripeSubscriber

Constants

ATTRIBUTE_FILTER_TYPES
ATTRIBUTE_NAMESPACE
DEFAULT_DESTINATIONS
EVENT_ATTRIBUTES

Public Instance Methods

finish_segment(event) click to toggle source
# File lib/new_relic/agent/instrumentation/stripe_subscriber.rb, line 23
def finish_segment(event)
  return unless is_execution_traced?

  segment = remove_and_return_nr_segment(event)
  add_stripe_attributes(segment, event)
  add_custom_attributes(segment, event)
rescue => e
  NewRelic::Agent.logger.error("Error finishing New Relic Stripe segment: #{e}")
ensure
  segment&.finish
end
start_segment(event) click to toggle source
# File lib/new_relic/agent/instrumentation/stripe_subscriber.rb, line 14
def start_segment(event)
  return unless is_execution_traced?

  segment = NewRelic::Agent::Tracer.start_segment(name: metric_name(event))
  event.user_data[:newrelic_segment] = segment
rescue => e
  NewRelic::Agent.logger.error("Error starting New Relic Stripe segment: #{e}")
end

Private Instance Methods

add_custom_attributes(segment, event) click to toggle source
# File lib/new_relic/agent/instrumentation/stripe_subscriber.rb, line 51
def add_custom_attributes(segment, event)
  return if NewRelic::Agent.config[:'stripe.user_data.include'].empty?

  filtered_attributes = NewRelic::Agent::AttributePreFiltering.pre_filter_hash(event.user_data, nr_attribute_options)
  filtered_attributes.each do |key, value|
    segment.add_agent_attribute("stripe_user_data_#{key}", value, DEFAULT_DESTINATIONS)
  end
end
add_stripe_attributes(segment, event) click to toggle source
# File lib/new_relic/agent/instrumentation/stripe_subscriber.rb, line 45
def add_stripe_attributes(segment, event)
  EVENT_ATTRIBUTES.each do |attribute|
    segment.add_agent_attribute("stripe_#{attribute}", event.send(attribute), DEFAULT_DESTINATIONS)
  end
end
is_execution_traced?() click to toggle source
# File lib/new_relic/agent/instrumentation/stripe_subscriber.rb, line 37
def is_execution_traced?
  NewRelic::Agent::Tracer.state.is_execution_traced?
end
metric_name(event) click to toggle source
# File lib/new_relic/agent/instrumentation/stripe_subscriber.rb, line 41
def metric_name(event)
  "Stripe#{event.path}/#{event.method}"
end
nr_attribute_options() click to toggle source
# File lib/new_relic/agent/instrumentation/stripe_subscriber.rb, line 60
def nr_attribute_options
  ATTRIBUTE_FILTER_TYPES.each_with_object({}) do |type, opts|
    pattern =
      NewRelic::Agent::AttributePreFiltering.formulate_regexp_union(:"#{ATTRIBUTE_NAMESPACE}.#{type}")
    opts[type] = pattern if pattern
  end
end
remove_and_return_nr_segment(event) click to toggle source
# File lib/new_relic/agent/instrumentation/stripe_subscriber.rb, line 68
def remove_and_return_nr_segment(event)
  segment = event.user_data[:newrelic_segment]
  event.user_data.delete(:newrelic_segment)

  segment
end