Class: EpsagonTracerMiddleware

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

Overview

Sinatra middleware for epsagon instrumentation

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ EpsagonTracerMiddleware

Returns a new instance of EpsagonTracerMiddleware.



10
11
12
# File 'lib/instrumentation/sinatra.rb', line 10

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/instrumentation/sinatra.rb', line 18

def call(env)
  request = Rack::Request.new(env)
  path, path_params = request.path.split(';')
  request_headers = JSON.generate(Hash[*env.select { |k, _v| k.start_with? 'HTTP_' }
    .collect { |k, v| [k.sub(/^HTTP_/, ''), v] }
    .collect { |k, v| [k.split('_').collect(&:capitalize).join('-'), v] }
    .sort
    .flatten])

  attributes = {
    'operation' => env['REQUEST_METHOD'],
    'type' => 'http',
    'http.scheme' => env['rack.url_scheme'],
    'http.request.path' => path,
    'http.request.headers' => request_headers
  }

  unless config[:epsagon][:metadata_only]
    request.body.rewind
    request_body = request.body.read
    request.body.rewind

    attributes.merge!(Util.epsagon_query_attributes(request.query_string))

    attributes.merge!({
                        'http.request.body' => request_body,
                        'http.request.path_params' => path_params,
                        'http.request.headers.User-Agent' => env['HTTP_USER_AGENT']
                      })
  end

  tracer.in_span(
    env['HTTP_HOST'],
    attributes: attributes,
    kind: :server,
    with_parent: parent_context(env)
  ) do |http_span|
    tracer.in_span(
      env['HTTP_HOST'],
      kind: :server,
      attributes: { type: 'sinatra' }
    ) do |framework_span|
      app.call(env).tap { |resp| trace_response(http_span, framework_span, env, resp) }
    end
  end
end

#configObject



14
15
16
# File 'lib/instrumentation/sinatra.rb', line 14

def config
  EpsagonSinatraInstrumentation.instance.config
end