class ElasticAPM::Middleware

@api private

Public Class Methods

new(app) click to toggle source
# File lib/elastic_apm/middleware.rb, line 9
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/AbcSize

# File lib/elastic_apm/middleware.rb, line 14
def call(env)
  begin
    if running? && !path_ignored?(env)
      transaction = start_transaction(env)
    end

    resp = @app.call env
  rescue InternalError
    raise # Don't report ElasticAPM errors
  rescue ::Exception => e
    context = ElasticAPM.build_context(rack_env: env, for_type: :error)
    ElasticAPM.report(e, context: context, handled: false)
    raise
  ensure
    if resp && transaction
      status, headers, _body = resp
      transaction.add_response(status, headers: headers.dup)
    end

    ElasticAPM.end_transaction http_result(status)
  end

  resp
end

Private Instance Methods

config() click to toggle source
# File lib/elastic_apm/middleware.rb, line 72
def config
  @config ||= ElasticAPM.agent.config
end
http_result(status) click to toggle source

rubocop:enable Metrics/MethodLength, Metrics/AbcSize

# File lib/elastic_apm/middleware.rb, line 42
def http_result(status)
  status && "HTTP #{status.to_s[0]}xx"
end
path_ignored?(env) click to toggle source
# File lib/elastic_apm/middleware.rb, line 46
def path_ignored?(env)
  config.ignore_url_patterns.any? do |r|
    env['PATH_INFO'].match r
  end
end
running?() click to toggle source
# File lib/elastic_apm/middleware.rb, line 68
def running?
  ElasticAPM.running?
end
start_transaction(env) click to toggle source
# File lib/elastic_apm/middleware.rb, line 52
def start_transaction(env)
  context = ElasticAPM.build_context(rack_env: env, for_type: :transaction)

  ElasticAPM.start_transaction 'Rack', 'request',
    context: context,
    trace_context: trace_context(env)
end
trace_context(env) click to toggle source
# File lib/elastic_apm/middleware.rb, line 60
def trace_context(env)
  return unless (header = env['HTTP_ELASTIC_APM_TRACEPARENT'])
  TraceContext.parse(header)
rescue TraceContext::InvalidTraceparentHeader
  warn "Couldn't parse invalid traceparent header: #{header.inspect}"
  nil
end