Skip to content

Commit

Permalink
WIP Create Request/Response Context obj
Browse files Browse the repository at this point in the history
Fix lint errors
  • Loading branch information
nguyhh committed Jan 10, 2023
1 parent bd8d7ee commit 380901a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 28 deletions.
54 changes: 43 additions & 11 deletions lib/stripe/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class RequestEndEvent
attr_reader :num_retries
attr_reader :path
attr_reader :request_id
attr_reader :response
attr_reader :response_header
attr_reader :response_body
attr_reader :request_header
attr_reader :request_body

Expand All @@ -43,22 +44,53 @@ class RequestEndEvent
# in `request_end`.
attr_reader :user_data

def initialize(duration:, http_status:, method:, num_retries:, path:,
request_id:, user_data: nil, response: nil, request_header: nil, request_body: nil)
@duration = duration
@http_status = http_status
@method = method
def initialize(request_context:, response_context:,
num_retries:, user_data: nil)
@duration = request_context.duration
@http_status = response_context.http_status
@method = request_context.method
@num_retries = num_retries
@path = path
@request_id = request_id
@path = request_context.path
@request_id = request_context.request_id
@user_data = user_data
@response = response
@request_header = request_header
@request_body = request_body
@response_header = response_context.header
@response_body = response_context.body
@request_header = request_context.header
@request_body = request_context.body
freeze
end
end

class RequestContext
attr_reader :duration
attr_reader :method
attr_reader :path
attr_reader :request_id
attr_reader :body
attr_reader :header

def initialize(duration:, context:, header:)
@duration = duration
@method = context.method
@path = context.path
@request_id = context.request_id
@body = context.body
@header = header
end
end

class ResponseContext
attr_reader :http_status
attr_reader :body
attr_reader :header

def initialize(http_status:, response:)
@http_status = http_status
@header = response.to_hash
@body = response.body
end
end

# This class was renamed for consistency. This alias is here for backwards
# compatibility.
RequestEvent = RequestEndEvent
Expand Down
41 changes: 24 additions & 17 deletions lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,16 @@ def self.maybe_gc_connection_managers
end
end

http_resp = execute_request_with_rescues(method, api_base, headers, context) do
self.class
.default_connection_manager(config)
.execute_request(method, url,
body: body,
headers: headers,
query: query,
&response_block)
end
http_resp =
execute_request_with_rescues(method, api_base, headers, context) do
self.class
.default_connection_manager(config)
.execute_request(method, url,
body: body,
headers: headers,
query: query,
&response_block)
end

[http_resp, api_key]
end
Expand Down Expand Up @@ -586,6 +587,7 @@ def self.maybe_gc_connection_managers
end

log_response(context, request_start, http_status, resp.body, resp)

notify_request_end(context, request_duration, http_status,
num_retries, user_data, resp, headers)

Expand Down Expand Up @@ -613,6 +615,7 @@ def self.maybe_gc_connection_managers
else
log_response_error(error_context, request_start, e)
end

notify_request_end(context, request_duration, http_status, num_retries,
user_data, resp, headers)

Expand Down Expand Up @@ -661,17 +664,21 @@ def self.maybe_gc_connection_managers
return if !Instrumentation.any_subscribers?(:request_end) &&
!Instrumentation.any_subscribers?(:request)

event = Instrumentation::RequestEndEvent.new(
request_context = Stripe::Instrumentation::RequestContext.new(
duration: duration,
context: context,
header: headers
)
response_context = Stripe::Instrumentation::ResponseContext.new(
http_status: http_status,
method: context.method,
response: resp
)

event = Instrumentation::RequestEndEvent.new(
request_context: request_context,
response_context: response_context,
num_retries: num_retries,
path: context.path,
request_id: context.request_id,
user_data: user_data || {},
response: resp,
request_header: headers,
request_body: context.body
user_data: user_data || {}
)
Stripe::Instrumentation.notify(:request_end, event)

Expand Down

0 comments on commit 380901a

Please sign in to comment.