Module: Datadog::Tracing::Contrib::HTTPX::Plugin::RequestTracer

Extended by:
Contrib::HttpAnnotationHelper
Defined in:
lib/httpx/adapters/datadog.rb

Constant Summary collapse

SPAN_REQUEST =
"httpx.request"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configuration(request) ⇒ Object



140
141
142
# File 'lib/httpx/adapters/datadog.rb', line 140

def configuration(request)
  Datadog.configuration.tracing[:httpx, request.uri.host]
end

.enabled?(request) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/httpx/adapters/datadog.rb', line 52

def enabled?(request)
  configuration(request).enabled
end

.finish(request, response) ⇒ Object



64
65
66
67
68
# File 'lib/httpx/adapters/datadog.rb', line 64

def finish(request, response)
  request.datadog_span ||= initialize_span(request, request.init_time) if request.init_time

  finish_span(response, request.datadog_span)
end

.finish_span(response, span) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/httpx/adapters/datadog.rb', line 70

def finish_span(response, span)
  if response.is_a?(::HTTPX::ErrorResponse)
    span.set_error(response.error)
  else
    span.set_tag(TAG_STATUS_CODE, response.status.to_s)

    span.set_error(::HTTPX::HTTPError.new(response)) if response.status.between?(400, 599)

    span.set_tags(
      Datadog.configuration.tracing.header_tags.response_tags(response.headers.to_h)
    ) if Datadog.configuration.tracing.respond_to?(:header_tags)
  end

  span.finish
end

.initialize_span(request, start_time) ⇒ Object

return a span initialized with the @request state.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/httpx/adapters/datadog.rb', line 87

def initialize_span(request, start_time)
  verb = request.verb
  uri = request.uri

  config = configuration(request)

  span = create_span(request, config, start_time)

  span.resource = verb

  # Tag original global service name if not used
  span.set_tag(TAG_BASE_SERVICE, Datadog.configuration.service) if span.service != Datadog.configuration.service

  span.set_tag(TAG_KIND, TAG_CLIENT)

  span.set_tag(TAG_COMPONENT, "httpx")
  span.set_tag(TAG_OPERATION, "request")

  span.set_tag(TAG_URL, request.path)
  span.set_tag(TAG_METHOD, verb)

  span.set_tag(TAG_TARGET_HOST, uri.host)
  span.set_tag(TAG_TARGET_PORT, uri.port)

  span.set_tag(TAG_PEER_HOSTNAME, uri.host)

  # Tag as an external peer service
  if (peer_service = config[:peer_service])
    span.set_tag(TAG_PEER_SERVICE, peer_service)
  end

  if config[:distributed_tracing]
    propagate_trace_http(
      Datadog::Tracing.active_trace,
      request.headers
    )
  end

  # Set analytics sample rate
  if Contrib::Analytics.enabled?(config[:analytics_enabled])
    Contrib::Analytics.set_sample_rate(span, config[:analytics_sample_rate])
  end

  span.set_tags(
    Datadog.configuration.tracing.header_tags.request_tags(request.headers.to_h)
  ) if Datadog.configuration.tracing.respond_to?(:header_tags)

  span
        rescue StandardError => e
Datadog.logger.error("error preparing span for http request: #{e}")
Datadog.logger.error(e.backtrace)
end

.reset(request) ⇒ Object



60
61
62
# File 'lib/httpx/adapters/datadog.rb', line 60

def reset(request)
  request.datadog_span = nil
end

.start(request) ⇒ Object



56
57
58
# File 'lib/httpx/adapters/datadog.rb', line 56

def start(request)
  request.datadog_span = initialize_span(request, request.init_time)
end

Instance Method Details

#create_span(request, configuration, start_time) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/httpx/adapters/datadog.rb', line 149

def create_span(request, configuration, start_time)
  Datadog::Tracing.trace(
    SPAN_REQUEST,
    service: service_name(request.uri.host, configuration),
    type: TYPE_OUTBOUND,
    start_time: start_time
  )
end

#propagate_trace_http(trace, headers) ⇒ Object



145
146
147
# File 'lib/httpx/adapters/datadog.rb', line 145

def propagate_trace_http(trace, headers)
  Datadog::Tracing::Contrib::HTTP.inject(trace, headers)
end