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'
|
|
|
|
|
2017-10-17 10:06:52 -04:00
|
|
|
describe Gitlab::Metrics::WebTransaction do
|
2017-09-06 12:43:06 -04:00
|
|
|
let(:env) { {} }
|
|
|
|
let(:transaction) { described_class.new(env) }
|
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
|
|
|
|
|
|
|
describe '#duration' do
|
|
|
|
it 'returns the duration of a transaction in seconds' do
|
|
|
|
transaction.run { sleep(0.5) }
|
|
|
|
|
|
|
|
expect(transaction.duration).to be >= 0.5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-12 08:59:30 -05:00
|
|
|
describe '#allocated_memory' do
|
|
|
|
it 'returns the allocated memory in bytes' do
|
|
|
|
transaction.run { 'a' * 32 }
|
|
|
|
|
|
|
|
expect(transaction.allocated_memory).to be_a_kind_of(Numeric)
|
|
|
|
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
|
|
|
describe '#run' do
|
|
|
|
it 'yields the supplied block' do
|
|
|
|
expect { |b| transaction.run(&b) }.to yield_control
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores the transaction in the current thread' do
|
|
|
|
transaction.run do
|
|
|
|
expect(Thread.current[described_class::THREAD_KEY]).to eq(transaction)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the transaction from the current thread upon completion' do
|
|
|
|
transaction.run { }
|
|
|
|
|
|
|
|
expect(Thread.current[described_class::THREAD_KEY]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#add_metric' do
|
2016-01-07 06:44:15 -05:00
|
|
|
it 'adds a metric to the transaction' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(Gitlab::Metrics::Metric).to receive(:new)
|
|
|
|
.with('rails_foo', { number: 10 }, {})
|
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
|
|
|
|
|
|
|
transaction.add_metric('foo', number: 10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-28 09:08:57 -04:00
|
|
|
describe '#method_call_for' do
|
|
|
|
it 'returns a MethodCall' do
|
2017-09-06 12:43:06 -04:00
|
|
|
method = transaction.method_call_for('Foo#bar', :Foo, '#bar')
|
2016-06-17 11:45:37 -04:00
|
|
|
|
2016-07-28 09:08:57 -04:00
|
|
|
expect(method).to be_an_instance_of(Gitlab::Metrics::MethodCall)
|
2016-06-17 11:45:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-04 05:37:46 -05:00
|
|
|
describe '#increment' do
|
|
|
|
it 'increments a counter' do
|
|
|
|
transaction.increment(:time, 1)
|
|
|
|
transaction.increment(:time, 2)
|
|
|
|
|
2016-01-12 08:59:30 -05:00
|
|
|
values = { duration: 0.0, time: 3, allocated_memory: a_kind_of(Numeric) }
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(transaction).to receive(:add_metric)
|
|
|
|
.with('transactions', values, {})
|
2016-01-04 05:37:46 -05:00
|
|
|
|
|
|
|
transaction.track_self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-07 07:05:00 -05:00
|
|
|
describe '#set' do
|
|
|
|
it 'sets a value' do
|
|
|
|
transaction.set(:number, 10)
|
|
|
|
|
2016-01-12 08:59:30 -05:00
|
|
|
values = {
|
|
|
|
duration: 0.0,
|
|
|
|
number: 10,
|
|
|
|
allocated_memory: a_kind_of(Numeric)
|
|
|
|
}
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(transaction).to receive(:add_metric)
|
|
|
|
.with('transactions', values, {})
|
2016-01-07 07:05:00 -05:00
|
|
|
|
|
|
|
transaction.track_self
|
|
|
|
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
|
|
|
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
|
2016-01-12 08:59:30 -05:00
|
|
|
values = {
|
|
|
|
duration: transaction.duration,
|
|
|
|
allocated_memory: a_kind_of(Numeric)
|
|
|
|
}
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(transaction).to receive(:add_metric)
|
|
|
|
.with('transactions', values, {})
|
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
|
|
|
|
|
|
|
transaction.track_self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#submit' do
|
|
|
|
it 'submits the metrics to Sidekiq' do
|
|
|
|
transaction.track_self
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(Gitlab::Metrics).to receive(:submit_metrics)
|
|
|
|
.with([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
|
|
|
|
|
|
|
transaction.submit
|
|
|
|
end
|
2016-01-11 10:51:01 -05:00
|
|
|
|
|
|
|
it 'adds the action as a tag for every metric' do
|
2017-09-06 12:43:06 -04:00
|
|
|
allow(transaction).to receive(:labels).and_return(controller: 'Foo', action: 'bar')
|
2016-01-11 10:51:01 -05:00
|
|
|
transaction.track_self
|
|
|
|
|
|
|
|
hash = {
|
|
|
|
series: 'rails_transactions',
|
|
|
|
tags: { action: 'Foo#bar' },
|
2016-01-12 08:59:30 -05:00
|
|
|
values: { duration: 0.0, allocated_memory: a_kind_of(Numeric) },
|
2017-02-22 09:10:38 -05:00
|
|
|
timestamp: a_kind_of(Integer)
|
2016-01-11 10:51:01 -05:00
|
|
|
}
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(Gitlab::Metrics).to receive(:submit_metrics)
|
|
|
|
.with([hash])
|
2016-01-11 10:51:01 -05:00
|
|
|
|
|
|
|
transaction.submit
|
|
|
|
end
|
2016-08-16 10:18:48 -04:00
|
|
|
|
|
|
|
it 'does not add an action tag for events' do
|
2017-09-06 12:43:06 -04:00
|
|
|
allow(transaction).to receive(:labels).and_return(controller: 'Foo', action: 'bar')
|
|
|
|
|
2016-08-16 10:18:48 -04:00
|
|
|
transaction.add_event(:meow)
|
|
|
|
|
|
|
|
hash = {
|
|
|
|
series: 'events',
|
|
|
|
tags: { event: :meow },
|
|
|
|
values: { count: 1 },
|
2017-02-22 09:10:38 -05:00
|
|
|
timestamp: a_kind_of(Integer)
|
2016-08-16 10:18:48 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(Gitlab::Metrics).to receive(:submit_metrics)
|
|
|
|
.with([hash])
|
2016-08-16 10:18:48 -04:00
|
|
|
|
|
|
|
transaction.submit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-07 05:15:27 -04:00
|
|
|
describe '#labels' do
|
|
|
|
context 'when request goes to Grape endpoint' do
|
|
|
|
before do
|
|
|
|
route = double(:route, request_method: 'GET', path: '/:version/projects/:id/archive(.:format)')
|
|
|
|
endpoint = double(:endpoint, route: route)
|
|
|
|
|
|
|
|
env['api.endpoint'] = endpoint
|
|
|
|
end
|
|
|
|
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.action).to eq('Grape#GET /projects/:id/archive')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not provide labels if route infos are missing' do
|
|
|
|
endpoint = double(:endpoint)
|
|
|
|
allow(endpoint).to receive(:route).and_raise
|
|
|
|
|
|
|
|
env['api.endpoint'] = endpoint
|
|
|
|
|
|
|
|
expect(transaction.labels).to eq({})
|
|
|
|
expect(transaction.action).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request goes to ActionController' do
|
2018-05-16 06:53:54 -04:00
|
|
|
let(:request) { double(:request, format: double(:format, ref: :html)) }
|
2017-09-07 05:15:27 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
klass = double(:klass, name: 'TestController')
|
2018-05-16 06:53:54 -04:00
|
|
|
controller = double(:controller, class: klass, action_name: 'show', request: request)
|
2017-09-07 05:15:27 -04:00
|
|
|
|
|
|
|
env['action_controller.instance'] = controller
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tags a transaction with the name and action of a controller' do
|
|
|
|
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show' })
|
|
|
|
expect(transaction.action).to eq('TestController#show')
|
|
|
|
end
|
|
|
|
|
2018-06-20 08:33:14 -04:00
|
|
|
context 'when the request content type is not :html' do
|
2018-05-16 06:53:54 -04:00
|
|
|
let(:request) { double(:request, format: double(:format, ref: :json)) }
|
2017-09-07 05:15:27 -04:00
|
|
|
|
|
|
|
it 'appends the mime type to the transaction action' do
|
|
|
|
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show.json' })
|
|
|
|
expect(transaction.action).to eq('TestController#show.json')
|
|
|
|
end
|
|
|
|
end
|
2018-06-20 08:33:14 -04:00
|
|
|
|
|
|
|
context 'when the request content type is not' do
|
|
|
|
let(:request) { double(:request, format: double(:format, ref: 'http://example.com')) }
|
|
|
|
|
|
|
|
it 'does not append the MIME type to the transaction action' do
|
|
|
|
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show' })
|
|
|
|
expect(transaction.action).to eq('TestController#show')
|
|
|
|
end
|
|
|
|
end
|
2017-09-07 05:15:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no labels when no route information is present in env' do
|
|
|
|
expect(transaction.labels).to eq({})
|
|
|
|
expect(transaction.action).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-16 10:18:48 -04:00
|
|
|
describe '#add_event' do
|
|
|
|
it 'adds a metric' do
|
|
|
|
transaction.add_event(:meow)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'allows tracking of custom tags' do
|
|
|
|
transaction.add_event(:meow, animal: 'cat')
|
|
|
|
|
|
|
|
metric = transaction.metrics[0]
|
|
|
|
|
|
|
|
expect(metric.tags).to eq(event: :meow, animal: 'cat')
|
|
|
|
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
|
|
|
|
end
|