blob: 5154659347c1ad9fd55f4d0bb1572ce14b11dd2d (
plain)
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
|
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"within.website/ln"
)
var (
readTimes = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "blogpage_read_times",
Help: "This tracks how much time people spend reading articles on my blog",
}, []string{"path"})
)
func init() {
_ = prometheus.Register(readTimes)
}
func handlePageViewTimer(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("DNT") == "1" {
http.NotFound(w, r)
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
ln.Error(r.Context(), err, ln.Info("while reading data"))
http.Error(w, "oopsie whoopsie uwu", http.StatusInternalServerError)
return
}
r.Body.Close()
type metricsData struct {
Path string `json:"path"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
var md metricsData
err = json.Unmarshal(data, &md)
if err != nil {
http.NotFound(w, r)
return
}
diff := md.EndTime.Sub(md.StartTime).Seconds()
readTimes.WithLabelValues(md.Path).Observe(float64(diff))
}
|