Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
gitlab-org
gitlab-exporter
Commits
3ed764b7
Commit
3ed764b7
authored
Aug 06, 2016
by
Pablo Carranza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract PrometheusMetrics to its own file
parent
dd845115
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
22 deletions
+42
-22
bin/gitlab-mon-ssh
bin/gitlab-mon-ssh
+0
-22
lib/gitlab_monitor.rb
lib/gitlab_monitor.rb
+1
-0
lib/gitlab_monitor/prometheus.rb
lib/gitlab_monitor/prometheus.rb
+26
-0
spec/util_spec.rb
spec/util_spec.rb
+15
-0
No files found.
bin/gitlab-mon-ssh
View file @
3ed764b7
...
...
@@ -24,28 +24,6 @@ class GitRunner
end
end
class
PrometheusMetrics
def
initialize
@metrics
=
{}
end
def
add
(
name
,
value
,
**
labels
)
@metrics
[
name
]
=
{
value:
value
,
labels:
labels
,
timestamp:
(
Time
.
now
.
to_f
*
1000
).
to_i
}
end
def
to_s
buffer
=
StringIO
.
new
@metrics
.
each
{
|
name
,
metrics
|
buffer
.
write
(
"
#{
name
}
"
)
labels
=
(
metrics
[
:labels
]
||
{}).
map
{
|
label
,
value
|
"
#{
label
}
=
\"
#{
value
}
\"
"
}.
join
(
","
)
buffer
.
write
(
"{
#{
labels
}
}"
)
unless
labels
.
empty?
buffer
.
write
(
"
#{
metrics
[
:value
]
}
#{
metrics
[
:timestamp
]
}
\n
"
)
}
buffer
.
rewind
buffer
.
read
end
end
def
main
runner
=
GitRunner
.
new
(
File
.
expand_path
(
"~/dev/emptyness"
))
metrics
=
PrometheusMetrics
.
new
...
...
lib/gitlab_monitor.rb
View file @
3ed764b7
module
GitLab
module
Monitor
autoload
:TimeTracker
,
"gitlab_monitor/util"
autoload
:PrometheusMetrics
,
"gitlab_monitor/prometheus"
end
end
lib/gitlab_monitor/prometheus.rb
0 → 100644
View file @
3ed764b7
module
GitLab
module
Monitor
class
PrometheusMetrics
def
initialize
@metrics
=
{}
end
def
add
(
name
,
value
,
**
labels
)
@metrics
[
name
]
=
{
value:
value
,
labels:
labels
,
timestamp:
(
Time
.
now
.
to_f
*
1000
).
to_i
}
self
end
def
to_s
buffer
=
StringIO
.
new
@metrics
.
each
{
|
name
,
metrics
|
buffer
.
write
(
"
#{
name
}
"
)
labels
=
(
metrics
[
:labels
]
||
{}).
map
{
|
label
,
value
|
"
#{
label
}
=
\"
#{
value
}
\"
"
}.
join
(
","
)
buffer
.
write
(
"{
#{
labels
}
}"
)
unless
labels
.
empty?
buffer
.
write
(
"
#{
metrics
[
:value
]
}
#{
metrics
[
:timestamp
]
}
\n
"
)
}
buffer
.
rewind
buffer
.
read
end
end
end
end
spec/util_spec.rb
View file @
3ed764b7
...
...
@@ -5,3 +5,18 @@ describe GitLab::Monitor::TimeTracker do
expect
(
subject
.
track
{
sleep
0.1
}).
to
satisfy
{
|
v
|
v
>=
0.1
}
end
end
describe
GitLab
::
Monitor
::
PrometheusMetrics
do
it
"supports simple metrics"
do
expect
(
subject
.
add
(
"mymetric"
,
1.1
).
to_s
).
to
match
(
/mymetric 1.1 \d*$/
)
end
it
"supports metrics with one label"
do
expect
(
subject
.
add
(
"mymetric"
,
1.2
,
mylabel:
"x"
).
to_s
).
to
match
(
/mymetric{mylabel="x"} 1.2 \d*$/
)
end
it
"supports metrics with many labels"
do
expect
(
subject
.
add
(
"mymetric"
,
1.3
,
mylabel:
"x"
,
myotherlabel:
"y"
).
to_s
).
to
match
(
/mymetric{mylabel="x",myotherlabel="y"} 1.3 \d*$/
)
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