Module: Sentry::Net::HTTP

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

Constant Summary collapse

OP_NAME =
"net.http"

Instance Method Summary collapse

Instance Method Details

#do_finishObject



50
51
52
53
54
# File 'lib/sentry/net/http.rb', line 50

def do_finish
  super.tap do
    finish_sentry_span
  end
end

#do_startObject



44
45
46
47
48
# File 'lib/sentry/net/http.rb', line 44

def do_start
  super.tap do
    start_sentry_span
  end
end

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

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 when the entire flow looks like this:

  1. #request is called.

- But because the request hasn't started yet, it calls #start (which then calls #do_start)
- At this moment @sentry_span is still nil, so #set_sentry_trace_header returns early
  1. #do_start then creates a new Span and assigns it to @sentry_span

  2. #request is called for the second time.

- This time @sentry_span should present. So #set_sentry_trace_header will set the sentry-trace header on the request object
  1. Once the request finished, it

- Records a breadcrumb if http_logger is set
- Finishes the Span inside @sentry_span and clears the instance variable


35
36
37
38
39
40
41
42
# File 'lib/sentry/net/http.rb', line 35

def request(req, body = nil, &block)
  set_sentry_trace_header(req)

  super.tap do |res|
    record_sentry_breadcrumb(req, res)
    record_sentry_span(req, res)
  end
end