module AppPerfRpm::Instruments::GrapeMiddleware

Public Instance Methods

call_with_trace(env) click to toggle source
# 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