-
Notifications
You must be signed in to change notification settings - Fork 85
/
monitoring.go
48 lines (42 loc) · 1.09 KB
/
monitoring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package opc
import (
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
opcReadsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "opc_reads_total",
Help: "Counts the total number of OPC tags read.",
},
[]string{"status"}, // "success" == 0 or "failed" == 1
)
opcReadsDuration = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "opc_reads_duration_seconds",
Help: "Read duration in seconds from OPC server.",
Buckets: prometheus.ExponentialBuckets(0.000001, 10, 6), // start with 500 ns, add 500 ns for 5 buckets.
},
)
)
func init() {
// Metrics have to be registered to be exposed:
prometheus.MustRegister(opcReadsCounter)
prometheus.MustRegister(opcReadsDuration)
}
//StartMonitoring exposes /metrics to Prometheus
func StartMonitoring(port string) {
var p string
if port == "" {
p = ":8080"
} else {
p = port
}
go func() {
log.Println("Listening on", p)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(p, nil))
}()
}