Class: Datadog::Tracing::Contrib::Excon::Middleware

Inherits:
Excon::Middleware::Base
  • Object
show all
Includes:
HttpAnnotationHelper
Defined in:
lib/datadog/tracing/contrib/excon/middleware.rb

Overview

Middleware implements an excon-middleware for datadog instrumentation

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HttpAnnotationHelper

#service_name

Constructor Details

#initialize(stack, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



20
21
22
23
# File 'lib/datadog/tracing/contrib/excon/middleware.rb', line 20

def initialize(stack, options = {})
  super(stack)
  @default_options = datadog_configuration.options_hash.merge(options)
end

Class Method Details

.around_default_stackObject

Returns a copy of the default stack with the trace middleware injected



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/datadog/tracing/contrib/excon/middleware.rb', line 83

def self.around_default_stack
  ::Excon.defaults[:middlewares].dup.tap do |default_stack|
    # If the default stack contains a version of the trace middleware already...
    existing_trace_middleware = default_stack.find { |m| m <= Middleware }
    default_stack.delete(existing_trace_middleware) if existing_trace_middleware

    # Inject after the ResponseParser middleware
    response_middleware_index = default_stack.index(::Excon::Middleware::ResponseParser).to_i
    default_stack.insert(response_middleware_index + 1, self)
  end
end

.with(options = {}) ⇒ Object

Returns a child class of this trace middleware With options given as defaults.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/datadog/tracing/contrib/excon/middleware.rb', line 60

def self.with(options = {})
  Class.new(self) do
    @options = options

    # rubocop:disable Style/TrivialAccessors
    def self.options
      @options
    end
    # rubocop:enable Style/TrivialAccessors

    # default_options in this case contains our specific middleware options
    # so we want it to take precedence in build_request_options
    def build_request_options!(datum)
      datadog_configuration(datum[:host]).options_hash.merge(@default_options)
    end

    def initialize(stack)
      super(stack, self.class.options)
    end
  end
end

Instance Method Details

#error_call(datum) ⇒ Object



53
54
55
56
# File 'lib/datadog/tracing/contrib/excon/middleware.rb', line 53

def error_call(datum)
  handle_response(datum)
  @stack.error_call(datum)
end

#request_call(datum) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/datadog/tracing/contrib/excon/middleware.rb', line 25

def request_call(datum)
  begin
    unless datum.key?(:datadog_span)
      @options = build_request_options!(datum)
      span = Tracing.trace(Ext::SPAN_REQUEST)
      trace = Tracing.active_trace
      datum[:datadog_span] = span
      annotate!(span, datum)
      if Datadog::AppSec::Utils::TraceOperation.appsec_standalone_reject?(trace)
        trace.sampling_priority = Tracing::Sampling::Ext::Priority::AUTO_REJECT
      end
      propagate!(trace, span, datum) if distributed_tracing?

      span
    end
  rescue StandardError => e
    Datadog.logger.debug(e.message)
  end

  @stack.request_call(datum)
end

#response_call(datum) ⇒ Object



47
48
49
50
51
# File 'lib/datadog/tracing/contrib/excon/middleware.rb', line 47

def response_call(datum)
  @stack.response_call(datum).tap do |d|
    handle_response(d)
  end
end