diff --git a/lib/stripe/stripe_client.rb b/lib/stripe/stripe_client.rb index 81a54b2e9..0fa2c7fd1 100644 --- a/lib/stripe/stripe_client.rb +++ b/lib/stripe/stripe_client.rb @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/test/stripe/stripe_client_test.rb b/test/stripe/stripe_client_test.rb index a33d1dd6b..5d9434bfa 100644 --- a/test/stripe/stripe_client_test.rb +++ b/test/stripe/stripe_client_test.rb @@ -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