Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
What's new
6
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gitlab-org
gitlab-exporter
Commits
6724873b
Unverified
Commit
6724873b
authored
Jun 30, 2021
by
Sean McGivern
Browse files
Allow describing metrics
parent
0b9c0dce
Changes
2
Hide whitespace changes
Inline
Side-by-side
lib/gitlab_exporter/prometheus.rb
View file @
6724873b
...
...
@@ -12,9 +12,14 @@ module GitLab
def
initialize
(
include_timestamp:
true
)
@metrics
=
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
[]
}
@quantiles
=
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
[]
}
@metric_descriptions
=
{}
@include_timestamp
=
include_timestamp
end
def
describe
(
name
,
description
)
@metric_descriptions
[
name
]
=
description
end
def
add
(
name
,
value
,
quantile
=
false
,
**
labels
)
fail
"value '
#{
value
}
' must be a number"
unless
value
.
is_a?
(
Numeric
)
...
...
@@ -32,6 +37,8 @@ module GitLab
buffer
=
""
@metrics
.
each
do
|
name
,
measurements
|
buffer
<<
"# HELP
#{
name
}
#{
@metric_descriptions
[
name
]
}
\n
"
if
@metric_descriptions
[
name
]
measurements
.
each
do
|
measurement
|
buffer
<<
name
.
to_s
labels
=
(
measurement
[
:labels
]
||
{}).
map
{
|
label
,
value
|
"
#{
label
}
=
\"
#{
value
}
\"
"
}.
join
(
","
)
...
...
spec/prometheus_metrics_spec.rb
View file @
6724873b
...
...
@@ -23,4 +23,19 @@ describe GitLab::Exporter::PrometheusMetrics do
subject
.
add
(
"mymetric"
,
"invalid"
,
mylabel:
"x"
,
myotherlabel:
"y"
).
to_s
}.
to
raise_error
(
RuntimeError
)
end
it
"supports described metrics"
do
time
=
Time
.
now
allow
(
Time
).
to
receive
(
:now
).
and_return
(
time
)
subject
.
describe
(
"mymetric"
,
"description"
)
subject
.
describe
(
"missingmetric"
,
"otherdescription"
)
subject
.
add
(
"mymetric"
,
1.3
,
mylabel:
"x"
,
myotherlabel:
"y"
)
expect
(
subject
.
to_s
).
to
eq
(
<<~
METRICS
)
# HELP mymetric description
mymetric{mylabel="x",myotherlabel="y"} 1.3
#{
(
time
.
to_f
*
1000
).
to_i
}
METRICS
end
end
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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