Sha256: beaa76dcf03d3a8560e558608c737b1c4f278b99bea81b2c92207979415ed7f0

Contents?: true

Size: 969 Bytes

Versions: 3

Compression:

Stored size: 969 Bytes

Contents

require 'appdash'
require 'rack'
require 'appdash/event/rack_server'

module Appdash
  # 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.
  class Middleware
    ENV_KEY = 'appdash.span'.freeze

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

    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

    private

      def build(recv, env, status, headers)
        Appdash::Event::RackServer.new Rack::Request.new(env), nil,
          response: {
            status_code: status,
            content_length: headers['Content-Length'],
          },
          recv: recv
      end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
appdash-0.6.3 lib/appdash/middleware.rb
appdash-0.6.2 lib/appdash/middleware.rb
appdash-0.6.1 lib/appdash/middleware.rb