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

Fix code after refactoring

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