-
Notifications
You must be signed in to change notification settings - Fork 0
/
observe.go
49 lines (41 loc) · 1.1 KB
/
observe.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
// Copyright 2024 Factorial GmbH. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"net/http"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// Metrics exposed for collection by Prometheus.
var (
PromVisitTxns = promauto.NewCounter(prometheus.CounterOpts{
Name: "visits_txns_total",
Help: "The total number of visits.",
})
)
// High Frequency metrics, these should be mutated through atomic operations.
var (
PulseVisitTxns int32 // A gauge that is reset every second.
)
// startPulse starts a go routine that pushes updates to the pulse endpoint.
func startPulse(ctx context.Context) {
ticker := time.NewTicker(1 * time.Second)
go func() {
for {
select {
case <-ticker.C:
v := atomic.LoadInt32(&PulseVisitTxns)
atomic.StoreInt32(&PulseVisitTxns, 0)
rb := strings.NewReader(strconv.Itoa(int(v)))
http.Post(PulseEndpoint+"/rps", "text/plain", rb)
}
}
}()
}