Module: Sentry::Net::HTTP Private

Defined in:
lib/sentry/net/http.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Constant Summary collapse

OP_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"http.client"
SPAN_ORIGIN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"auto.http.net_http"
"net.http"

Instance Method Summary collapse

Instance Method Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

To explain how the entire thing works, we need to know how the original Net::HTTP#request works Here’s part of its definition. As you can see, it usually calls itself inside a #start block

“‘ def request(req, body = nil, &block)

unless started?
  start {
    req['connection'] ||= 'close'
    return request(req, body, &block) # <- request will be called for the second time from the first call
  }
end
# .....

end “‘

So we’re only instrumenting request when ‘Net::HTTP` is already started



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sentry/net/http.rb', line 30

def request(req, body = nil, &block)
  return super unless started? && Sentry.initialized?
  return super if from_sentry_sdk?

  Sentry.with_child_span(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f, origin: SPAN_ORIGIN) do |sentry_span|
    request_info = extract_request_info(req)

    if propagate_trace?(request_info[:url], Sentry.configuration)
      set_propagation_headers(req)
    end

    super.tap do |res|
      record_sentry_breadcrumb(request_info, res)

      if sentry_span
        sentry_span.set_description("#{request_info[:method]} #{request_info[:url]}")
        sentry_span.set_data(Span::DataConventions::URL, request_info[:url])
        sentry_span.set_data(Span::DataConventions::HTTP_METHOD, request_info[:method])
        sentry_span.set_data(Span::DataConventions::HTTP_QUERY, request_info[:query]) if request_info[:query]
        sentry_span.set_data(Span::DataConventions::HTTP_STATUS_CODE, res.code.to_i)
      end
    end
  end
end