Module: AppPerfRpm::Instruments::GrapeMiddleware

Defined in:
lib/app_perf_rpm/instruments/grape.rb

Instance Method Summary collapse

Instance Method Details

#call_with_trace(env) ⇒ Object



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/app_perf_rpm/instruments/grape.rb', line 7

def call_with_trace(env)
  req = ::Rack::Request.new(env)

  if AppPerfRpm::Tracer.tracing?
    request_method = req.request_method.to_s.upcase
    path = req.path
    endpoint = env["api.endpoint"]

    if endpoint && endpoint.options
      options = endpoint.options
      request_method = options[:method].first.to_s.upcase
      klass = options[:for]
      namespace = endpoint.namespace
      namespace = "" if namespace == "/"

      path = options[:path].first.to_s
      path = "/#{path}" if path[0] != "/"
      path = "#{namespace}#{path}"
    end

    action = path.to_s.split("/").last
    operation = "#{klass}##{action}"

    span = AppPerfRpm.tracer.start_span(operation, tags: {
      "component" => "Grape",
      "span.kind" => "client",
      "class.name" => @app.class.name,
      "http.path" => path,
      "http.method" => request_method
    })
    AppPerfRpm::Utils.log_source_and_backtrace(span, :rack)
  end

  begin
    call_without_trace(env)
  rescue Exception => e
    if span
      span.set_tag('error', true)
      span.log_error(e)
    end
    raise
  ensure
    span.finish if span
  end
end