Commit b8b90f2c authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'rc/remove_influxdb_entry_points' into 'master'

Remove influxdb entry points

See merge request gitlab-org/gitlab!31149
parents 48623138 bed42254
...@@ -18,7 +18,6 @@ linters: ...@@ -18,7 +18,6 @@ linters:
- "app/views/admin/application_settings/_abuse.html.haml" - "app/views/admin/application_settings/_abuse.html.haml"
- "app/views/admin/application_settings/_diff_limits.html.haml" - "app/views/admin/application_settings/_diff_limits.html.haml"
- "app/views/admin/application_settings/_gitaly.html.haml" - "app/views/admin/application_settings/_gitaly.html.haml"
- "app/views/admin/application_settings/_influx.html.haml"
- "app/views/admin/application_settings/_ip_limits.html.haml" - "app/views/admin/application_settings/_ip_limits.html.haml"
- "app/views/admin/application_settings/_performance.html.haml" - "app/views/admin/application_settings/_performance.html.haml"
- "app/views/admin/application_settings/_plantuml.html.haml" - "app/views/admin/application_settings/_plantuml.html.haml"
......
...@@ -135,7 +135,6 @@ end ...@@ -135,7 +135,6 @@ end
# loading of our custom migration templates. # loading of our custom migration templates.
if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && defined?(Rails::Generators)) if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && defined?(Rails::Generators))
require 'pathname' require 'pathname'
require 'influxdb'
require 'connection_pool' require 'connection_pool'
require 'method_source' require 'method_source'
...@@ -193,10 +192,6 @@ if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && d ...@@ -193,10 +192,6 @@ if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && d
GC::Profiler.enable GC::Profiler.enable
Gitlab::Cluster::LifecycleEvents.on_worker_start do
Gitlab::Metrics::Samplers::InfluxSampler.initialize_instance.start
end
module TrackNewRedisConnections module TrackNewRedisConnections
def connect(*args) def connect(*args)
val = super val = super
......
...@@ -2,17 +2,106 @@ ...@@ -2,17 +2,106 @@
module Gitlab module Gitlab
module Metrics module Metrics
include Gitlab::Metrics::InfluxDb
include Gitlab::Metrics::Prometheus include Gitlab::Metrics::Prometheus
include Gitlab::Metrics::Methods
EXECUTION_MEASUREMENT_BUCKETS = [0.001, 0.01, 0.1, 1].freeze
@error = false @error = false
def self.enabled? def self.enabled?
influx_metrics_enabled? || prometheus_metrics_enabled? prometheus_metrics_enabled?
end end
def self.error? def self.error?
@error @error
end end
# Tracks an event.
#
# See `Gitlab::Metrics::Transaction#add_event` for more details.
def self.add_event(*args)
current_transaction&.add_event(*args)
end
# Allow access from other metrics related middlewares
def self.current_transaction
Transaction.current
end
# Returns the prefix to use for the name of a series.
def self.series_prefix
@series_prefix ||= Gitlab::Runtime.sidekiq? ? 'sidekiq_' : 'rails_'
end
def self.settings
@settings ||= begin
current_settings = Gitlab::CurrentSettings.current_application_settings
{
method_call_threshold: current_settings[:metrics_method_call_threshold]
}
end
end
def self.method_call_threshold
# This is memoized since this method is called for every instrumented
# method. Loading data from an external cache on every method call slows
# things down too much.
# in milliseconds
@method_call_threshold ||= settings[:method_call_threshold]
end
# Measures the execution time of a block.
#
# Example:
#
# Gitlab::Metrics.measure(:find_by_username_duration) do
# UserFinder.new(some_username).find_by_username
# end
#
# name - The name of the field to store the execution time in.
#
# Returns the value yielded by the supplied block.
def self.measure(name)
trans = current_transaction
return yield unless trans
real_start = System.monotonic_time
cpu_start = System.cpu_time
retval = yield
cpu_stop = System.cpu_time
real_stop = System.monotonic_time
real_time = (real_stop - real_start)
cpu_time = cpu_stop - cpu_start
real_duration_seconds = fetch_histogram("gitlab_#{name}_real_duration_seconds".to_sym) do
docstring "Measure #{name}"
base_labels Transaction::BASE_LABELS
buckets EXECUTION_MEASUREMENT_BUCKETS
end
real_duration_seconds.observe(trans.labels, real_time)
cpu_duration_seconds = fetch_histogram("gitlab_#{name}_cpu_duration_seconds".to_sym) do
docstring "Measure #{name}"
base_labels Transaction::BASE_LABELS
buckets EXECUTION_MEASUREMENT_BUCKETS
with_feature "prometheus_metrics_measure_#{name}_cpu_duration"
end
cpu_duration_seconds.observe(trans.labels, cpu_time)
trans.increment("#{name}_real_time", real_time.in_milliseconds, false)
trans.increment("#{name}_cpu_time", cpu_time.in_milliseconds, false)
trans.increment("#{name}_call_count", 1, false)
retval
end
end end
end end
...@@ -20,10 +20,6 @@ module Gitlab ...@@ -20,10 +20,6 @@ module Gitlab
trans.add_event(:rails_exception) trans.add_event(:rails_exception)
raise error raise error
# Even in the event of an error we want to submit any metrics we
# might've gathered up to this point.
ensure
trans.finish
end end
retval retval
......
...@@ -17,8 +17,6 @@ module Gitlab ...@@ -17,8 +17,6 @@ module Gitlab
trans.add_event(:sidekiq_exception) trans.add_event(:sidekiq_exception)
raise error raise error
ensure
trans.finish
end end
end end
end end
......
...@@ -26,23 +26,17 @@ module Gitlab ...@@ -26,23 +26,17 @@ module Gitlab
private private
def track(event) def track(event)
values = values_for(event)
tags = tags_for(event) tags = tags_for(event)
self.class.gitlab_view_rendering_duration_seconds.observe(current_transaction.labels.merge(tags), event.duration) self.class.gitlab_view_rendering_duration_seconds.observe(current_transaction.labels.merge(tags), event.duration)
current_transaction.increment(:view_duration, event.duration) current_transaction.increment(:view_duration, event.duration)
current_transaction.add_metric(SERIES, values, tags)
end end
def relative_path(path) def relative_path(path)
path.gsub(%r{^#{Rails.root}/?}, '') path.gsub(%r{^#{Rails.root}/?}, '')
end end
def values_for(event)
{ duration: event.duration }
end
def tags_for(event) def tags_for(event)
path = relative_path(event.payload[:identifier]) path = relative_path(event.payload[:identifier])
......
...@@ -16,20 +16,18 @@ module Gitlab ...@@ -16,20 +16,18 @@ module Gitlab
# The series to store events (e.g. Git pushes) in. # The series to store events (e.g. Git pushes) in.
EVENT_SERIES = 'events' EVENT_SERIES = 'events'
attr_reader :tags, :values, :method, :metrics attr_reader :tags, :method
def self.current def self.current
Thread.current[THREAD_KEY] Thread.current[THREAD_KEY]
end end
def initialize def initialize
@metrics = []
@methods = {} @methods = {}
@started_at = nil @started_at = nil
@finished_at = nil @finished_at = nil
@values = Hash.new(0)
@tags = {} @tags = {}
@memory_before = 0 @memory_before = 0
...@@ -40,10 +38,6 @@ module Gitlab ...@@ -40,10 +38,6 @@ module Gitlab
@finished_at ? (@finished_at - @started_at) : 0.0 @finished_at ? (@finished_at - @started_at) : 0.0
end end
def duration_milliseconds
duration.in_milliseconds.to_i
end
def thread_cpu_duration def thread_cpu_duration
System.thread_cpu_duration(@thread_cputime_start) System.thread_cpu_duration(@thread_cputime_start)
end end
...@@ -71,10 +65,6 @@ module Gitlab ...@@ -71,10 +65,6 @@ module Gitlab
Thread.current[THREAD_KEY] = nil Thread.current[THREAD_KEY] = nil
end end
def add_metric(series, values, tags = {})
@metrics << Metric.new("#{::Gitlab::Metrics.series_prefix}#{series}", values, filter_tags(tags))
end
# Tracks a business level event # Tracks a business level event
# #
# Business level events including events such as Git pushes, Emails being # Business level events including events such as Git pushes, Emails being
...@@ -85,7 +75,6 @@ module Gitlab ...@@ -85,7 +75,6 @@ module Gitlab
def add_event(event_name, tags = {}) def add_event(event_name, tags = {})
filtered_tags = filter_tags(tags) filtered_tags = filter_tags(tags)
self.class.transaction_metric(event_name, :counter, prefix: 'event_', tags: filtered_tags).increment(filtered_tags.merge(labels)) self.class.transaction_metric(event_name, :counter, prefix: 'event_', tags: filtered_tags).increment(filtered_tags.merge(labels))
@metrics << Metric.new(EVENT_SERIES, { count: 1 }, filtered_tags.merge(event: event_name), :event)
end end
# Returns a MethodCall object for the given name. # Returns a MethodCall object for the given name.
...@@ -99,55 +88,16 @@ module Gitlab ...@@ -99,55 +88,16 @@ module Gitlab
def increment(name, value, use_prometheus = true) def increment(name, value, use_prometheus = true)
self.class.transaction_metric(name, :counter).increment(labels, value) if use_prometheus self.class.transaction_metric(name, :counter).increment(labels, value) if use_prometheus
@values[name] += value
end end
def set(name, value, use_prometheus = true) def set(name, value, use_prometheus = true)
self.class.transaction_metric(name, :gauge).set(labels, value) if use_prometheus self.class.transaction_metric(name, :gauge).set(labels, value) if use_prometheus
@values[name] = value
end
def finish
track_self
submit
end
def track_self
values = { duration: duration_milliseconds, allocated_memory: allocated_memory }
@values.each do |name, value|
values[name] = value
end
add_metric('transactions', values, @tags)
end
def submit
submit = @metrics.dup
@methods.each do |name, method|
submit << method.to_metric if method.above_threshold?
end
submit_hashes = submit.map do |metric|
hash = metric.to_hash
hash[:tags][:action] ||= action if action && !metric.event?
hash
end
::Gitlab::Metrics.submit_metrics(submit_hashes)
end end
def labels def labels
BASE_LABELS BASE_LABELS
end end
# returns string describing the action performed, usually the class plus method name.
def action
"#{labels[:controller]}##{labels[:action]}" if labels && !labels.empty?
end
define_histogram :gitlab_transaction_cputime_seconds do define_histogram :gitlab_transaction_cputime_seconds do
docstring 'Transaction thread cputime' docstring 'Transaction thread cputime'
base_labels BASE_LABELS base_labels BASE_LABELS
......
...@@ -172,7 +172,6 @@ module Gitlab ...@@ -172,7 +172,6 @@ module Gitlab
dependency_proxy_enabled: Gitlab.config.try(:dependency_proxy)&.enabled, dependency_proxy_enabled: Gitlab.config.try(:dependency_proxy)&.enabled,
gitlab_shared_runners_enabled: alt_usage_data { Gitlab.config.gitlab_ci.shared_runners_enabled }, gitlab_shared_runners_enabled: alt_usage_data { Gitlab.config.gitlab_ci.shared_runners_enabled },
gravatar_enabled: alt_usage_data { Gitlab::CurrentSettings.gravatar_enabled? }, gravatar_enabled: alt_usage_data { Gitlab::CurrentSettings.gravatar_enabled? },
influxdb_metrics_enabled: alt_usage_data { Gitlab::Metrics.influx_metrics_enabled? },
ldap_enabled: alt_usage_data { Gitlab.config.ldap.enabled }, ldap_enabled: alt_usage_data { Gitlab.config.ldap.enabled },
mattermost_enabled: alt_usage_data { Gitlab.config.mattermost.enabled }, mattermost_enabled: alt_usage_data { Gitlab.config.mattermost.enabled },
omniauth_enabled: alt_usage_data { Gitlab::Auth.omniauth_enabled? }, omniauth_enabled: alt_usage_data { Gitlab::Auth.omniauth_enabled? },
......
...@@ -5,15 +5,11 @@ require 'spec_helper' ...@@ -5,15 +5,11 @@ require 'spec_helper'
describe 'instrument_classes' do describe 'instrument_classes' do
let(:config) { double(:config) } let(:config) { double(:config) }
let(:influx_sampler) { double(:influx_sampler) }
before do before do
allow(config).to receive(:instrument_method) allow(config).to receive(:instrument_method)
allow(config).to receive(:instrument_methods) allow(config).to receive(:instrument_methods)
allow(config).to receive(:instrument_instance_method) allow(config).to receive(:instrument_instance_method)
allow(config).to receive(:instrument_instance_methods) allow(config).to receive(:instrument_instance_methods)
allow(Gitlab::Metrics::Samplers::InfluxSampler).to receive(:initialize_instance).and_return(influx_sampler)
allow(influx_sampler).to receive(:start)
allow(Gitlab::Application).to receive(:configure) allow(Gitlab::Application).to receive(:configure)
end end
......
...@@ -7,12 +7,6 @@ describe Gitlab::Metrics::BackgroundTransaction do ...@@ -7,12 +7,6 @@ describe Gitlab::Metrics::BackgroundTransaction do
subject { described_class.new(test_worker_class) } subject { described_class.new(test_worker_class) }
describe '#action' do
it 'returns transaction action name' do
expect(subject.action).to eq('TestWorker#perform')
end
end
describe '#label' do describe '#label' do
it 'returns labels based on class name' do it 'returns labels based on class name' do
expect(subject.labels).to eq(controller: 'TestWorker', action: 'perform') expect(subject.labels).to eq(controller: 'TestWorker', action: 'perform')
......
...@@ -10,10 +10,6 @@ describe Gitlab::Metrics::RackMiddleware do ...@@ -10,10 +10,6 @@ describe Gitlab::Metrics::RackMiddleware do
let(:env) { { 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/foo' } } let(:env) { { 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/foo' } }
describe '#call' do describe '#call' do
before do
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:finish)
end
it 'tracks a transaction' do it 'tracks a transaction' do
expect(app).to receive(:call).with(env).and_return('yay') expect(app).to receive(:call).with(env).and_return('yay')
...@@ -36,26 +32,5 @@ describe Gitlab::Metrics::RackMiddleware do ...@@ -36,26 +32,5 @@ describe Gitlab::Metrics::RackMiddleware do
it 'returns a Transaction' do it 'returns a Transaction' do
expect(transaction).to be_an_instance_of(Gitlab::Metrics::WebTransaction) expect(transaction).to be_an_instance_of(Gitlab::Metrics::WebTransaction)
end end
it 'stores the request method and URI in the transaction as values' do
expect(transaction.values[:request_method]).to eq('GET')
expect(transaction.values[:request_uri]).to eq('/foo')
end
context "when URI includes sensitive parameters" do
let(:env) do
{
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/foo?private_token=my-token',
'PATH_INFO' => '/foo',
'QUERY_STRING' => 'private_token=my_token',
'action_dispatch.parameter_filter' => [:private_token]
}
end
it 'stores the request URI with the sensitive parameters filtered' do
expect(transaction.values[:request_uri]).to eq('/foo?private_token=[FILTERED]')
end
end
end end
end end
...@@ -17,8 +17,6 @@ describe Gitlab::Metrics::SidekiqMiddleware do ...@@ -17,8 +17,6 @@ describe Gitlab::Metrics::SidekiqMiddleware do
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:set) expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:set)
.with(:sidekiq_queue_duration, instance_of(Float)) .with(:sidekiq_queue_duration, instance_of(Float))
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:finish)
middleware.call(worker, message, :test) { nil } middleware.call(worker, message, :test) { nil }
end end
...@@ -32,8 +30,6 @@ describe Gitlab::Metrics::SidekiqMiddleware do ...@@ -32,8 +30,6 @@ describe Gitlab::Metrics::SidekiqMiddleware do
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:set) expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:set)
.with(:sidekiq_queue_duration, instance_of(Float)) .with(:sidekiq_queue_duration, instance_of(Float))
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:finish)
middleware.call(worker, {}, :test) { nil } middleware.call(worker, {}, :test) { nil }
end end
...@@ -46,9 +42,6 @@ describe Gitlab::Metrics::SidekiqMiddleware do ...@@ -46,9 +42,6 @@ describe Gitlab::Metrics::SidekiqMiddleware do
expect_any_instance_of(Gitlab::Metrics::Transaction) expect_any_instance_of(Gitlab::Metrics::Transaction)
.to receive(:add_event).with(:sidekiq_exception) .to receive(:add_event).with(:sidekiq_exception)
expect_any_instance_of(Gitlab::Metrics::Transaction)
.to receive(:finish)
expect { middleware.call(worker, message, :test) } expect { middleware.call(worker, message, :test) }
.to raise_error(RuntimeError) .to raise_error(RuntimeError)
end end
......
...@@ -21,15 +21,9 @@ describe Gitlab::Metrics::Subscribers::ActionView do ...@@ -21,15 +21,9 @@ describe Gitlab::Metrics::Subscribers::ActionView do
describe '#render_template' do describe '#render_template' do
it 'tracks rendering of a template' do it 'tracks rendering of a template' do
values = { duration: 2.1 }
tags = { view: 'app/views/x.html.haml' }
expect(transaction).to receive(:increment) expect(transaction).to receive(:increment)
.with(:view_duration, 2.1) .with(:view_duration, 2.1)
expect(transaction).to receive(:add_metric)
.with(described_class::SERIES, values, tags)
subscriber.render_template(event) subscriber.render_template(event)
end end
......
...@@ -4,7 +4,6 @@ require 'spec_helper' ...@@ -4,7 +4,6 @@ require 'spec_helper'
describe Gitlab::Metrics::Transaction do describe Gitlab::Metrics::Transaction do
let(:transaction) { described_class.new } let(:transaction) { described_class.new }
let(:metric) { transaction.metrics[0] }
let(:sensitive_tags) do let(:sensitive_tags) do
{ {
...@@ -13,12 +12,6 @@ describe Gitlab::Metrics::Transaction do ...@@ -13,12 +12,6 @@ describe Gitlab::Metrics::Transaction do
} }
end end
shared_examples 'tag filter' do |sane_tags|
it 'filters potentially sensitive tags' do
expect(metric.tags).to eq(sane_tags)
end
end
describe '#duration' do describe '#duration' do
it 'returns the duration of a transaction in seconds' do it 'returns the duration of a transaction in seconds' do
transaction.run { } transaction.run { }
...@@ -61,25 +54,6 @@ describe Gitlab::Metrics::Transaction do ...@@ -61,25 +54,6 @@ describe Gitlab::Metrics::Transaction do
end end
end end
describe '#add_metric' do
it 'adds a metric to the transaction' do
transaction.add_metric('foo', value: 1)
expect(metric.series).to eq('rails_foo')
expect(metric.tags).to eq({})
expect(metric.values).to eq(value: 1)
end
context 'with sensitive tags' do
before do
transaction
.add_metric('foo', { value: 1 }, **sensitive_tags.merge(sane: 'yes'))
end
it_behaves_like 'tag filter', sane: 'yes'
end
end
describe '#method_call_for' do describe '#method_call_for' do
it 'returns a MethodCall' do it 'returns a MethodCall' do
method = transaction.method_call_for('Foo#bar', :Foo, '#bar') method = transaction.method_call_for('Foo#bar', :Foo, '#bar')
...@@ -88,133 +62,23 @@ describe Gitlab::Metrics::Transaction do ...@@ -88,133 +62,23 @@ describe Gitlab::Metrics::Transaction do
end end
end end
describe '#increment' do
it 'increments a counter' do
transaction.increment(:time, 1)
transaction.increment(:time, 2)
values = metric_values(time: 3)
expect(transaction).to receive(:add_metric)
.with('transactions', values, {})
transaction.track_self
end
end
describe '#set' do
it 'sets a value' do
transaction.set(:number, 10)
values = metric_values(number: 10)
expect(transaction).to receive(:add_metric)
.with('transactions', values, {})
transaction.track_self
end
end
describe '#finish' do
it 'tracks the transaction details and submits them to Sidekiq' do
expect(transaction).to receive(:track_self)
expect(transaction).to receive(:submit)
transaction.finish
end
end
describe '#track_self' do
it 'adds a metric for the transaction itself' do
values = metric_values
expect(transaction).to receive(:add_metric)
.with('transactions', values, {})
transaction.track_self
end
end
describe '#submit' do
it 'submits the metrics to Sidekiq' do
transaction.track_self
expect(Gitlab::Metrics).to receive(:submit_metrics)
.with([an_instance_of(Hash)])
transaction.submit
end
it 'adds the action as a tag for every metric' do
allow(transaction)
.to receive(:labels)
.and_return(controller: 'Foo', action: 'bar')
transaction.track_self
hash = {
series: 'rails_transactions',
tags: { action: 'Foo#bar' },
values: metric_values,
timestamp: a_kind_of(Integer)
}
expect(Gitlab::Metrics).to receive(:submit_metrics)
.with([hash])
transaction.submit
end
it 'does not add an action tag for events' do
allow(transaction)
.to receive(:labels)
.and_return(controller: 'Foo', action: 'bar')
transaction.add_event(:meow)
hash = {
series: 'events',
tags: { event: :meow },
values: { count: 1 },
timestamp: a_kind_of(Integer)
}
expect(Gitlab::Metrics).to receive(:submit_metrics)
.with([hash])
transaction.submit
end
end
describe '#add_event' do describe '#add_event' do
it 'adds a metric' do let(:prometheus_metric) { instance_double(Prometheus::Client::Counter, increment: nil) }
transaction.add_event(:meow)
expect(metric).to be_an_instance_of(Gitlab::Metrics::Metric)
end
it "does not prefix the metric's series name" do
transaction.add_event(:meow)
expect(metric.series).to eq(described_class::EVENT_SERIES) before do
allow(described_class).to receive(:transaction_metric).and_return(prometheus_metric)
end end
it 'tracks a counter for every event' do it 'adds a metric' do
transaction.add_event(:meow) expect(prometheus_metric).to receive(:increment)
expect(metric.values).to eq(count: 1)
end
it 'tracks the event name' do
transaction.add_event(:meow) transaction.add_event(:meow)
expect(metric.tags).to eq(event: :meow)
end end
it 'allows tracking of custom tags' do it 'allows tracking of custom tags' do
transaction.add_event(:bau, animal: 'dog') expect(prometheus_metric).to receive(:increment).with(hash_including(animal: "dog"))
expect(metric.tags).to eq(event: :bau, animal: 'dog') transaction.add_event(:bau, animal: 'dog')
end end
context 'with sensitive tags' do context 'with sensitive tags' do
...@@ -222,16 +86,11 @@ describe Gitlab::Metrics::Transaction do ...@@ -222,16 +86,11 @@ describe Gitlab::Metrics::Transaction do
transaction.add_event(:baubau, **sensitive_tags.merge(sane: 'yes')) transaction.add_event(:baubau, **sensitive_tags.merge(sane: 'yes'))
end end
it_behaves_like 'tag filter', event: :baubau, sane: 'yes' it 'filters tags' do
expect(prometheus_metric).not_to receive(:increment).with(hash_including(sensitive_tags))
transaction.add_event(:baubau, **sensitive_tags.merge(sane: 'yes'))
end end
end end
private
def metric_values(opts = {})
{
duration: 0.0,
allocated_memory: a_kind_of(Numeric)
}.merge(opts)
end end
end end
...@@ -5,6 +5,11 @@ require 'spec_helper' ...@@ -5,6 +5,11 @@ require 'spec_helper'
describe Gitlab::Metrics::WebTransaction do describe Gitlab::Metrics::WebTransaction do
let(:env) { {} } let(:env) { {} }
let(:transaction) { described_class.new(env) } let(:transaction) { described_class.new(env) }
let(:prometheus_metric) { double("prometheus metric") }
before do
allow(described_class).to receive(:transaction_metric).and_return(prometheus_metric)
end
describe '#duration' do describe '#duration' do
it 'returns the duration of a transaction in seconds' do it 'returns the duration of a transaction in seconds' do
...@@ -40,15 +45,6 @@ describe Gitlab::Metrics::WebTransaction do ...@@ -40,15 +45,6 @@ describe Gitlab::Metrics::WebTransaction do
end end
end end
describe '#add_metric' do
it 'adds a metric to the transaction' do
expect(Gitlab::Metrics::Metric).to receive(:new)
.with('rails_foo', { number: 10 }, {})
transaction.add_metric('foo', number: 10)
end
end
describe '#method_call_for' do describe '#method_call_for' do
it 'returns a MethodCall' do it 'returns a MethodCall' do
method = transaction.method_call_for('Foo#bar', :Foo, '#bar') method = transaction.method_call_for('Foo#bar', :Foo, '#bar')
...@@ -59,101 +55,17 @@ describe Gitlab::Metrics::WebTransaction do ...@@ -59,101 +55,17 @@ describe Gitlab::Metrics::WebTransaction do
describe '#increment' do describe '#increment' do
it 'increments a counter' do it 'increments a counter' do
transaction.increment(:time, 1) expect(prometheus_metric).to receive(:increment).with({}, 1)
transaction.increment(:time, 2)
values = { duration: 0.0, time: 3, allocated_memory: a_kind_of(Numeric) } transaction.increment(:time, 1)
expect(transaction).to receive(:add_metric)
.with('transactions', values, {})
transaction.track_self
end end
end end
describe '#set' do describe '#set' do
it 'sets a value' do it 'sets a value' do
transaction.set(:number, 10) expect(prometheus_metric).to receive(:set).with({}, 10)
values = {
duration: 0.0,
number: 10,
allocated_memory: a_kind_of(Numeric)
}
expect(transaction).to receive(:add_metric)
.with('transactions', values, {})
transaction.track_self
end
end
describe '#finish' do
it 'tracks the transaction details and submits them to Sidekiq' do
expect(transaction).to receive(:track_self)
expect(transaction).to receive(:submit)
transaction.finish
end
end
describe '#track_self' do
it 'adds a metric for the transaction itself' do
values = {
duration: transaction.duration,
allocated_memory: a_kind_of(Numeric)
}
expect(transaction).to receive(:add_metric)
.with('transactions', values, {})
transaction.track_self
end
end
describe '#submit' do
it 'submits the metrics to Sidekiq' do
transaction.track_self
expect(Gitlab::Metrics).to receive(:submit_metrics)
.with([an_instance_of(Hash)])
transaction.submit
end
it 'adds the action as a tag for every metric' do transaction.set(:number, 10)
allow(transaction).to receive(:labels).and_return(controller: 'Foo', action: 'bar')
transaction.track_self
hash = {
series: 'rails_transactions',
tags: { action: 'Foo#bar' },
values: { duration: 0.0, allocated_memory: a_kind_of(Numeric) },
timestamp: a_kind_of(Integer)
}
expect(Gitlab::Metrics).to receive(:submit_metrics)
.with([hash])
transaction.submit
end
it 'does not add an action tag for events' do
allow(transaction).to receive(:labels).and_return(controller: 'Foo', action: 'bar')
transaction.add_event(:meow)
hash = {
series: 'events',
tags: { event: :meow },
values: { count: 1 },
timestamp: a_kind_of(Integer)
}
expect(Gitlab::Metrics).to receive(:submit_metrics)
.with([hash])
transaction.submit
end end
end end
...@@ -167,7 +79,6 @@ describe Gitlab::Metrics::WebTransaction do ...@@ -167,7 +79,6 @@ describe Gitlab::Metrics::WebTransaction do
end end
it 'provides labels with the method and path of the route in the grape endpoint' do it 'provides labels with the method and path of the route in the grape endpoint' do
expect(transaction.labels).to eq({ controller: 'Grape', action: 'GET /projects/:id/archive' }) expect(transaction.labels).to eq({ controller: 'Grape', action: 'GET /projects/:id/archive' })
expect(transaction.action).to eq('Grape#GET /projects/:id/archive')
end end
it 'does not provide labels if route infos are missing' do it 'does not provide labels if route infos are missing' do
...@@ -177,7 +88,6 @@ describe Gitlab::Metrics::WebTransaction do ...@@ -177,7 +88,6 @@ describe Gitlab::Metrics::WebTransaction do
env['api.endpoint'] = endpoint env['api.endpoint'] = endpoint
expect(transaction.labels).to eq({}) expect(transaction.labels).to eq({})
expect(transaction.action).to be_nil
end end
end end
...@@ -193,7 +103,6 @@ describe Gitlab::Metrics::WebTransaction do ...@@ -193,7 +103,6 @@ describe Gitlab::Metrics::WebTransaction do
it 'tags a transaction with the name and action of a controller' do it 'tags a transaction with the name and action of a controller' do
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show' }) expect(transaction.labels).to eq({ controller: 'TestController', action: 'show' })
expect(transaction.action).to eq('TestController#show')
end end
context 'when the request content type is not :html' do context 'when the request content type is not :html' do
...@@ -201,7 +110,6 @@ describe Gitlab::Metrics::WebTransaction do ...@@ -201,7 +110,6 @@ describe Gitlab::Metrics::WebTransaction do
it 'appends the mime type to the transaction action' do it 'appends the mime type to the transaction action' do
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show.json' }) expect(transaction.labels).to eq({ controller: 'TestController', action: 'show.json' })
expect(transaction.action).to eq('TestController#show.json')
end end
end end
...@@ -210,54 +118,26 @@ describe Gitlab::Metrics::WebTransaction do ...@@ -210,54 +118,26 @@ describe Gitlab::Metrics::WebTransaction do
it 'does not append the MIME type to the transaction action' do it 'does not append the MIME type to the transaction action' do
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show' }) expect(transaction.labels).to eq({ controller: 'TestController', action: 'show' })
expect(transaction.action).to eq('TestController#show')
end end
end end
end end
it 'returns no labels when no route information is present in env' do it 'returns no labels when no route information is present in env' do
expect(transaction.labels).to eq({}) expect(transaction.labels).to eq({})
expect(transaction.action).to eq(nil)
end end
end end
describe '#add_event' do describe '#add_event' do
it 'adds a metric' do it 'adds a metric' do
transaction.add_event(:meow) expect(prometheus_metric).to receive(:increment)
expect(transaction.metrics[0]).to be_an_instance_of(Gitlab::Metrics::Metric)
end
it "does not prefix the metric's series name" do
transaction.add_event(:meow) transaction.add_event(:meow)
metric = transaction.metrics[0]
expect(metric.series).to eq(described_class::EVENT_SERIES)
end
it 'tracks a counter for every event' do
transaction.add_event(:meow)
metric = transaction.metrics[0]
expect(metric.values).to eq(count: 1)
end
it 'tracks the event name' do
transaction.add_event(:meow)
metric = transaction.metrics[0]
expect(metric.tags).to eq(event: :meow)
end end
it 'allows tracking of custom tags' do it 'allows tracking of custom tags' do
transaction.add_event(:bau, animal: 'dog') expect(prometheus_metric).to receive(:increment).with(animal: "dog")
metric = transaction.metrics[0]
expect(metric.tags).to eq(event: :bau, animal: 'dog') transaction.add_event(:bau, animal: 'dog')
end end
end end
end end
...@@ -53,60 +53,6 @@ describe Gitlab::Metrics do ...@@ -53,60 +53,6 @@ describe Gitlab::Metrics do
end end
end end
describe '.influx_metrics_enabled?' do
it 'returns a boolean' do
expect(described_class.influx_metrics_enabled?).to be_in([true, false])
end
end
describe '.submit_metrics' do
it 'prepares and writes the metrics to InfluxDB' do
connection = double(:connection)
pool = double(:pool)
expect(pool).to receive(:with).and_yield(connection)
expect(connection).to receive(:write_points).with(an_instance_of(Array))
expect(described_class).to receive(:pool).and_return(pool)
described_class.submit_metrics([{ 'series' => 'kittens', 'tags' => {} }])
end
end
describe '.prepare_metrics' do
it 'returns a Hash with the keys as Symbols' do
metrics = described_class
.prepare_metrics([{ 'values' => {}, 'tags' => {} }])
expect(metrics).to eq([{ values: {}, tags: {} }])
end
it 'escapes tag values' do
metrics = described_class.prepare_metrics([
{ 'values' => {}, 'tags' => { 'foo' => 'bar=' } }
])
expect(metrics).to eq([{ values: {}, tags: { 'foo' => 'bar\\=' } }])
end
it 'drops empty tags' do
metrics = described_class.prepare_metrics([
{ 'values' => {}, 'tags' => { 'cats' => '', 'dogs' => nil } }
])
expect(metrics).to eq([{ values: {}, tags: {} }])
end
end
describe '.escape_value' do
it 'escapes an equals sign' do
expect(described_class.escape_value('foo=')).to eq('foo\\=')
end
it 'casts values to Strings' do
expect(described_class.escape_value(10)).to eq('10')
end
end
describe '.measure' do describe '.measure' do
context 'without a transaction' do context 'without a transaction' do
it 'returns the return value of the block' do it 'returns the return value of the block' do
...@@ -145,30 +91,6 @@ describe Gitlab::Metrics do ...@@ -145,30 +91,6 @@ describe Gitlab::Metrics do
end end
end end
describe '.action=' do
context 'without a transaction' do
it 'does nothing' do
expect_any_instance_of(Gitlab::Metrics::Transaction)
.not_to receive(:action=)
described_class.action = 'foo'
end
end
context 'with a transaction' do
it 'sets the action of a transaction' do
trans = Gitlab::Metrics::WebTransaction.new({})
expect(described_class).to receive(:current_transaction)
.and_return(trans)
expect(trans).to receive(:action=).with('foo')
described_class.action = 'foo'
end
end
end
describe '#series_prefix' do describe '#series_prefix' do
it 'returns a String' do it 'returns a String' do
expect(described_class.series_prefix).to be_an_instance_of(String) expect(described_class.series_prefix).to be_an_instance_of(String)
......
...@@ -133,7 +133,6 @@ module UsageDataHelpers ...@@ -133,7 +133,6 @@ module UsageDataHelpers
gitaly gitaly
database database
avg_cycle_analytics avg_cycle_analytics
influxdb_metrics_enabled
prometheus_metrics_enabled prometheus_metrics_enabled
web_ide_clientside_preview_enabled web_ide_clientside_preview_enabled
ingress_modsecurity_enabled ingress_modsecurity_enabled
......
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