Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
ef9d9dde
Commit
ef9d9dde
authored
May 22, 2017
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for metrics behavior
parent
57902dbe
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
134 additions
and
7 deletions
+134
-7
lib/gitlab/metrics.rb
lib/gitlab/metrics.rb
+9
-7
spec/lib/gitlab/metrics_spec.rb
spec/lib/gitlab/metrics_spec.rb
+125
-0
No files found.
lib/gitlab/metrics.rb
View file @
ef9d9dde
...
...
@@ -54,23 +54,25 @@ module Gitlab
end
def
self
.
counter
(
name
,
docstring
,
base_labels
=
{})
dummy_metric
||
registry
.
get
(
name
)
||
registry
.
counter
(
name
,
docstring
,
base_labels
)
provide_metric
(
name
)
||
registry
.
counter
(
name
,
docstring
,
base_labels
)
end
def
self
.
summary
(
name
,
docstring
,
base_labels
=
{})
dummy_metric
||
registry
.
get
(
name
)
||
registry
.
summary
(
name
,
docstring
,
base_labels
)
provide_metric
(
name
)
||
registry
.
summary
(
name
,
docstring
,
base_labels
)
end
def
self
.
gauge
(
name
,
docstring
,
base_labels
=
{})
dummy_metric
||
registry
.
get
(
name
)
||
registry
.
gauge
(
name
,
docstring
,
base_labels
)
provide_metric
(
name
)
||
registry
.
gauge
(
name
,
docstring
,
base_labels
)
end
def
self
.
histogram
(
name
,
docstring
,
base_labels
=
{},
buckets
=
Histogram
::
DEFAULT_BUCKETS
)
dummy_metric
||
registry
.
get
(
name
)
||
registry
.
histogram
(
name
,
docstring
,
base_labels
,
buckets
)
def
self
.
histogram
(
name
,
docstring
,
base_labels
=
{},
buckets
=
::
Prometheus
::
Client
::
Histogram
::
DEFAULT_BUCKETS
)
provide_metric
(
name
)
||
registry
.
histogram
(
name
,
docstring
,
base_labels
,
buckets
)
end
def
self
.
dummy_metric
unless
prometheus_metrics_enabled?
def
self
.
provide_metric
(
name
)
if
prometheus_metrics_enabled?
registry
.
get
(
name
)
else
DummyMetric
.
new
end
end
...
...
spec/lib/gitlab/metrics_spec.rb
View file @
ef9d9dde
...
...
@@ -13,6 +13,18 @@ describe Gitlab::Metrics do
end
end
describe
'.prometheus_metrics_enabled?'
do
it
'returns a boolean'
do
expect
([
true
,
false
].
include?
(
described_class
.
prometheus_metrics_enabled?
)).
to
eq
(
true
)
end
end
describe
'.influx_metrics_enabled?'
do
it
'returns a boolean'
do
expect
([
true
,
false
].
include?
(
described_class
.
influx_metrics_enabled?
)).
to
eq
(
true
)
end
end
describe
'.submit_metrics'
do
it
'prepares and writes the metrics to InfluxDB'
do
connection
=
double
(
:connection
)
...
...
@@ -177,4 +189,117 @@ describe Gitlab::Metrics do
end
end
end
shared_examples
'prometheus metrics API'
do
describe
'#counter'
do
subject
{
described_class
.
counter
(
:couter
,
'doc'
)
}
describe
'#increment'
do
it
{
expect
{
subject
.
increment
}.
not_to
raise_exception
}
it
{
expect
{
subject
.
increment
({})
}.
not_to
raise_exception
}
it
{
expect
{
subject
.
increment
({},
1
)
}.
not_to
raise_exception
}
end
end
describe
'#summary'
do
subject
{
described_class
.
summary
(
:summary
,
'doc'
)
}
describe
'#observe'
do
it
{
expect
{
subject
.
observe
({},
2
)
}.
not_to
raise_exception
}
end
end
describe
'#gauge'
do
subject
{
described_class
.
gauge
(
:gauge
,
'doc'
)
}
describe
'#observe'
do
it
{
expect
{
subject
.
set
({},
1
)
}.
not_to
raise_exception
}
end
end
describe
'#histogram'
do
subject
{
described_class
.
histogram
(
:histogram
,
'doc'
)
}
describe
'#observe'
do
it
{
expect
{
subject
.
observe
({},
2
)
}.
not_to
raise_exception
}
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
'#dummy_metric'
do
subject
{
described_class
.
provide_metric
(
:test
)
}
it
{
is_expected
.
to
be_a
(
Gitlab
::
Metrics
::
DummyMetric
)
}
end
describe
'#counter'
do
subject
{
described_class
.
counter
(
:counter
,
'doc'
)
}
it
{
is_expected
.
to
be_a
(
Gitlab
::
Metrics
::
DummyMetric
)
}
end
describe
'#summary'
do
subject
{
described_class
.
summary
(
:summary
,
'doc'
)
}
it
{
is_expected
.
to
be_a
(
Gitlab
::
Metrics
::
DummyMetric
)
}
end
describe
'#gauge'
do
subject
{
described_class
.
gauge
(
:gauge
,
'doc'
)
}
it
{
is_expected
.
to
be_a
(
Gitlab
::
Metrics
::
DummyMetric
)
}
end
describe
'#histogram'
do
subject
{
described_class
.
histogram
(
:histogram
,
'doc'
)
}
it
{
is_expected
.
to
be_a
(
Gitlab
::
Metrics
::
DummyMetric
)
}
end
end
context
'prometheus metrics enabled'
do
before
do
allow
(
described_class
).
to
receive
(
:prometheus_metrics_enabled?
).
and_return
(
true
)
end
it_behaves_like
'prometheus metrics API'
describe
'#dummy_metric'
do
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
::
DummyMetric
)
}
end
describe
'#summary'
do
subject
{
described_class
.
summary
(
:name
,
'doc'
)
}
it
{
is_expected
.
not_to
be_a
(
Gitlab
::
Metrics
::
DummyMetric
)
}
end
describe
'#gauge'
do
subject
{
described_class
.
gauge
(
:name
,
'doc'
)
}
it
{
is_expected
.
not_to
be_a
(
Gitlab
::
Metrics
::
DummyMetric
)
}
end
describe
'#histogram'
do
subject
{
described_class
.
histogram
(
:name
,
'doc'
)
}
it
{
is_expected
.
not_to
be_a
(
Gitlab
::
Metrics
::
DummyMetric
)
}
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment