forked from grafana/groupcache_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (48 loc) · 1.22 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
// Package main implements the example.
package main
import (
"context"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/golang/groupcache"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/udhos/groupcache_exporter"
"github.com/udhos/groupcache_exporter/groupcache/google"
)
func main() {
appName := filepath.Base(os.Args[0])
cache := startGroupcache()
//
// expose prometheus metrics
//
{
metricsRoute := "/metrics"
metricsPort := ":3000"
log.Printf("starting metrics server at: %s %s", metricsPort, metricsRoute)
google := google.New(cache)
labels := map[string]string{
"app": appName,
}
namespace := ""
collector := groupcache_exporter.NewExporter(namespace, labels, google)
prometheus.MustRegister(collector)
go func() {
http.Handle(metricsRoute, promhttp.Handler())
log.Fatal(http.ListenAndServe(metricsPort, nil))
}()
}
//
// query cache periodically
//
const interval = 5 * time.Second
for {
var dst []byte
cache.Get(context.TODO(), "/etc/passwd", groupcache.AllocatingByteSliceSink(&dst))
log.Printf("cache answer: %d bytes, sleeping %v", len(dst), interval)
time.Sleep(interval)
}
}