module HTTPX::Plugins::Sentry::Tracer

  1. lib/httpx/adapters/sentry.rb

Public Instance methods

call(request)
[show source]
   # File lib/httpx/adapters/sentry.rb
10 def call(request)
11   sentry_span = start_sentry_span
12 
13   return unless sentry_span
14 
15   set_sentry_trace_header(request, sentry_span)
16 
17   request.on(:response, &method(:finish_sentry_span).curry(3)[sentry_span, request])
18 end
extract_request_info(req)
[show source]
   # File lib/httpx/adapters/sentry.rb
79 def extract_request_info(req)
80   uri = req.uri
81 
82   result = {
83     method: req.verb,
84   }
85 
86   if ::Sentry.configuration.send_default_pii
87     uri += "?#{req.query}" unless req.query.empty?
88     result[:body] = req.body.to_s unless req.body.empty? || req.body.unbounded_body?
89   end
90 
91   result[:url] = uri.to_s
92 
93   result
94 end
finish_sentry_span(span, request, response)
[show source]
   # File lib/httpx/adapters/sentry.rb
39 def finish_sentry_span(span, request, response)
40   return unless ::Sentry.initialized?
41 
42   record_sentry_breadcrumb(request, response)
43   record_sentry_span(request, response, span)
44 end
record_sentry_breadcrumb(req, res)
[show source]
   # File lib/httpx/adapters/sentry.rb
46 def record_sentry_breadcrumb(req, res)
47   return unless ::Sentry.configuration.breadcrumbs_logger.include?(:http_logger)
48 
49   request_info = extract_request_info(req)
50 
51   data = if res.is_a?(HTTPX::ErrorResponse)
52     { error: res.error.message, **request_info }
53   else
54     { status: res.status, **request_info }
55   end
56 
57   crumb = ::Sentry::Breadcrumb.new(
58     level: :info,
59     category: "httpx",
60     type: :info,
61     data: data
62   )
63   ::Sentry.add_breadcrumb(crumb)
64 end
record_sentry_span(req, res, sentry_span)
[show source]
   # File lib/httpx/adapters/sentry.rb
66 def record_sentry_span(req, res, sentry_span)
67   return unless sentry_span
68 
69   request_info = extract_request_info(req)
70   sentry_span.set_description("#{request_info[:method]} #{request_info[:url]}")
71   if res.is_a?(HTTPX::ErrorResponse)
72     sentry_span.set_data(:error, res.error.message)
73   else
74     sentry_span.set_data(:status, res.status)
75   end
76   sentry_span.set_timestamp(::Sentry.utc_now.to_f)
77 end
set_sentry_trace_header(request, sentry_span)
[show source]
   # File lib/httpx/adapters/sentry.rb
27 def set_sentry_trace_header(request, sentry_span)
28   return unless sentry_span
29 
30   config = ::Sentry.configuration
31   url = request.uri.to_s
32 
33   return unless config.propagate_traces && config.trace_propagation_targets.any? { |target| url.match?(target) }
34 
35   trace = ::Sentry.get_current_client.generate_sentry_trace(sentry_span)
36   request.headers[::Sentry::SENTRY_TRACE_HEADER_NAME] = trace if trace
37 end
start_sentry_span()
[show source]
   # File lib/httpx/adapters/sentry.rb
20 def start_sentry_span
21   return unless ::Sentry.initialized? && (span = ::Sentry.get_current_scope.get_span)
22   return if span.sampled == false
23 
24   span.start_child(op: "httpx.client", start_timestamp: ::Sentry.utc_now.to_f)
25 end