Commit 66c1acba authored by Pawel Chojnacki's avatar Pawel Chojnacki

Fix code after refactoring

parent 53f818fd
...@@ -9,6 +9,10 @@ module Gitlab ...@@ -9,6 +9,10 @@ module Gitlab
end end
class_methods do class_methods do
def reload_metric!(name)
@@_metrics_provider_cache.delete(name)
end
private private
def define_metric(type, name, opts = {}, &block) def define_metric(type, name, opts = {}, &block)
...@@ -16,7 +20,7 @@ module Gitlab ...@@ -16,7 +20,7 @@ module Gitlab
raise ArgumentError, "metrics method #{name} already exists" raise ArgumentError, "metrics method #{name} already exists"
end end
define_method(name) do define_singleton_method(name) do
# avoid unnecessary method call to speed up metric access # avoid unnecessary method call to speed up metric access
return @@_metrics_provider_cache[name] if @@_metrics_provider_cache.has_key?(name) return @@_metrics_provider_cache[name] if @@_metrics_provider_cache.has_key?(name)
...@@ -45,12 +49,11 @@ module Gitlab ...@@ -45,12 +49,11 @@ module Gitlab
case type case type
when :gauge when :gauge
Gitlab::Metrics.gauge(name, options.docs, options.base_labels, options.multiprocess_mode) Gitlab::Metrics.gauge(name, options.docstring, options.base_labels, options.multiprocess_mode)
when :counter when :counter
Gitlab::Metrics.counter(name, options.docs, options.base_labels) Gitlab::Metrics.counter(name, options.docstring, options.base_labels)
when :histogram when :histogram
options[:buckets] ||= ::Prometheus::Client::Histogram::DEFAULT_BUCKETS Gitlab::Metrics.histogram(name, options.docstring, options.base_labels, options.buckets)
Gitlab::Metrics.histogram(name, options.docs, options.base_labels, options.buckets)
when :summary when :summary
raise NotImplementedError, "summary metrics are not currently supported" raise NotImplementedError, "summary metrics are not currently supported"
else else
...@@ -58,12 +61,6 @@ module Gitlab ...@@ -58,12 +61,6 @@ module Gitlab
end end
end end
counter :global do
docstring "Global counter"
multiprocess_mode :all
buckets [0, 1]
end
# Fetch and/or initialize counter metric # Fetch and/or initialize counter metric
# @param [Symbol] name # @param [Symbol] name
# @param [Hash] opts # @param [Hash] opts
......
...@@ -6,34 +6,36 @@ module Gitlab ...@@ -6,34 +6,36 @@ module Gitlab
@multiprocess_mode = options[:multiprocess_mode] || :all @multiprocess_mode = options[:multiprocess_mode] || :all
@buckets = options[:buckets] || ::Prometheus::Client::Histogram::DEFAULT_BUCKETS @buckets = options[:buckets] || ::Prometheus::Client::Histogram::DEFAULT_BUCKETS
@base_labels = options[:base_labels] || {} @base_labels = options[:base_labels] || {}
@docstring = options[:docstring]
@with_feature = options[:with_feature]
end end
def docs(docs = nil) def docstring(docstring = nil)
@docs = docs unless docs.nil? @docstring = docstring unless docstring.nil?
@docs @docstring
end end
def multiprocess_mode(mode) def multiprocess_mode(mode = nil)
@multiprocess_mode = mode unless @multiprocess_mode.nil? @multiprocess_mode = mode unless mode.nil?
@multiprocess_mode @multiprocess_mode
end end
def buckets(buckets) def buckets(buckets = nil)
@buckets = buckets unless @buckets.nil? @buckets = buckets unless buckets.nil?
@buckets @buckets
end end
def base_labels(base_labels) def base_labels(base_labels = nil)
@base_labels = base_labels unless @base_labels.nil? @base_labels = base_labels unless base_labels.nil?
@base_labels @base_labels
end end
def with_feature(name) def with_feature(name = nil)
@feature_name = name unless @feature_name.nil? @feature_name = name unless name.nil?
@feature_name @feature_name
end end
......
...@@ -8,10 +8,11 @@ module Gitlab ...@@ -8,10 +8,11 @@ module Gitlab
BASE_LABELS = { module: nil, method: nil }.freeze BASE_LABELS = { module: nil, method: nil }.freeze
attr_reader :real_time, :cpu_time, :call_count, :labels attr_reader :real_time, :cpu_time, :call_count, :labels
histogram :gitlab_method_call_duration_seconds, 'Method calls real duration', define_histogram :gitlab_method_call_duration_seconds,
base_labels: Transaction::BASE_LABELS.merge(BASE_LABELS), docstring: 'Method calls real duration',
buckets: [0.01, 0.05, 0.1, 0.5, 1], base_labels: Transaction::BASE_LABELS.merge(BASE_LABELS),
with_feature: :prometheus_metrics_method_instrumentation buckets: [0.01, 0.05, 0.1, 0.5, 1],
with_feature: :prometheus_metrics_method_instrumentation
# name - The full name of the method (including namespace) such as # name - The full name of the method (including namespace) such as
# `User#sign_in`. # `User#sign_in`.
......
...@@ -2,11 +2,12 @@ module Gitlab ...@@ -2,11 +2,12 @@ module Gitlab
module Metrics module Metrics
# Class for storing metrics information of a single transaction. # Class for storing metrics information of a single transaction.
class Transaction class Transaction
include Gitlab::Metrics::Concern
# base labels shared among all transactions # base labels shared among all transactions
BASE_LABELS = { controller: nil, action: nil }.freeze BASE_LABELS = { controller: nil, action: nil }.freeze
THREAD_KEY = :_gitlab_metrics_transaction THREAD_KEY = :_gitlab_metrics_transaction
METRICS_MUTEX = Mutex.new
# The series to store events (e.g. Git pushes) in. # The series to store events (e.g. Git pushes) in.
EVENT_SERIES = 'events'.freeze EVENT_SERIES = 'events'.freeze
...@@ -136,27 +137,25 @@ module Gitlab ...@@ -136,27 +137,25 @@ module Gitlab
"#{labels[:controller]}##{labels[:action]}" if labels && !labels.empty? "#{labels[:controller]}##{labels[:action]}" if labels && !labels.empty?
end end
histogram :gitlab_transaction_duration_seconds, 'Transaction duration', define_histogram :gitlab_transaction_duration_seconds,
base_labels: BASE_LABELS, docstring: 'Transaction duration',
buckets: [0.001, 0.01, 0.1, 0.5, 10.0], base_labels: BASE_LABELS,
with_feature: :prometheus_metrics_method_instrumentation buckets: [0.001, 0.01, 0.1, 0.5, 10.0],
with_feature: :prometheus_metrics_method_instrumentation
histogram :gitlab_transaction_allocated_memory_bytes, 'Transaction allocated memory bytes', define_histogram :gitlab_transaction_allocated_memory_bytes,
base_labels: BASE_LABELS, docstring: 'Transaction allocated memory bytes',
buckets: [100, 1000, 10000, 100000, 1000000, 10000000] base_labels: BASE_LABELS,
buckets: [100, 1000, 10000, 100000, 1000000, 10000000]
def self.transaction_metric(name, type, prefix: nil, tags: {}) def self.transaction_metric(name, type, prefix: nil, tags: {})
return @transaction_metric[name] if @transaction_metric[name]&.has_key?(name) metric_name = "gitlab_transaction_#{prefix}#{name}_total".to_sym
fetch_metric(type, metric_name) do
METRICS_MUTEX.synchronize do docstring "Transaction #{prefix}#{name} #{type}"
@transaction_metric ||= {} base_labels tags.merge(BASE_LABELS)
@transaction_metric[name] ||= if type == :counter if type == :gauge
Gitlab::Metrics.counter("gitlab_transaction_#{prefix}#{name}_total".to_sym, multiprocess_mode :livesum
"Transaction #{prefix}#{name} counter", tags.merge(BASE_LABELS)) end
else
Gitlab::Metrics.gauge("gitlab_transaction_#{name}".to_sym,
"Transaction gauge #{name} ", tags.merge(BASE_LABELS), :livesum)
end
end end
end end
end end
......
...@@ -6,7 +6,7 @@ describe Gitlab::Metrics::MethodCall do ...@@ -6,7 +6,7 @@ describe Gitlab::Metrics::MethodCall do
describe '#measure' do describe '#measure' do
before do before do
described_class.reload_gitlab_method_call_duration_seconds! described_class.reload_metric!(:gitlab_method_call_duration_seconds)
end end
it 'measures the performance of the supplied block' do it 'measures the performance of the supplied block' 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