-
Notifications
You must be signed in to change notification settings - Fork 3
/
dummy_exporter.go
141 lines (129 loc) · 3.71 KB
/
dummy_exporter.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
package main
import (
"fmt"
"github.com/kobtea/dummy_exporter/pkg/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"time"
)
const (
namespace = "dummy"
)
var (
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry").Default(":9510").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
configFile = kingpin.Flag("config", "Path to config file").Default("").String()
)
type collector struct {
namespace string
config map[string]config.Metric
counters map[string]*prometheus.CounterVec
gauges map[string]*prometheus.GaugeVec
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func newCollector(namespace string, metrics []config.Metric) (*collector, error) {
c := map[string]config.Metric{}
counters := map[string]*prometheus.CounterVec{}
gauges := map[string]*prometheus.GaugeVec{}
for _, metric := range metrics {
var keys []string
for k := range metric.Labels {
keys = append(keys, k)
}
keys = append([]string{"id"}, keys...)
c[metric.Name] = metric
switch metric.Type {
case "counter":
counters[metric.Name] = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: metric.Name,
Help: "dummy counter",
}, keys)
case "gauge":
gauges[metric.Name] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: metric.Name,
Help: "dummy gauge",
}, keys)
default:
return nil, fmt.Errorf("invalid type: %s for %s", metric.Type, metric.Name)
}
}
return &collector{
namespace: namespace,
config: c,
counters: counters,
gauges: gauges,
}, nil
}
func (collector collector) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range collector.counters {
metric.Describe(ch)
}
for _, metric := range collector.gauges {
metric.Describe(ch)
}
}
func (collector collector) Collect(ch chan<- prometheus.Metric) {
for name, conf := range collector.config {
for i := 0; i < conf.Size; i++ {
labels := map[string]string{"id": strconv.Itoa(i)}
for key, vals := range conf.Labels {
labels[key] = vals[i%len(vals)]
}
switch conf.Type {
case "counter":
collector.counters[name].With(labels).Inc()
collector.counters[name].With(labels).Collect(ch)
case "gauge":
collector.gauges[name].With(labels).Set(rand.Float64())
collector.gauges[name].With(labels).Collect(ch)
default:
log.Errorf("invalid type: %s for %s", conf.Type, conf.Name)
}
}
}
}
func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("dummy_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
conf := &config.Config{}
if *configFile != "" {
buf, err := ioutil.ReadFile(*configFile)
if err != nil {
log.Fatal("failed to read config file")
}
conf, err = config.Parse(buf)
if err != nil {
log.Fatal("invalid config format")
}
}
collector, err := newCollector(namespace, conf.Metrics)
if err != nil {
log.Fatal(err)
}
prometheus.MustRegister(collector)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Dummy Exporter</title></head>
<body>
<h1>Dummy Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Infoln("listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}