Commit dc9ee8e2 authored by Alper Akgun's avatar Alper Akgun

Harden Prometheus client wrapper

parent 77a98f10
---
title: Harden Prometheus client usage data wrapper
merge_request: 56210
author:
type: other
...@@ -113,11 +113,13 @@ module Gitlab ...@@ -113,11 +113,13 @@ module Gitlab
end end
end end
def with_prometheus_client(fallback: nil, verify: true) def with_prometheus_client(fallback: {}, verify: true)
client = prometheus_client(verify: verify) client = prometheus_client(verify: verify)
return fallback unless client return fallback unless client
yield client yield client
rescue
fallback
end end
def measure_duration def measure_duration
......
...@@ -246,6 +246,13 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -246,6 +246,13 @@ RSpec.describe Gitlab::Utils::UsageData do
end end
describe '#with_prometheus_client' do 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 shared_examples 'query data from Prometheus' do
it 'yields a client instance and returns the block result' do it 'yields a client instance and returns the block result' do
result = described_class.with_prometheus_client { |client| client } result = described_class.with_prometheus_client { |client| client }
...@@ -255,10 +262,10 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -255,10 +262,10 @@ RSpec.describe Gitlab::Utils::UsageData do
end end
shared_examples 'does not query data from Prometheus' do 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 } result = described_class.with_prometheus_client { |client| client }
expect(result).to be_nil expect(result).to eq({})
end end
it 'returns fallback if provided' do 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