File: middleware.rb

package info (click to toggle)
ruby-sentry-ruby 5.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 672 kB
  • sloc: ruby: 6,118; makefile: 8; sh: 4
file content (77 lines) | stat: -rw-r--r-- 1,929 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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