gitlab-org--gitlab-foss/spec/lib/gitlab/metrics_spec.rb

354 lines
9.3 KiB
Ruby
Raw Normal View History

Storing of application metrics in InfluxDB This adds the ability to write application metrics (e.g. SQL timings) to InfluxDB. These metrics can in turn be visualized using Grafana, or really anything else that can read from InfluxDB. These metrics can be used to track application performance over time, between different Ruby versions, different GitLab versions, etc. == Transaction Metrics Currently the following is tracked on a per transaction basis (a transaction is a Rails request or a single Sidekiq job): * Timings per query along with the raw (obfuscated) SQL and information about what file the query originated from. * Timings per view along with the path of the view and information about what file triggered the rendering process. * The duration of a request itself along with the controller/worker class and method name. * The duration of any instrumented method calls (more below). == Sampled Metrics Certain metrics can't be directly associated with a transaction. For example, a process' total memory usage is unrelated to any running transactions. While a transaction can result in the memory usage going up there's no accurate way to determine what transaction is to blame, this becomes especially problematic in multi-threaded environments. To solve this problem there's a separate thread that takes samples at a fixed interval. This thread (using the class Gitlab::Metrics::Sampler) currently tracks the following: * The process' total memory usage. * The number of file descriptors opened by the process. * The amount of Ruby objects (using ObjectSpace.count_objects). * GC statistics such as timings, heap slots, etc. The default/current interval is 15 seconds, any smaller interval might put too much pressure on InfluxDB (especially when running dozens of processes). == Method Instrumentation While currently not yet used methods can be instrumented to track how long they take to run. Unlike the likes of New Relic this doesn't require modifying the source code (e.g. including modules), it all happens from the outside. For example, to track `User.by_login` we'd add the following code somewhere in an initializer: Gitlab::Metrics::Instrumentation. instrument_method(User, :by_login) to instead instrument an instance method: Gitlab::Metrics::Instrumentation. instrument_instance_method(User, :save) Instrumentation for either all public model methods or a few crucial ones will be added in the near future, I simply haven't gotten to doing so just yet. == Configuration By default metrics are disabled. This means users don't have to bother setting anything up if they don't want to. Metrics can be enabled by editing one's gitlab.yml configuration file (see config/gitlab.yml.example for example settings). == Writing Data To InfluxDB Because InfluxDB is still a fairly young product I expect the worse. Data loss, unexpected reboots, the database not responding, you name it. Because of this data is _not_ written to InfluxDB directly, instead it's queued and processed by Sidekiq. This ensures that users won't notice anything when InfluxDB is giving trouble. The metrics worker can be started in a standalone manner as following: bundle exec sidekiq -q metrics The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
require 'spec_helper'
describe Gitlab::Metrics do
include StubENV
describe '.settings' do
it 'returns a Hash' do
expect(described_class.settings).to be_an_instance_of(Hash)
Storing of application metrics in InfluxDB This adds the ability to write application metrics (e.g. SQL timings) to InfluxDB. These metrics can in turn be visualized using Grafana, or really anything else that can read from InfluxDB. These metrics can be used to track application performance over time, between different Ruby versions, different GitLab versions, etc. == Transaction Metrics Currently the following is tracked on a per transaction basis (a transaction is a Rails request or a single Sidekiq job): * Timings per query along with the raw (obfuscated) SQL and information about what file the query originated from. * Timings per view along with the path of the view and information about what file triggered the rendering process. * The duration of a request itself along with the controller/worker class and method name. * The duration of any instrumented method calls (more below). == Sampled Metrics Certain metrics can't be directly associated with a transaction. For example, a process' total memory usage is unrelated to any running transactions. While a transaction can result in the memory usage going up there's no accurate way to determine what transaction is to blame, this becomes especially problematic in multi-threaded environments. To solve this problem there's a separate thread that takes samples at a fixed interval. This thread (using the class Gitlab::Metrics::Sampler) currently tracks the following: * The process' total memory usage. * The number of file descriptors opened by the process. * The amount of Ruby objects (using ObjectSpace.count_objects). * GC statistics such as timings, heap slots, etc. The default/current interval is 15 seconds, any smaller interval might put too much pressure on InfluxDB (especially when running dozens of processes). == Method Instrumentation While currently not yet used methods can be instrumented to track how long they take to run. Unlike the likes of New Relic this doesn't require modifying the source code (e.g. including modules), it all happens from the outside. For example, to track `User.by_login` we'd add the following code somewhere in an initializer: Gitlab::Metrics::Instrumentation. instrument_method(User, :by_login) to instead instrument an instance method: Gitlab::Metrics::Instrumentation. instrument_instance_method(User, :save) Instrumentation for either all public model methods or a few crucial ones will be added in the near future, I simply haven't gotten to doing so just yet. == Configuration By default metrics are disabled. This means users don't have to bother setting anything up if they don't want to. Metrics can be enabled by editing one's gitlab.yml configuration file (see config/gitlab.yml.example for example settings). == Writing Data To InfluxDB Because InfluxDB is still a fairly young product I expect the worse. Data loss, unexpected reboots, the database not responding, you name it. Because of this data is _not_ written to InfluxDB directly, instead it's queued and processed by Sidekiq. This ensures that users won't notice anything when InfluxDB is giving trouble. The metrics worker can be started in a standalone manner as following: bundle exec sidekiq -q metrics The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
end
end
describe '.enabled?' do
it 'returns a boolean' do
expect(described_class.enabled?).to be_in([true, false])
Storing of application metrics in InfluxDB This adds the ability to write application metrics (e.g. SQL timings) to InfluxDB. These metrics can in turn be visualized using Grafana, or really anything else that can read from InfluxDB. These metrics can be used to track application performance over time, between different Ruby versions, different GitLab versions, etc. == Transaction Metrics Currently the following is tracked on a per transaction basis (a transaction is a Rails request or a single Sidekiq job): * Timings per query along with the raw (obfuscated) SQL and information about what file the query originated from. * Timings per view along with the path of the view and information about what file triggered the rendering process. * The duration of a request itself along with the controller/worker class and method name. * The duration of any instrumented method calls (more below). == Sampled Metrics Certain metrics can't be directly associated with a transaction. For example, a process' total memory usage is unrelated to any running transactions. While a transaction can result in the memory usage going up there's no accurate way to determine what transaction is to blame, this becomes especially problematic in multi-threaded environments. To solve this problem there's a separate thread that takes samples at a fixed interval. This thread (using the class Gitlab::Metrics::Sampler) currently tracks the following: * The process' total memory usage. * The number of file descriptors opened by the process. * The amount of Ruby objects (using ObjectSpace.count_objects). * GC statistics such as timings, heap slots, etc. The default/current interval is 15 seconds, any smaller interval might put too much pressure on InfluxDB (especially when running dozens of processes). == Method Instrumentation While currently not yet used methods can be instrumented to track how long they take to run. Unlike the likes of New Relic this doesn't require modifying the source code (e.g. including modules), it all happens from the outside. For example, to track `User.by_login` we'd add the following code somewhere in an initializer: Gitlab::Metrics::Instrumentation. instrument_method(User, :by_login) to instead instrument an instance method: Gitlab::Metrics::Instrumentation. instrument_instance_method(User, :save) Instrumentation for either all public model methods or a few crucial ones will be added in the near future, I simply haven't gotten to doing so just yet. == Configuration By default metrics are disabled. This means users don't have to bother setting anything up if they don't want to. Metrics can be enabled by editing one's gitlab.yml configuration file (see config/gitlab.yml.example for example settings). == Writing Data To InfluxDB Because InfluxDB is still a fairly young product I expect the worse. Data loss, unexpected reboots, the database not responding, you name it. Because of this data is _not_ written to InfluxDB directly, instead it's queued and processed by Sidekiq. This ensures that users won't notice anything when InfluxDB is giving trouble. The metrics worker can be started in a standalone manner as following: bundle exec sidekiq -q metrics The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
end
end
describe '.prometheus_metrics_enabled_unmemoized' do
subject { described_class.send(:prometheus_metrics_enabled_unmemoized) }
context 'prometheus metrics enabled in config' do
before do
allow(described_class).to receive(:current_application_settings).and_return(prometheus_metrics_enabled: true)
end
context 'when metrics folder is present' do
before do
allow(described_class).to receive(:metrics_folder_present?).and_return(true)
end
it 'metrics are enabled' do
expect(subject).to eq(true)
end
end
context 'when metrics folder is missing' do
before do
allow(described_class).to receive(:metrics_folder_present?).and_return(false)
end
it 'metrics are disabled' do
expect(subject).to eq(false)
end
end
end
end
2017-05-22 09:47:04 -04:00
describe '.prometheus_metrics_enabled?' do
it 'returns a boolean' do
expect(described_class.prometheus_metrics_enabled?).to be_in([true, false])
2017-05-22 09:47:04 -04:00
end
end
describe '.influx_metrics_enabled?' do
it 'returns a boolean' do
expect(described_class.influx_metrics_enabled?).to be_in([true, false])
2017-05-22 09:47:04 -04:00
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
2017-06-21 09:48:12 -04:00
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
context 'without a transaction' do
it 'returns the return value of the block' do
val = described_class.measure(:foo) { 10 }
expect(val).to eq(10)
end
end
context 'with a transaction' do
let(:transaction) { Gitlab::Metrics::Transaction.new }
before do
2017-06-21 09:48:12 -04:00
allow(described_class).to receive(:current_transaction)
.and_return(transaction)
end
it 'adds a metric to the current transaction' do
2017-06-21 09:48:12 -04:00
expect(transaction).to receive(:increment)
.with('foo_real_time', a_kind_of(Numeric))
2017-06-21 09:48:12 -04:00
expect(transaction).to receive(:increment)
.with('foo_cpu_time', a_kind_of(Numeric))
2017-06-21 09:48:12 -04:00
expect(transaction).to receive(:increment)
.with('foo_call_count', 1)
described_class.measure(:foo) { 10 }
end
it 'returns the return value of the block' do
val = described_class.measure(:foo) { 10 }
expect(val).to eq(10)
end
end
end
describe '.tag_transaction' do
context 'without a transaction' do
it 'does nothing' do
2017-06-21 09:48:12 -04:00
expect_any_instance_of(Gitlab::Metrics::Transaction)
.not_to receive(:add_tag)
described_class.tag_transaction(:foo, 'bar')
end
end
context 'with a transaction' do
let(:transaction) { Gitlab::Metrics::Transaction.new }
it 'adds the tag to the transaction' do
2017-06-21 09:48:12 -04:00
expect(described_class).to receive(:current_transaction)
.and_return(transaction)
2017-06-21 09:48:12 -04:00
expect(transaction).to receive(:add_tag)
.with(:foo, 'bar')
described_class.tag_transaction(:foo, 'bar')
end
end
end
describe '.action=' do
context 'without a transaction' do
it 'does nothing' do
2017-06-21 09:48:12 -04:00
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::Transaction.new
2017-06-21 09:48:12 -04:00
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
it 'returns a String' do
expect(described_class.series_prefix).to be_an_instance_of(String)
end
end
describe '.add_event' do
context 'without a transaction' do
it 'does nothing' do
2017-06-21 09:48:12 -04:00
expect_any_instance_of(Gitlab::Metrics::Transaction)
.not_to receive(:add_event)
described_class.add_event(:meow)
end
end
context 'with a transaction' do
it 'adds an event' do
transaction = Gitlab::Metrics::Transaction.new
expect(transaction).to receive(:add_event).with(:meow)
2017-06-21 09:48:12 -04:00
expect(described_class).to receive(:current_transaction)
.and_return(transaction)
described_class.add_event(:meow)
end
end
end
2017-05-22 09:47:04 -04:00
shared_examples 'prometheus metrics API' do
describe '#counter' do
subject { described_class.counter(:couter, 'doc') }
describe '#increment' do
it 'successfully calls #increment without arguments' do
expect { subject.increment }.not_to raise_exception
end
it 'successfully calls #increment with 1 argument' do
expect { subject.increment({}) }.not_to raise_exception
end
it 'successfully calls #increment with 2 arguments' do
expect { subject.increment({}, 1) }.not_to raise_exception
end
2017-05-22 09:47:04 -04:00
end
end
describe '#summary' do
subject { described_class.summary(:summary, 'doc') }
describe '#observe' do
it 'successfully calls #observe with 2 arguments' do
expect { subject.observe({}, 2) }.not_to raise_exception
end
2017-05-22 09:47:04 -04:00
end
end
describe '#gauge' do
subject { described_class.gauge(:gauge, 'doc') }
describe '#set' do
it 'successfully calls #set with 2 arguments' do
expect { subject.set({}, 1) }.not_to raise_exception
end
2017-05-22 09:47:04 -04:00
end
end
describe '#histogram' do
subject { described_class.histogram(:histogram, 'doc') }
describe '#observe' do
it 'successfully calls #observe with 2 arguments' do
expect { subject.observe({}, 2) }.not_to raise_exception
end
2017-05-22 09:47:04 -04:00
end
end
end
context 'prometheus metrics disabled' do
before do
allow(described_class).to receive(:prometheus_metrics_enabled?).and_return(false)
end
it_behaves_like 'prometheus metrics API'
describe '#null_metric' do
2017-05-22 09:47:04 -04:00
subject { described_class.provide_metric(:test) }
it { is_expected.to be_a(Gitlab::Metrics::NullMetric) }
2017-05-22 09:47:04 -04:00
end
describe '#counter' do
subject { described_class.counter(:counter, 'doc') }
it { is_expected.to be_a(Gitlab::Metrics::NullMetric) }
2017-05-22 09:47:04 -04:00
end
describe '#summary' do
subject { described_class.summary(:summary, 'doc') }
it { is_expected.to be_a(Gitlab::Metrics::NullMetric) }
2017-05-22 09:47:04 -04:00
end
describe '#gauge' do
subject { described_class.gauge(:gauge, 'doc') }
it { is_expected.to be_a(Gitlab::Metrics::NullMetric) }
2017-05-22 09:47:04 -04:00
end
describe '#histogram' do
subject { described_class.histogram(:histogram, 'doc') }
it { is_expected.to be_a(Gitlab::Metrics::NullMetric) }
2017-05-22 09:47:04 -04:00
end
end
context 'prometheus metrics enabled' do
let(:metrics_multiproc_dir) { Dir.mktmpdir }
2017-05-22 09:47:04 -04:00
before do
stub_const('Prometheus::Client::Multiprocdir', metrics_multiproc_dir)
2017-05-22 09:47:04 -04:00
allow(described_class).to receive(:prometheus_metrics_enabled?).and_return(true)
end
it_behaves_like 'prometheus metrics API'
describe '#null_metric' do
2017-05-22 09:47:04 -04:00
subject { described_class.provide_metric(:test) }
it { is_expected.to be_nil }
end
describe '#counter' do
subject { described_class.counter(:name, 'doc') }
it { is_expected.not_to be_a(Gitlab::Metrics::NullMetric) }
2017-05-22 09:47:04 -04:00
end
describe '#summary' do
subject { described_class.summary(:name, 'doc') }
it { is_expected.not_to be_a(Gitlab::Metrics::NullMetric) }
2017-05-22 09:47:04 -04:00
end
describe '#gauge' do
subject { described_class.gauge(:name, 'doc') }
it { is_expected.not_to be_a(Gitlab::Metrics::NullMetric) }
2017-05-22 09:47:04 -04:00
end
describe '#histogram' do
subject { described_class.histogram(:name, 'doc') }
it { is_expected.not_to be_a(Gitlab::Metrics::NullMetric) }
2017-05-22 09:47:04 -04:00
end
end
Storing of application metrics in InfluxDB This adds the ability to write application metrics (e.g. SQL timings) to InfluxDB. These metrics can in turn be visualized using Grafana, or really anything else that can read from InfluxDB. These metrics can be used to track application performance over time, between different Ruby versions, different GitLab versions, etc. == Transaction Metrics Currently the following is tracked on a per transaction basis (a transaction is a Rails request or a single Sidekiq job): * Timings per query along with the raw (obfuscated) SQL and information about what file the query originated from. * Timings per view along with the path of the view and information about what file triggered the rendering process. * The duration of a request itself along with the controller/worker class and method name. * The duration of any instrumented method calls (more below). == Sampled Metrics Certain metrics can't be directly associated with a transaction. For example, a process' total memory usage is unrelated to any running transactions. While a transaction can result in the memory usage going up there's no accurate way to determine what transaction is to blame, this becomes especially problematic in multi-threaded environments. To solve this problem there's a separate thread that takes samples at a fixed interval. This thread (using the class Gitlab::Metrics::Sampler) currently tracks the following: * The process' total memory usage. * The number of file descriptors opened by the process. * The amount of Ruby objects (using ObjectSpace.count_objects). * GC statistics such as timings, heap slots, etc. The default/current interval is 15 seconds, any smaller interval might put too much pressure on InfluxDB (especially when running dozens of processes). == Method Instrumentation While currently not yet used methods can be instrumented to track how long they take to run. Unlike the likes of New Relic this doesn't require modifying the source code (e.g. including modules), it all happens from the outside. For example, to track `User.by_login` we'd add the following code somewhere in an initializer: Gitlab::Metrics::Instrumentation. instrument_method(User, :by_login) to instead instrument an instance method: Gitlab::Metrics::Instrumentation. instrument_instance_method(User, :save) Instrumentation for either all public model methods or a few crucial ones will be added in the near future, I simply haven't gotten to doing so just yet. == Configuration By default metrics are disabled. This means users don't have to bother setting anything up if they don't want to. Metrics can be enabled by editing one's gitlab.yml configuration file (see config/gitlab.yml.example for example settings). == Writing Data To InfluxDB Because InfluxDB is still a fairly young product I expect the worse. Data loss, unexpected reboots, the database not responding, you name it. Because of this data is _not_ written to InfluxDB directly, instead it's queued and processed by Sidekiq. This ensures that users won't notice anything when InfluxDB is giving trouble. The metrics worker can be started in a standalone manner as following: bundle exec sidekiq -q metrics The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
end