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

Start tracking StripeClient#request usage #1396

Merged
merged 4 commits into from
May 3, 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
9 changes: 8 additions & 1 deletion lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def initialize(config_arg = {})
@system_profiler = SystemProfiler.new
@last_request_metrics = nil

# The following attribute is only used to log whether or not
# StripeClient#request has been called. To be removed in a
# future major version.
@usage = []

@config = case config_arg
when Hash
Stripe.config.reverse_duplicate_merge(config_arg)
Expand Down Expand Up @@ -186,6 +191,7 @@ def connection_manager
# charge, resp = client.request { Charge.create }
#
def request
@usage = ["stripe_client_request"]
old_stripe_client = self.class.current_thread_context.active_client
self.class.current_thread_context.active_client = self

Expand All @@ -200,6 +206,7 @@ def request
res = yield
[res, self.class.current_thread_context.last_responses[object_id]]
ensure
@usage = []
self.class.current_thread_context.active_client = old_stripe_client
self.class.current_thread_context.last_responses.delete(object_id)
end
Expand Down Expand Up @@ -583,7 +590,7 @@ def self.maybe_gc_connection_managers
if config.enable_telemetry? && context.request_id
request_duration_ms = (request_duration * 1000).to_i
@last_request_metrics =
StripeRequestMetrics.new(context.request_id, request_duration_ms, usage: usage)
StripeRequestMetrics.new(context.request_id, request_duration_ms, usage: usage + @usage)
end

# We rescue all exceptions from a request so that we have an easy spot to
Expand Down
72 changes: 72 additions & 0 deletions test/stripe/stripe_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,78 @@ class StripeClientTest < Test::Unit::TestCase
assert(!trace_payload["last_request_metrics"]["request_duration_ms"].nil?)
assert(trace_payload["last_request_metrics"]["usage"].nil?)
end

should "send metrics on #request" do
Stripe.enable_telemetry = true

trace_metrics_header = nil

stub_request(:get, "#{Stripe.api_base}/v1/charges")
.with do |req|
trace_metrics_header = req.headers["X-Stripe-Client-Telemetry"]
false
end.to_return(body: JSON.generate(object: "charge"))

client = StripeClient.new
client.request do
Charge.create
Charge.list
end

assert_not_nil(trace_metrics_header)
trace_payload = JSON.parse(trace_metrics_header)
assert_equal(["stripe_client_request"], trace_payload["last_request_metrics"]["usage"])
end

should "not send metrics outside of #request block" do
Stripe.enable_telemetry = true

trace_metrics_header = nil

stub_request(:get, "#{Stripe.api_base}/v1/charges")
.with do |req|
trace_metrics_header = req.headers["X-Stripe-Client-Telemetry"]
false
end.to_return(body: JSON.generate(object: "charge"))

client = StripeClient.new
client.request do
Charge.create
Charge.list
end

Charge.create
Charge.list

assert_not_nil(trace_metrics_header)
trace_payload = JSON.parse(trace_metrics_header)
assert_nil(trace_payload["last_request_metrics"]["usage"])
end

should "append stripe_client_request usage to existing usage" do
Stripe.enable_telemetry = true

trace_metrics_header = nil
stub_request(:get, "#{Stripe.api_base}/v1/charges")
.with do |req|
trace_metrics_header = req.headers["X-Stripe-Client-Telemetry"]
false
end.to_return(body: JSON.generate(object: "charge"))

client = StripeClient.new

client.request do
cus = Customer.new("cus_xyz")
cus.description = "hello"
cus.save
Charge.list
end

assert_not_nil(trace_metrics_header)
trace_payload = JSON.parse(trace_metrics_header)

assert(trace_payload["last_request_metrics"]["usage"] == %w[save stripe_client_request])
end
end

context "instrumentation" do
Expand Down
Loading