Skip to content

Commit

Permalink
Use registry as singleton object
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmad Tolba <tolpa1@gmail.com>
  • Loading branch information
ahmgeek committed Sep 11, 2019
1 parent bb20bd9 commit 8550da5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/prometheus/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Prometheus
module Client
# Returns a default registry object
def self.registry
@registry ||= Registry.new
Registry.instance
end

def self.config
Expand Down
3 changes: 3 additions & 0 deletions lib/prometheus/client/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
require 'prometheus/client/gauge'
require 'prometheus/client/histogram'
require 'concurrent'
require 'singleton'

module Prometheus
module Client
# Registry
class Registry
class AlreadyRegisteredError < StandardError; end

include Singleton

def initialize
@metrics = {}
@lock = Concurrent::ReentrantReadWriteLock.new
Expand Down
2 changes: 1 addition & 1 deletion spec/prometheus/client/formats/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Prometheus::Client.config.data_store = Prometheus::Client::DataStores::Synchronized.new
end

let(:registry) { Prometheus::Client::Registry.new }
let(:registry) { Prometheus::Client::Registry.instance }

before do
foo = registry.counter(:foo,
Expand Down
8 changes: 7 additions & 1 deletion spec/prometheus/client/registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'prometheus/client/registry'

describe Prometheus::Client::Registry do
let(:registry) { Prometheus::Client::Registry.new }
let(:registry) { Prometheus::Client::Registry.instance }

describe '.new' do
it 'returns a new registry instance' do
Expand All @@ -13,6 +13,8 @@
end

describe '#register' do
let(:registry) { Prometheus::Client::Registry.clone.instance }

it 'registers a new metric container and returns it' do
metric = double(name: :test)

Expand Down Expand Up @@ -89,6 +91,8 @@ def registry.exist?(*args)
end

describe '#exist?' do
let(:registry) { Prometheus::Client::Registry.clone.instance }

it 'returns true if a metric name has been registered' do
registry.register(double(name: :test))

Expand All @@ -101,6 +105,8 @@ def registry.exist?(*args)
end

describe '#get' do
let(:registry) { Prometheus::Client::Registry.clone.instance }

it 'returns a previously registered metric container' do
registry.register(double(name: :test))

Expand Down
25 changes: 15 additions & 10 deletions spec/prometheus/middleware/collector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

let(:registry) do
Prometheus::Client::Registry.new
Prometheus::Client::Registry.instance
end

let(:original_app) do
Expand Down Expand Up @@ -41,18 +41,22 @@
expect(last_response).to be_ok
end

it 'traces request information' do
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.2)
context 'trace' do
let(:registry) { Prometheus::Client::Registry.clone.instance }

get '/foo'
it 'traces request information' do
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.2)

metric = :http_server_requests_total
labels = { method: 'get', path: '/foo', code: '200' }
expect(registry.get(metric).get(labels: labels)).to eql(1.0)
get '/foo'

metric = :http_server_request_duration_seconds
labels = { method: 'get', path: '/foo' }
expect(registry.get(metric).get(labels: labels)).to include("0.1" => 0, "0.25" => 1)
metric = :http_server_requests_total
labels = { method: 'get', path: '/foo', code: '200' }
expect(registry.get(metric).get(labels: labels)).to eql(1.0)

metric = :http_server_request_duration_seconds
labels = { method: 'get', path: '/foo' }
expect(registry.get(metric).get(labels: labels)).to include("0.1" => 0, "0.25" => 1)
end
end

it 'normalizes paths containing numeric IDs by default' do
Expand Down Expand Up @@ -113,6 +117,7 @@
metrics_prefix: 'lolrus',
)
end
let(:registry) { Prometheus::Client::Registry.clone.instance }

it 'provides alternate metric names' do
expect(
Expand Down
2 changes: 1 addition & 1 deletion spec/prometheus/middleware/exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
include Rack::Test::Methods

let(:registry) do
Prometheus::Client::Registry.new
Prometheus::Client::Registry.instance
end

let(:app) do
Expand Down

0 comments on commit 8550da5

Please sign in to comment.