mirror of
				https://github.com/stripe/stripe-ruby.git
				synced 2025-11-04 00:01:47 -05:00 
			
		
		
		
	Adds a new instrumentation callback called `request_begin` which, as the name suggests, is invoked before an HTTP request is dispatched. As outlined originally in #900, the idea is that this will enable a set of hooks that can be used for distributed tracing. The PR also renames the existing `request` callback to `request_end`, although the old name is still invoked for the time being for backwards compatibility. A special `user_data` property is passed to `request_begin` which allows subscribers to set custom data that will be passed through to `request_end` for any given request. This allows, for example, a user assigned ID to be set for the request and recognized on both ends. I chose the naming `_begin` and `_end` (as opposed to start/finish or any other combination) based on the naming conventions of Ruby itself. Fixes #900.
		
			
				
	
	
		
			75 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
# frozen_string_literal: true
 | 
						|
 | 
						|
require_relative "../test_helper"
 | 
						|
 | 
						|
module Stripe
 | 
						|
  class InstrumentationTest < Test::Unit::TestCase
 | 
						|
    context ".notify" do
 | 
						|
      teardown do
 | 
						|
        Stripe::Instrumentation.send(:subscribers).clear
 | 
						|
      end
 | 
						|
 | 
						|
      should "notify subscribers for the right topic" do
 | 
						|
        sub1_events = []
 | 
						|
        Stripe::Instrumentation.subscribe(:test1, :sub1) { |event| sub1_events << event }
 | 
						|
        sub2_events = []
 | 
						|
        Stripe::Instrumentation.subscribe(:test2, :sub2) { |event| sub2_events << event }
 | 
						|
 | 
						|
        Stripe::Instrumentation.notify(:test1, "hello")
 | 
						|
        assert_equal(1, sub1_events.size)
 | 
						|
        assert_equal(0, sub2_events.size)
 | 
						|
      end
 | 
						|
 | 
						|
      should "notify multiple subscribers of the same topic" do
 | 
						|
        sub1_events = []
 | 
						|
        Stripe::Instrumentation.subscribe(:test, :sub1) { |event| sub1_events << event }
 | 
						|
        sub2_events = []
 | 
						|
        Stripe::Instrumentation.subscribe(:test, :sub2) { |event| sub2_events << event }
 | 
						|
 | 
						|
        Stripe::Instrumentation.notify(:test, "hello")
 | 
						|
        assert_equal(1, sub1_events.size)
 | 
						|
        assert_equal(1, sub2_events.size)
 | 
						|
      end
 | 
						|
 | 
						|
      should "not notify a subscriber once it has unsubscribed" do
 | 
						|
        events = []
 | 
						|
        Stripe::Instrumentation.subscribe(:test, :sub) { |event| events << event }
 | 
						|
 | 
						|
        Stripe::Instrumentation.notify(:test, "hello")
 | 
						|
        assert_equal(1, events.size)
 | 
						|
 | 
						|
        Stripe::Instrumentation.unsubscribe(:test, :sub)
 | 
						|
        Stripe::Instrumentation.notify(:test, "hello")
 | 
						|
        assert_equal(1, events.size)
 | 
						|
      end
 | 
						|
    end
 | 
						|
 | 
						|
    context "RequestEventBegin" do
 | 
						|
      should "return a frozen object" do
 | 
						|
        event = Stripe::Instrumentation::RequestBeginEvent.new(
 | 
						|
          method: :get,
 | 
						|
          path: "/v1/test",
 | 
						|
          user_data: nil
 | 
						|
        )
 | 
						|
 | 
						|
        assert(event.frozen?)
 | 
						|
      end
 | 
						|
    end
 | 
						|
 | 
						|
    context "RequestEventEnd" do
 | 
						|
      should "return a frozen object" do
 | 
						|
        event = Stripe::Instrumentation::RequestEndEvent.new(
 | 
						|
          duration: 0.1,
 | 
						|
          http_status: 200,
 | 
						|
          method: :get,
 | 
						|
          num_retries: 0,
 | 
						|
          path: "/v1/test",
 | 
						|
          user_data: nil
 | 
						|
        )
 | 
						|
 | 
						|
        assert(event.frozen?)
 | 
						|
      end
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 |