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 15:45:51 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Metrics do
|
2015-12-31 16:47:07 +00:00
|
|
|
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 15:45:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.enabled?' do
|
|
|
|
it 'returns a boolean' do
|
|
|
|
expect([true, false].include?(described_class.enabled?)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-04 10:47:10 +00:00
|
|
|
describe '.submit_metrics' do
|
2015-12-29 12:40:42 +00:00
|
|
|
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(Gitlab::Metrics).to receive(:pool).and_return(pool)
|
|
|
|
|
|
|
|
described_class.submit_metrics([{ 'series' => 'kittens', 'tags' => {} }])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-04 10:47:10 +00:00
|
|
|
describe '.prepare_metrics' do
|
2015-12-29 12:40:42 +00:00
|
|
|
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
|
|
|
|
|
2016-04-04 10:47:10 +00:00
|
|
|
describe '.escape_value' do
|
2015-12-29 12:40:42 +00:00
|
|
|
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
|
2016-04-04 12:00:35 +00:00
|
|
|
|
|
|
|
describe '.measure' do
|
|
|
|
context 'without a transaction' do
|
|
|
|
it 'returns the return value of the block' do
|
|
|
|
val = Gitlab::Metrics.measure(:foo) { 10 }
|
|
|
|
|
|
|
|
expect(val).to eq(10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a transaction' do
|
|
|
|
let(:transaction) { Gitlab::Metrics::Transaction.new }
|
|
|
|
|
|
|
|
before do
|
2016-04-11 12:29:38 +00:00
|
|
|
allow(Gitlab::Metrics).to receive(:current_transaction).
|
2016-04-04 12:00:35 +00:00
|
|
|
and_return(transaction)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a metric to the current transaction' do
|
2016-04-11 10:23:37 +00:00
|
|
|
expect(transaction).to receive(:increment).
|
|
|
|
with('foo_real_time', a_kind_of(Numeric))
|
2016-04-04 12:00:35 +00:00
|
|
|
|
2016-04-11 10:23:37 +00:00
|
|
|
expect(transaction).to receive(:increment).
|
|
|
|
with('foo_cpu_time', a_kind_of(Numeric))
|
2016-04-04 12:00:35 +00:00
|
|
|
|
2016-04-11 11:27:24 +00:00
|
|
|
expect(transaction).to receive(:increment).
|
|
|
|
with('foo_call_count', 1)
|
|
|
|
|
2016-04-11 10:23:37 +00:00
|
|
|
Gitlab::Metrics.measure(:foo) { 10 }
|
2016-04-04 12:00:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the return value of the block' do
|
|
|
|
val = Gitlab::Metrics.measure(:foo) { 10 }
|
|
|
|
|
|
|
|
expect(val).to eq(10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-12 10:00:21 +00:00
|
|
|
|
|
|
|
describe '.tag_transaction' do
|
|
|
|
context 'without a transaction' do
|
|
|
|
it 'does nothing' do
|
|
|
|
expect_any_instance_of(Gitlab::Metrics::Transaction).
|
|
|
|
not_to receive(:add_tag)
|
|
|
|
|
|
|
|
Gitlab::Metrics.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
|
|
|
|
expect(Gitlab::Metrics).to receive(:current_transaction).
|
|
|
|
and_return(transaction)
|
|
|
|
|
|
|
|
expect(transaction).to receive(:add_tag).
|
|
|
|
with(:foo, 'bar')
|
|
|
|
|
|
|
|
Gitlab::Metrics.tag_transaction(:foo, 'bar')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-20 20:42:52 +00:00
|
|
|
|
|
|
|
describe '.action=' do
|
|
|
|
context 'without a transaction' do
|
|
|
|
it 'does nothing' do
|
|
|
|
expect_any_instance_of(Gitlab::Metrics::Transaction).
|
|
|
|
not_to receive(:action=)
|
|
|
|
|
|
|
|
Gitlab::Metrics.action = 'foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a transaction' do
|
|
|
|
it 'sets the action of a transaction' do
|
|
|
|
trans = Gitlab::Metrics::Transaction.new
|
|
|
|
|
|
|
|
expect(Gitlab::Metrics).to receive(:current_transaction).
|
|
|
|
and_return(trans)
|
|
|
|
|
|
|
|
expect(trans).to receive(:action=).with('foo')
|
|
|
|
|
|
|
|
Gitlab::Metrics.action = 'foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-28 13:08:57 +00:00
|
|
|
|
|
|
|
describe '#series_prefix' do
|
|
|
|
it 'returns a String' do
|
|
|
|
expect(described_class.series_prefix).to be_an_instance_of(String)
|
|
|
|
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 15:45:51 +00:00
|
|
|
end
|