-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Excon instrumentation * Add missing specs * Add CHANGELOG * Fix Rubocop * Fix spec support for Ruby < 3 * Update sentry-ruby/lib/sentry/excon/middleware.rb Co-authored-by: Peter Solnica <peter@solnica.online> * Handle case for Excon 1+ with Hash as query * Borrow Rack::Utils for building nested query * Add one more specs for more advanced query building in Excon * Fixes changelog * Add new specs using the query parameter encoding --------- Co-authored-by: Peter Solnica <peter@solnica.online>
- Loading branch information
1 parent
b31f0f3
commit a9b3687
Showing
8 changed files
with
372 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -614,3 +614,4 @@ def utc_now | |
require "sentry/puma" | ||
require "sentry/graphql" | ||
require "sentry/faraday" | ||
require "sentry/excon" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
Sentry.register_patch(:excon) do | ||
if defined?(::Excon) | ||
require "sentry/excon/middleware" | ||
if Excon.defaults[:middlewares] | ||
Excon.defaults[:middlewares] << Sentry::Excon::Middleware unless Excon.defaults[:middlewares].include?(Sentry::Excon::Middleware) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sentry | ||
module Excon | ||
OP_NAME = "http.client" | ||
|
||
class Middleware < ::Excon::Middleware::Base | ||
def initialize(stack) | ||
super | ||
@instrumenter = Instrumenter.new | ||
end | ||
|
||
def request_call(datum) | ||
@instrumenter.start_transaction(datum) | ||
@stack.request_call(datum) | ||
end | ||
|
||
def response_call(datum) | ||
@instrumenter.finish_transaction(datum) | ||
@stack.response_call(datum) | ||
end | ||
end | ||
|
||
class Instrumenter | ||
SPAN_ORIGIN = "auto.http.excon" | ||
BREADCRUMB_CATEGORY = "http" | ||
|
||
include Utils::HttpTracing | ||
|
||
def start_transaction(env) | ||
return unless Sentry.initialized? | ||
|
||
current_span = Sentry.get_current_scope&.span | ||
@span = current_span&.start_child(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f, origin: SPAN_ORIGIN) | ||
|
||
request_info = extract_request_info(env) | ||
|
||
if propagate_trace?(request_info[:url]) | ||
set_propagation_headers(env[:headers]) | ||
end | ||
end | ||
|
||
def finish_transaction(response) | ||
return unless @span | ||
|
||
response_status = response[:response][:status] | ||
request_info = extract_request_info(response) | ||
|
||
if record_sentry_breadcrumb? | ||
record_sentry_breadcrumb(request_info, response_status) | ||
end | ||
|
||
set_span_info(@span, request_info, response_status) | ||
ensure | ||
@span&.finish | ||
end | ||
|
||
private | ||
|
||
def extract_request_info(env) | ||
url = env[:scheme] + "://" + env[:hostname] + env[:path] | ||
result = { method: env[:method].to_s.upcase, url: url } | ||
|
||
if Sentry.configuration.send_default_pii | ||
result[:query] = env[:query] | ||
|
||
# Handle excon 1.0.0+ | ||
result[:query] = build_nested_query(result[:query]) unless result[:query].is_a?(String) | ||
|
||
result[:body] = env[:body] | ||
end | ||
|
||
result | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.