Commit 7d716cc8 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Convert InfluxDB to concern. Fix uninitialized metrics when metrics code is inherited.

parent ea6196d4
module Gitlab module Gitlab
module Metrics module Metrics
extend Gitlab::Metrics::InfluxDb include Gitlab::Metrics::InfluxDb
include Gitlab::Metrics::Prometheus include Gitlab::Metrics::Prometheus
def self.enabled? def self.enabled?
......
...@@ -6,7 +6,7 @@ module Gitlab ...@@ -6,7 +6,7 @@ module Gitlab
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
@_metric_provider_mutex = Mutex.new @@_metric_provider_mutex = Mutex.new
@_metrics_provider_cache = {} @_metrics_provider_cache = {}
end end
...@@ -23,12 +23,12 @@ module Gitlab ...@@ -23,12 +23,12 @@ module Gitlab
end end
define_singleton_method(name) do define_singleton_method(name) do
@_metrics_provider_cache[name] || init_metric(type, name, opts, &block) @_metrics_provider_cache&.[](name) || init_metric(type, name, opts, &block)
end end
end end
def fetch_metric(type, name, opts = {}, &block) def fetch_metric(type, name, opts = {}, &block)
@_metrics_provider_cache[name] || init_metric(type, name, opts, &block) @_metrics_provider_cache&.[](name) || init_metric(type, name, opts, &block)
end end
def init_metric(type, name, opts = {}, &block) def init_metric(type, name, opts = {}, &block)
...@@ -43,7 +43,8 @@ module Gitlab ...@@ -43,7 +43,8 @@ module Gitlab
end end
def synchronized_cache_fill(key) def synchronized_cache_fill(key)
@_metric_provider_mutex.synchronize do @@_metric_provider_mutex.synchronize do
@_metrics_provider_cache ||= {}
@_metrics_provider_cache[key] ||= yield @_metrics_provider_cache[key] ||= yield
end end
end end
......
module Gitlab module Gitlab
module Metrics module Metrics
module InfluxDb module InfluxDb
extend ActiveSupport::Concern
include Gitlab::Metrics::Concern include Gitlab::Metrics::Concern
include Gitlab::CurrentSettings
extend self EXECUTION_MEASUREMENT_BUCKETS = [0.001, 0.01, 0.1, 1].freeze
RAILS_ROOT = Rails.root.to_s
METRICS_ROOT = Rails.root.join('lib', 'gitlab', 'metrics').to_s
PATH_REGEX = /^#{RAILS_ROOT}\/?/
MUTEX = Mutex.new MUTEX = Mutex.new
private_constant :MUTEX private_constant :MUTEX
class_methods do
include Gitlab::CurrentSettings
def influx_metrics_enabled? def influx_metrics_enabled?
settings[:enabled] || false settings[:enabled] || false
end end
# Prometheus histogram buckets used for arbitrary code measurements # Prometheus histogram buckets used for arbitrary code measurements
EXECUTION_MEASUREMENT_BUCKETS = [0.001, 0.01, 0.1, 1].freeze
RAILS_ROOT = Rails.root.to_s
METRICS_ROOT = Rails.root.join('lib', 'gitlab', 'metrics').to_s
PATH_REGEX = /^#{RAILS_ROOT}\/?/
def settings def settings
@settings ||= { @settings ||= {
...@@ -181,4 +184,5 @@ module Gitlab ...@@ -181,4 +184,5 @@ module Gitlab
# rubocop:enable Gitlab/ModuleWithInstanceVariables # rubocop:enable Gitlab/ModuleWithInstanceVariables
end end
end end
end
end end
...@@ -4,13 +4,12 @@ module Gitlab ...@@ -4,13 +4,12 @@ module Gitlab
module Metrics module Metrics
module Prometheus module Prometheus
extend ActiveSupport::Concern extend ActiveSupport::Concern
include Gitlab::Metrics::Concern
include Gitlab::CurrentSettings
REGISTRY_MUTEX = Mutex.new REGISTRY_MUTEX = Mutex.new
PROVIDER_MUTEX = Mutex.new PROVIDER_MUTEX = Mutex.new
class_methods do class_methods do
include Gitlab::CurrentSettings
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
def metrics_folder_present? def metrics_folder_present?
......
...@@ -144,7 +144,10 @@ describe Gitlab::Metrics::Subscribers::RailsCache do ...@@ -144,7 +144,10 @@ describe Gitlab::Metrics::Subscribers::RailsCache do
end end
context 'with a transaction' do context 'with a transaction' do
let(:metric_cache_misses_total) { double('metric_cache_misses_total', increment: nil) }
before do before do
allow(subscriber).to receive(:metric_cache_misses_total).and_return(metric_cache_misses_total)
allow(subscriber).to receive(:current_transaction) allow(subscriber).to receive(:current_transaction)
.and_return(transaction) .and_return(transaction)
end end
...@@ -157,9 +160,9 @@ describe Gitlab::Metrics::Subscribers::RailsCache do ...@@ -157,9 +160,9 @@ describe Gitlab::Metrics::Subscribers::RailsCache do
end end
it 'increments the cache_read_miss total' do it 'increments the cache_read_miss total' do
expect(subscriber.send(:metric_cache_misses_total)).to receive(:increment).with({})
subscriber.cache_generate(event) subscriber.cache_generate(event)
expect(metric_cache_misses_total).to have_received(:increment).with({})
end end
end end
end end
......
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