Class: Appdash::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/appdash/middleware.rb

Overview

Middleware is a rack middleware that can be used to add tracing to Rack servers. It creates a span per incoming request and stores it on the ‘appdash.span’ key on the env.

Constant Summary collapse

ENV_KEY =
'appdash.span'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, client) ⇒ Middleware

Returns a new instance of Middleware.



11
12
13
14
# File 'lib/appdash/middleware.rb', line 11

def initialize(app, client)
  @app    = app
  @client = client
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/appdash/middleware.rb', line 16

def call(env)
  recv = Time.now
  span = env[ENV_KEY] = @client.span

  status, headers, body = @app.call(env)
  span.event build(recv, env, status, headers)
  [status, headers, body]
ensure
  env[ENV_KEY].flush if env.key?(ENV_KEY)
end