Class: OpenTelemetry::Instrumentation::Sinatra::Middlewares::TracerMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertrace/instrumentation/sinatra.rb

Overview

Re-open sinatra middleware to customize middleware to call our Hypertrace specific capture

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hypertrace/instrumentation/sinatra.rb', line 5

def call env
  extracted_context = OpenTelemetry.propagation.extract(
    env,
    getter: OpenTelemetry::Common::Propagation.rack_env_getter
  )
  OpenTelemetry::Context.with_current(extracted_context) do
    attrs = Hypertrace::Instrumentation::RackCompatible.extract_req_headers_as_attributes(env, 'http.request.header')
    if Hypertrace::Instrumentation::RackCompatible.should_record_env?(env)
      body_cap = Hypertrace::Instrumentation::DataCapture.capturable_body(env['rack.input'])
      attrs['http.request.body'] = body_cap if body_cap
    end

    tracer.in_span(
      env['PATH_INFO'],
      attributes: request_span_attributes(env: env).merge!(attrs),
      kind: :server
    ) do |span|
      @app.call(env).tap do |resp|
        response_attributes = Hypertrace::Instrumentation::RackCompatible.extract_response_headers_as_attributes(resp, 'http.response.header')
        if Hypertrace::Instrumentation::RackCompatible.should_record_rack_array?(resp)
          cap_body = Hypertrace::Instrumentation::RackCompatible.extract_response_body(resp)
          response_attributes['http.response.body'] = cap_body if cap_body
        end
        span.add_attributes(response_attributes)
        trace_response(span, env, resp)
      end
    end
  end
end

#request_span_attributes(env:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hypertrace/instrumentation/sinatra.rb', line 35

def request_span_attributes(env:)
  attributes = {
    'http.method' => env['REQUEST_METHOD'],
    'http.host' => env['HTTP_HOST'] || 'unknown',
    'http.scheme' => env['rack.url_scheme'],
    'http.target' => env['QUERY_STRING'].empty? ? env['PATH_INFO'] : "#{env['PATH_INFO']}?#{env['QUERY_STRING']}"
  }

  attributes['http.user_agent'] = env['HTTP_USER_AGENT'] if env['HTTP_USER_AGENT']
  attributes
end