Module: Datadog::Tracing::Contrib::HTTP::Instrumentation::InstanceMethods

Includes:
Datadog::Tracing::Contrib::HttpAnnotationHelper
Defined in:
lib/datadog/tracing/contrib/http/instrumentation.rb

Overview

InstanceMethods - implementing instrumentation

Instance Method Summary collapse

Methods included from Datadog::Tracing::Contrib::HttpAnnotationHelper

#service_name

Instance Method Details

#annotate_span_with_error!(span, error) ⇒ Object



99
100
101
# File 'lib/datadog/tracing/contrib/http/instrumentation.rb', line 99

def annotate_span_with_error!(span, error)
  span.set_error(error)
end

#annotate_span_with_request!(span, request, request_options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/datadog/tracing/contrib/http/instrumentation.rb', line 70

def annotate_span_with_request!(span, request, request_options)
  span.set_tag(Tracing::::Ext::TAG_KIND, Tracing::::Ext::SpanKind::TAG_CLIENT)

  span.set_tag(Tracing::::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
  span.set_tag(Tracing::::Ext::TAG_OPERATION, Ext::TAG_OPERATION_REQUEST)

  span.set_tag(Tracing::::Ext::HTTP::TAG_URL, request.path)
  span.set_tag(Tracing::::Ext::HTTP::TAG_METHOD, request.method)

  host, port = host_and_port(request)
  span.set_tag(Tracing::::Ext::NET::TAG_TARGET_HOST, host)
  span.set_tag(Tracing::::Ext::NET::TAG_TARGET_PORT, port.to_s)

  # Tag as an external peer service
  span.set_tag(Tracing::::Ext::TAG_PEER_SERVICE, span.service)
  span.set_tag(Tracing::::Ext::TAG_PEER_HOSTNAME, host)

  # Set analytics sample rate
  set_analytics_sample_rate(span, request_options)
end

#annotate_span_with_response!(span, response, request_options) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/datadog/tracing/contrib/http/instrumentation.rb', line 91

def annotate_span_with_response!(span, response, request_options)
  return unless response && response.code

  span.set_tag(Tracing::::Ext::HTTP::TAG_STATUS_CODE, response.code)

  span.set_error(response) if request_options[:error_status_codes].include? response.code.to_i
end

#request(req, body = nil, &block) ⇒ Object

:yield: response



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
# File 'lib/datadog/tracing/contrib/http/instrumentation.rb', line 33

def request(req, body = nil, &block)
  host, = host_and_port(req)
  request_options = datadog_configuration(host)
  client_config = Datadog.configuration_for(self)

  return super(req, body, &block) if Contrib::HTTP.should_skip_tracing?(req)

  Tracing.trace(Ext::SPAN_REQUEST, on_error: method(:annotate_span_with_error!)) do |span, trace|
    begin
      span.service = service_name(host, request_options, client_config)
      span.span_type = Tracing::::Ext::HTTP::TYPE_OUTBOUND
      span.resource = req.method

      if Tracing.enabled? && !Contrib::HTTP.should_skip_distributed_tracing?(client_config)
        Tracing::Propagation::HTTP.inject!(trace, req)
      end

      # Add additional request specific tags to the span.
      annotate_span_with_request!(span, req, request_options)
    rescue StandardError => e
      Datadog.logger.error("error preparing span for http request: #{e}")
    ensure
      response = super(req, body, &block)
    end

    # Add additional response specific tags to the span.
    annotate_span_with_response!(span, response, request_options)

    # Invoke hook, if set.
    unless Contrib::HTTP::Instrumentation.after_request.nil?
      Contrib::HTTP::Instrumentation.after_request.call(span, self, req, response)
    end

    response
  end
end

#set_analytics_sample_rate(span, request_options) ⇒ Object



103
104
105
106
107
# File 'lib/datadog/tracing/contrib/http/instrumentation.rb', line 103

def set_analytics_sample_rate(span, request_options)
  return unless analytics_enabled?(request_options)

  Contrib::Analytics.set_sample_rate(span, analytics_sample_rate(request_options))
end