-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
179 lines (161 loc) · 4.16 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/fsnotify/fsnotify"
)
var (
metricsDir = flag.String("metrics-directory", "/metrics", "The directory to read metrics from")
metricsPath = flag.String("metrics-path", "/metrics", "The http path under which metrics are exposed")
listenAddr = flag.String("listen-addr", ":8080", "The address to listen on for http requests")
silent = flag.Bool("silent", false, "Silent mode - no logging of metric changes")
metrics = make(map[string]*prometheus.GaugeVec)
)
func main() {
flag.Parse()
files, err := filepath.Glob(filepath.Join(*metricsDir, "*"))
if err != nil {
log.Fatal(err)
}
for _, file := range files {
updateMetric(file)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
go func() {
for {
select {
case event := <-watcher.Events:
switch event.Op {
case fsnotify.Create:
updateMetric(event.Name)
case fsnotify.Write:
updateMetric(event.Name)
case fsnotify.Remove:
removeMetric(event.Name)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
err = watcher.Add(*metricsDir)
if err != nil {
log.Fatal(err)
}
http.Handle(*metricsPath, promhttp.Handler())
if !*silent {
log.Println("Watching " + *metricsDir + " for metrics")
}
log.Fatal(http.ListenAndServe(*listenAddr, nil))
}
func getOrCreateMetricForPath(path string) *prometheus.GaugeVec {
metricName := metricNameFromPath(path)
metric, ok := metrics[metricName]
if ok {
return metric
}
if !*silent {
log.Println("Attempting to generate metric from file: " + path)
}
labels := labelsFromPath(path)
labelKeys := make([]string, 0, len(labels))
for k := range labels {
labelKeys = append(labelKeys, k)
}
metric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metricName,
Help: "Auto generated from filesystem path: " + *metricsDir + "/" + metricName,
},
labelKeys)
prometheus.MustRegister(metric)
metrics[metricName] = metric
return metric
}
func updateMetric(path string) {
if pathIsDir(path) {
return
}
if !*silent {
log.Println("Attempting to update metric with value written to: " + path)
}
metric := getOrCreateMetricForPath(path)
dat, err := ioutil.ReadFile(path)
if err != nil {
log.Println(err)
return
}
value, err := strconv.ParseFloat(strings.TrimSpace(string(dat)), 64)
if err != nil {
log.Println(err)
return
}
if !*silent {
log.Println("Setting metric for " + path + " to " + strconv.FormatFloat(value, 'f', 10, 64))
}
defer func() {
if r := recover(); r != nil {
log.Println("Error in metric:", r)
}
}()
metric.With(labelsFromPath(path)).Set(value)
}
func removeMetric(path string) {
if !*silent {
log.Println("Attempting to remove metric because of deleted file: " + path)
}
metric := metrics[path]
if metric == nil {
return
}
prometheus.Unregister(metric)
delete(metrics, path)
}
func pathIsDir(path string) bool {
file, err := os.Stat(path)
if err != nil {
log.Println(err)
return true
}
switch mode := file.Mode(); {
case mode.IsDir():
log.Println(path + " is a directory")
return true
default:
return false
}
}
func filenameFromPath(path string) string {
var pathComponents = strings.Split(path, "/")
return pathComponents[len(pathComponents)-1]
}
func metricNameFromPath(path string) string {
return strings.Split(filenameFromPath(path), ";")[0]
}
func labelsFromPath(path string) map[string]string {
labels := make(map[string]string)
labelsFromFilename := strings.Split(filenameFromPath(path), ";")
//remove the first element - it's the metrics name
labelsFromFilename = labelsFromFilename[1:]
for _, labelPair := range labelsFromFilename {
splittedLabelPair := strings.Split(labelPair, "=")
if len(splittedLabelPair) != 2 {
log.Println(labelPair + " in file" + path + " is invalid - please make sure all filenames have the following format: metricName;label=value;label=value")
continue
}
labels[splittedLabelPair[0]] = splittedLabelPair[1]
}
return labels
}