...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package main
18
19 import (
20 "fmt"
21 "log"
22 "math/rand"
23 "net/http"
24 "time"
25
26 "github.com/prometheus/client_golang/prometheus"
27 "github.com/prometheus/client_golang/prometheus/collectors"
28 "github.com/prometheus/client_golang/prometheus/promhttp"
29 )
30
31 func main() {
32 requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{
33 Name: "http_request_duration_seconds",
34 Help: "A histogram of the HTTP request durations in seconds.",
35 Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5),
36 })
37
38
39 registry := prometheus.NewRegistry()
40
41
42 registry.MustRegister(
43 collectors.NewGoCollector(),
44 collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
45 requestDurations,
46 )
47
48 go func() {
49 for {
50
51 now := time.Now()
52 requestDurations.(prometheus.ExemplarObserver).ObserveWithExemplar(
53 time.Since(now).Seconds(), prometheus.Labels{"dummyID": fmt.Sprint(rand.Intn(100000))},
54 )
55 time.Sleep(600 * time.Millisecond)
56 }
57 }()
58
59
60 http.Handle(
61 "/metrics", promhttp.HandlerFor(
62 registry,
63 promhttp.HandlerOpts{
64 EnableOpenMetrics: true,
65 }),
66 )
67
68 log.Fatalln(http.ListenAndServe(":8080", nil))
69 }
70
View as plain text