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

Stripe: do not include path ids in metric names #2716

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 22 additions & 1 deletion lib/new_relic/agent/instrumentation/stripe_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class StripeSubscriber
EVENT_ATTRIBUTES = %i[http_status method num_retries path request_id].freeze
ATTRIBUTE_NAMESPACE = 'stripe.user_data'
ATTRIBUTE_FILTER_TYPES = %i[include exclude].freeze
PATH_PORTION_PATTERN = %r{^/([^/]+/[^/]+)(?:/|\z)}.freeze

def start_segment(event)
return unless is_execution_traced?
Expand Down Expand Up @@ -39,7 +40,27 @@ def is_execution_traced?
end

def metric_name(event)
"Stripe#{event.path}/#{event.method}"
# Grab only the first 2 items from the slash (/) delimited event path.
# These items are the API version string and the category. Grabbing
# any more of the path will result in unique method names that will
# easily grow to be too numerous to sort through in the UI and
# possibly even violate default New Relic metric count thresholds.
# See newrelic/newrelic-ruby-agent#2654 and
# newrelic/newrelic-ruby-agent#2709 for more details.
#
# In Ruby v3.4 benchmarks, using regex to get at the first two path
# elements was seen as more performant than using String#split.
#
# Regex legend:
#
# ^ = starts with
# / = a literal '/'
# () = capture
# (?:) = don't capture
# [^/]+ = 1 or more characters that are not '/'
# /|\z = a literal '/' OR the end of the string
path_portion = event.path =~ PATH_PORTION_PATTERN ? Regexp.last_match(1) : NewRelic::UNKNOWN
"Stripe/#{path_portion}/#{event.method}"
end

def add_stripe_attributes(segment, event)
Expand Down
22 changes: 22 additions & 0 deletions test/multiverse/suites/stripe/stripe_instrumentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class StripeInstrumentation < Minitest::Test
API_KEY = '123456789'
STRIPE_URL = 'Stripe/v1/customers/get'
DummyEvent = Struct.new(:path, :method)

def setup
Stripe.api_key = API_KEY
Expand Down Expand Up @@ -168,6 +169,27 @@ def test_start_segment_records_error
end
end

def test_metric_names_are_not_specific_enough_to_cause_a_cardinality_explosion
categories = %w[Trzy_kolory The_Apu_Trilogy The_Lord_of_the_Rings]
paths = ["/v1/#{categories[0]}/Niebieski",
"/v1/#{categories[0]}/Biały",
"/v1/#{categories[0]}/Czerwony",
"/v1/#{categories[1]}/Pather_Panchali",
"/v1/#{categories[1]}/Aparajito",
"/v1/#{categories[1]}/The_World_of_Apu",
"/v1/#{categories[2]}/The_Fellowship_of_the_Ring",
"/v1/#{categories[2]}/The_Two_Towers",
"/v1/#{categories[2]}/The_Return_of_the_King"]
method = 'get'

subscriber = NewRelic::Agent::Instrumentation::StripeSubscriber.new
expected = categories.map { |c| Array.new(3) { "Stripe/v1/#{c}/#{method}" } }.flatten
actual = paths.map { |p| subscriber.send(:metric_name, DummyEvent.new(p, method)) }

assert_equal expected, actual,
"Expected everything after the category in each path to be stripped away. Expected: #{expected} Actual: #{actual}"
end

def start_stripe_event
Stripe::Customer.list({limit: 3})
end
Expand Down
Loading