-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (154 loc) · 4.49 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
180
181
182
183
package main
import (
"net/http"
"os"
"sync"
"time"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
const (
namespace = "mini"
metricsGroupDemo = "demo"
)
var (
allMetrics map[string]metricInfo
collectMetricsGroup map[string]bool
)
type metricInfo struct {
Desc *prometheus.Desc
Type prometheus.ValueType
}
func MetricsGroup_Values() []string {
return []string{
metricsGroupDemo,
}
}
func newMetricInfo(instanceName string, metricName string, docString string, t prometheus.ValueType, variableLabels []string, constLabels prometheus.Labels) metricInfo {
return metricInfo{
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, instanceName, metricName),
docString,
variableLabels,
constLabels,
),
Type: t,
}
}
func createMetrics(instanceName string) {
allMetrics = make(map[string]metricInfo)
allMetrics["demo"] = newMetricInfo(instanceName, "demo", "Mini overall demo show current unix time", prometheus.GaugeValue, nil, nil)
}
type MiniExporter struct {
instance string
// Cache-releated
cacheEnabled bool
cacheDuration time.Duration
lastCollectTime time.Time
cache []prometheus.Metric
collectMutex sync.Mutex
}
// NewMiniExporter constructs a MiniExporter instance
func NewMiniExporter() *MiniExporter {
return &MiniExporter{
cache: make([]prometheus.Metric, 0),
lastCollectTime: time.Unix(0, 0),
collectMutex: sync.Mutex{},
}
}
// Describe describes all the metrics ever exported by the Mini exporter. It
// implements prometheus.Collector.
func (e *MiniExporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range allMetrics {
ch <- m.Desc
}
}
// Collect fetches the stats from configured Mini location and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *MiniExporter) Collect(outCh chan<- prometheus.Metric) {
if e.cacheEnabled {
e.collectMutex.Lock()
defer e.collectMutex.Unlock()
expiry := e.lastCollectTime.Add(e.cacheDuration)
if time.Now().Before(expiry) {
// Return cached
for _, cachedMetric := range e.cache {
outCh <- cachedMetric
}
return
}
// Reset cache for fresh sampling, but re-use underlying array
e.cache = e.cache[:0]
}
samplesCh := make(chan prometheus.Metric)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for metric := range samplesCh {
outCh <- metric
if e.cacheEnabled {
e.cache = append(e.cache, metric)
}
}
wg.Done()
}()
e.collectDemoMetric(samplesCh)
close(samplesCh)
e.lastCollectTime = time.Now()
wg.Wait()
}
func main() {
var (
listenAddress = kingpin.Flag(
"web.listen-address",
"Address on which to expose metrics and web interface.",
).Default(":9100").String()
metricsPath = kingpin.Flag(
"web.telemetry-path",
"Path under which to expose metrics.",
).Default("/metrics").String()
)
var miniInstance = NewMiniExporter()
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
collectMetricsGroup = make(map[string]bool)
for _, v := range MetricsGroup_Values() {
collectMetricsGroup[v] = true
}
for k, v := range collectMetricsGroup {
level.Info(logger).Log("metrics_group", k, "collect", v)
}
createMetrics(miniInstance.instance)
prometheus.MustRegister(miniInstance)
prometheus.MustRegister(version.NewCollector("mini_exporter"))
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Mini Exporter</title></head>
<body>
<h1>Mini Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
<h2>Build</h2>
<pre>` + version.Info() + ` ` + version.BuildContext() + `</pre>
</body>
</html>`))
})
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}
// 收集指标数据: 即当前Unix时间戳
func (e *MiniExporter) collectDemoMetric(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
allMetrics["demo"].Desc, allMetrics["demo"].Type, float64(time.Now().Local().Unix()),
)
}