generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprometheus.go
103 lines (82 loc) · 2.87 KB
/
prometheus.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
package main
import (
"context"
"net/http"
"os"
"time"
"github.com/VictoriaMetrics/metrics"
"github.com/johnwarden/httperror"
"golang.org/x/exp/slog"
)
// Register various metrics.
// Metric name may contain labels in Prometheus format - see below.
var (
crawlErrorsTotal = metrics.NewCounter(`errors_total{type="crawl"}`)
requestErrorsTotal = metrics.NewCounter(`errors_total{type="request"}`)
crawlDuration = metrics.NewHistogram("crawl_duration_seconds")
crawlPostprocessingDuration = metrics.NewHistogram("crawl_postprocessing_duration_seconds")
upvotesTotal = metrics.NewCounter(`upvotes_total`)
submissionsTotal = metrics.NewCounter(`submissions_total`)
databaseSizeBytes *metrics.Gauge
databaseFragmentationPercent *metrics.Gauge
vacuumOperationsTotal = metrics.NewCounter(`database_vacuum_operations_total{database="frontpage"}`)
// Store histograms per route to avoid duplicate registration
routeHistograms = make(map[string]*metrics.Histogram)
)
// getRouteHistogram returns an existing histogram for a route or creates a new one
func getRouteHistogram(routeName string) *metrics.Histogram {
if h, exists := routeHistograms[routeName]; exists {
return h
}
h := metrics.NewHistogram(`requests_duration_seconds{route="` + routeName + `"}`)
routeHistograms[routeName] = h
return h
}
func servePrometheusMetrics() func(ctx context.Context) error {
mux := http.NewServeMux()
// Export all the registered metrics in Prometheus format at `/metrics` http path.
mux.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
metrics.WritePrometheus(w, true)
})
listenAddress := os.Getenv("LISTEN_ADDRESS")
s := &http.Server{
Addr: listenAddress + ":9091",
Handler: mux,
}
go func() {
LogFatal(slog.Default(), "Listen and serve prometheus", s.ListenAndServe())
}()
return s.Shutdown
}
func prometheusMiddleware[P any](routeName string, h httperror.XHandler[P]) httperror.XHandlerFunc[P] {
requestDuration := getRouteHistogram(routeName)
return func(w http.ResponseWriter, r *http.Request, p P) error {
var startTime time.Time
if r.Method != http.MethodHead {
startTime = time.Now()
}
err := h.Serve(w, r, p)
if r.Method != http.MethodHead {
requestDuration.UpdateDuration(startTime)
}
return err
}
}
func (app *app) initDatabaseMetrics() {
databaseSizeBytes = metrics.NewGauge(`database_size_bytes{database="frontpage"}`, func() float64 {
size, _, _, err := app.ndb.getDatabaseStats()
if err != nil {
app.logger.Error("getDatabaseStats", err)
return 0
}
return float64(size)
})
databaseFragmentationPercent = metrics.NewGauge(`database_fragmentation_percent{database="frontpage"}`, func() float64 {
_, _, fragmentation, err := app.ndb.getDatabaseStats()
if err != nil {
app.logger.Error("getDatabaseStats", err)
return 0
}
return fragmentation
})
}