Commit 8b902393 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'aa-harden-with-prometheus-callback' into 'master'

Harden Prometheus client wrapper

See merge request gitlab-org/gitlab!56210
parents a33dd1ce dc9ee8e2
---
title: Harden Prometheus client usage data wrapper
merge_request: 56210
author:
type: other
......@@ -113,11 +113,13 @@ module Gitlab
end
end
def with_prometheus_client(fallback: nil, verify: true)
def with_prometheus_client(fallback: {}, verify: true)
client = prometheus_client(verify: verify)
return fallback unless client
yield client
rescue
fallback
end
def measure_duration
......
......@@ -246,6 +246,13 @@ RSpec.describe Gitlab::Utils::UsageData do
end
describe '#with_prometheus_client' do
it 'returns fallback with for an exception in yield block' do
allow(described_class).to receive(:prometheus_client).and_return(Gitlab::PrometheusClient.new('http://localhost:9090'))
result = described_class.with_prometheus_client(fallback: -42) { |client| raise StandardError }
expect(result).to be(-42)
end
shared_examples 'query data from Prometheus' do
it 'yields a client instance and returns the block result' do
result = described_class.with_prometheus_client { |client| client }
......@@ -255,10 +262,10 @@ RSpec.describe Gitlab::Utils::UsageData do
end
shared_examples 'does not query data from Prometheus' do
it 'returns nil by default' do
it 'returns {} by default' do
result = described_class.with_prometheus_client { |client| client }
expect(result).to be_nil
expect(result).to eq({})
end
it 'returns fallback if provided' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment