-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
36 lines (32 loc) · 832 Bytes
/
metrics.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log/slog"
"net/http"
)
var (
wordFrequency = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "web_scraper_word_frequency",
Help: "The frequency of each word on scraped URLs",
},
[]string{"url", "word"},
)
)
func UpdateMetrics(url string, wordCounts map[string]int) {
for word, count := range wordCounts {
wordFrequency.WithLabelValues(url, word).Set(float64(count))
}
}
func RunMetricsServer(addr string) {
http.Handle("/metrics", promhttp.Handler())
go func() {
slog.Info("serving on ", "addr", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
slog.Error("error serving: %s", err)
}
}()
}