Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Kevin Lyda
gqgmc
Commits
4681baba
Commit
4681baba
authored
Feb 01, 2017
by
Kevin Lyda
💬
Browse files
Add metrics collection.
parent
b04758da
Pipeline
#1227
passed with stage
in 1 minute and 4 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
cmd/gqgmcd/main.go
View file @
4681baba
...
@@ -13,7 +13,9 @@ import (
...
@@ -13,7 +13,9 @@ import (
"log"
"log"
"net/http"
"net/http"
"path"
"path"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gitlab.com/lyda/gqgmc/devices/geiger"
"gitlab.com/lyda/gqgmc/devices/geiger"
)
)
...
@@ -30,6 +32,7 @@ type indexPage struct {
...
@@ -30,6 +32,7 @@ type indexPage struct {
Serial
string
Serial
string
Volts
int16
Volts
int16
CPM
uint16
CPM
uint16
CPS
uint16
}
}
var
gc
geiger
.
Counter
var
gc
geiger
.
Counter
...
@@ -50,18 +53,98 @@ func staticHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -50,18 +53,98 @@ func staticHandler(w http.ResponseWriter, r *http.Request) {
http
.
ServeFile
(
w
,
r
,
staticFile
)
http
.
ServeFile
(
w
,
r
,
staticFile
)
}
}
// Metrics collects the metrics.
type
Metrics
struct
{
CPM
,
CPS
,
Volts
,
Errors
*
prometheus
.
HistogramVec
}
var
metrics
=
&
Metrics
{
CPM
:
prometheus
.
NewHistogramVec
(
prometheus
.
HistogramOpts
{
Namespace
:
"gqgmc"
,
Subsystem
:
"geiger"
,
Name
:
"cpm"
,
Help
:
"CPM readings"
,
Buckets
:
[]
float64
{
50
,
99
,
999
,
1999
},
},
[]
string
{
"serial"
}),
CPS
:
prometheus
.
NewHistogramVec
(
prometheus
.
HistogramOpts
{
Namespace
:
"gqgmc"
,
Subsystem
:
"geiger"
,
Name
:
"cps"
,
Help
:
"CPS readings"
,
Buckets
:
[]
float64
{
1
,
2
,
16
,
33
},
},
[]
string
{
"serial"
}),
Volts
:
prometheus
.
NewHistogramVec
(
prometheus
.
HistogramOpts
{
Namespace
:
"gqgmc"
,
Subsystem
:
"power"
,
Name
:
"volts"
,
Help
:
"Voltage readings"
,
Buckets
:
[]
float64
{
22
,
27
,
42
,
50
},
},
[]
string
{
"serial"
}),
Errors
:
prometheus
.
NewHistogramVec
(
prometheus
.
HistogramOpts
{
Namespace
:
"gqgmc"
,
Subsystem
:
"sys"
,
Name
:
"errors"
,
Help
:
"Error counts"
,
Buckets
:
[]
float64
{
1
,
10
,
100
},
},
[]
string
{
"serial"
}),
}
func
gatherMetrics
()
{
var
(
cpm
,
cps
uint16
volts
int16
errCt
float64
err
error
)
for
{
if
cpm
,
err
=
gc
.
GetCPM
();
err
!=
nil
{
log
.
Printf
(
"gc.GetCPM error: %s
\n
"
,
err
)
errCt
++
}
else
{
metrics
.
CPM
.
WithLabelValues
(
gc
.
Version
())
.
Observe
(
float64
(
cpm
))
}
if
cps
,
err
=
gc
.
GetCPS
();
err
!=
nil
{
log
.
Printf
(
"gc.GetCPS error: %s
\n
"
,
err
)
errCt
++
}
else
{
metrics
.
CPS
.
WithLabelValues
(
gc
.
Version
())
.
Observe
(
float64
(
cps
))
}
if
volts
,
err
=
gc
.
Volts
();
err
!=
nil
{
log
.
Printf
(
"gc.Volts error: %s
\n
"
,
err
)
errCt
++
}
else
{
metrics
.
Volts
.
WithLabelValues
(
gc
.
Version
())
.
Observe
(
float64
(
volts
))
}
metrics
.
Errors
.
WithLabelValues
(
gc
.
Version
())
.
Observe
(
errCt
)
time
.
Sleep
(
5
*
time
.
Second
)
}
}
func
main
()
{
func
main
()
{
flag
.
Parse
()
flag
.
Parse
()
gc
,
_
=
geiger
.
New
(
geiger
.
Config
{
Model
:
*
model
,
Device
:
*
device
})
gc
,
_
=
geiger
.
New
(
geiger
.
Config
{
Model
:
*
model
,
Device
:
*
device
})
indexPg
.
Model
=
gc
.
Model
()
indexPg
.
Model
=
gc
.
Model
()
indexPg
.
Version
=
gc
.
Version
()
indexPg
.
Version
=
gc
.
Version
()
indexPg
.
Serial
=
gc
.
Serial
()
indexPg
.
Serial
=
gc
.
Serial
()
prometheus
.
MustRegister
(
metrics
.
CPM
)
prometheus
.
MustRegister
(
metrics
.
CPS
)
prometheus
.
MustRegister
(
metrics
.
Volts
)
prometheus
.
MustRegister
(
metrics
.
Errors
)
http
.
HandleFunc
(
"/"
,
indexHandler
)
http
.
HandleFunc
(
"/"
,
indexHandler
)
http
.
HandleFunc
(
"/favicon.ico"
,
staticHandler
)
http
.
HandleFunc
(
"/favicon.ico"
,
staticHandler
)
http
.
HandleFunc
(
"/robots.txt"
,
staticHandler
)
http
.
HandleFunc
(
"/robots.txt"
,
staticHandler
)
http
.
HandleFunc
(
"/humans.txt"
,
staticHandler
)
http
.
HandleFunc
(
"/humans.txt"
,
staticHandler
)
http
.
Handle
(
"/metrics"
,
promhttp
.
Handler
())
http
.
Handle
(
"/metrics"
,
promhttp
.
Handler
())
http
.
Handle
(
"/static"
,
http
.
StripPrefix
(
"/static/"
,
http
.
FileServer
(
http
.
Dir
(
*
staticDir
))))
http
.
Handle
(
"/static"
,
http
.
StripPrefix
(
"/static/"
,
http
.
FileServer
(
http
.
Dir
(
*
staticDir
))))
go
gatherMetrics
()
log
.
Fatal
(
http
.
ListenAndServe
(
*
addr
,
nil
))
log
.
Fatal
(
http
.
ListenAndServe
(
*
addr
,
nil
))
}
}
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