require 'inst_statsd' describe QuizApiClient::HttpRequest::Metrics do let(:config) { QuizApiClient::Config.new } let(:method) { 'GET' } let(:url) { 'https://something.com' } let(:code) { 200 } subject { described_class.new(config, method, url, code) } describe '#increment' do it 'does nothing when the metrics handler is nil' do allow(config).to receive(:metrics_handler).and_return(nil) expect(InstStatsd::Statsd).to receive(:increment).never subject.increment end it 'does nothing when the metrics namespace is nil' do allow(config).to receive(:metrics_namespace).and_return(nil) expect(InstStatsd::Statsd).to receive(:increment).never subject.increment end it 'calls the #increment method for InstStatsd' do config.setup_metrics(:inststatsd, 'fake-namespace') expect(InstStatsd::Statsd).to receive(:increment).with( 'fake-namespace.quiz_api_client.request.count', tags: { method: method, status: code, url: url } ) subject.increment end end describe '#duration' do let(:duration_in_secs) { 10 } let(:start_time) { Time.parse('2021-11-09 15:11:48 -0500') } let(:end_time) { start_time + duration_in_secs } it 'does nothing when the metrics handler is nil' do allow(config).to receive(:metrics_handler).and_return(nil) expect(InstStatsd::Statsd).to receive(:timing).never subject.duration(start_time, end_time) end it 'does nothing when the metrics namespace is nil' do allow(config).to receive(:metrics_namespace).and_return(nil) expect(InstStatsd::Statsd).to receive(:timing).never subject.duration(start_time, end_time) end it 'calls the #timing method for InstStatsd' do config.setup_metrics(:inststatsd, 'fake-namespace') expect(InstStatsd::Statsd).to receive(:timing).with( 'fake-namespace.quiz_api_client.request.duration_ms', duration_in_secs * 1_000, tags: { method: method, status: code, url: url } ) subject.duration(start_time, end_time) end end end