Sha256: 37e8aacccc1e7f8285cfe823292b04b64068411e84236282d18cbb553841695e

Contents?: true

Size: 1.73 KB

Versions: 9

Compression:

Stored size: 1.73 KB

Contents

require 'spec_helper'

describe Macmillan::Utils::StatsdMiddleware do
  let(:app)           { ->(env) { [200, env, 'app'] } }
  let(:request)       { req_for('http://example.com') }
  let(:statsd_client) { spy(:statsd_client) }

  subject { Macmillan::Utils::StatsdMiddleware.new(app, client: statsd_client) }

  it 'sends an increment metric for the status_code' do
    subject.call(request.env)
    expect(statsd_client).to have_received(:increment).with('rack.http_status.request.200').once
  end

  it 'sends an increment metric for all requests' do
    subject.call(request.env)
    expect(statsd_client).to have_received(:increment).with('rack.increments.request').once
  end

  it 'sends a timing metric for all requests' do
    subject.call(request.env)
    expect(statsd_client).to have_received(:timing).with('rack.timers.request', anything).once
  end

  context 'when extra metrics have been pushed into the env' do
    let(:app) do
      ->(env) do
        env['statsd.timers']     << 'foo.bar'
        env['statsd.increments'] << 'woo.waa'

        [200, env, 'app']
      end
    end

    it 'sends these to statsd' do
      subject.call(request.env)
      expect(statsd_client).to have_received(:timing).with('rack.timers.foo.bar', anything).once
      expect(statsd_client).to have_received(:increment).with('rack.increments.woo.waa').once
      expect(statsd_client).to have_received(:increment).with('rack.http_status.woo.waa.200').once
    end
  end

  context 'upon error' do
    let(:app) { ->(env) { raise } }

    it 'sends an exception increment to statsd and raises the error' do
      expect { subject.call(request.env) }.to raise_error
      expect(statsd_client).to have_received(:increment).with('rack.exception').once
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
macmillan-utils-1.0.21 spec/lib/macmillan/utils/statsd_middleware_spec.rb
macmillan-utils-1.0.20 spec/lib/macmillan/utils/statsd_middleware_spec.rb
macmillan-utils-1.0.19 spec/lib/macmillan/utils/statsd_middleware_spec.rb
macmillan-utils-1.0.18 spec/lib/macmillan/utils/statsd_middleware_spec.rb
macmillan-utils-1.0.17 spec/lib/macmillan/utils/statsd_middleware_spec.rb
macmillan-utils-1.0.16 spec/lib/macmillan/utils/statsd_middleware_spec.rb
macmillan-utils-1.0.15 spec/lib/macmillan/utils/statsd_middleware_spec.rb
macmillan-utils-1.0.12 spec/lib/macmillan/utils/statsd_middleware_spec.rb
macmillan-utils-1.0.11 spec/lib/macmillan/utils/statsd_middleware_spec.rb