forked from masih/fdbmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdbmeter.go
129 lines (121 loc) · 3.03 KB
/
fdbmeter.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package fdbmeter
import (
"context"
"encoding/json"
"log"
"net"
"net/http"
"sync/atomic"
"time"
"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const fdbStatusJsonKey = "\xff\xff/status/json"
type FDBMeter struct {
*options
db fdb.Database
status atomic.Pointer[Status]
metrics *Metrics
server *http.Server
}
func New(o ...Option) (*FDBMeter, error) {
opts, err := newOptions(o...)
if err != nil {
return nil, err
}
metrics, err := NewMetrics(opts.commonAttributes...)
if err != nil {
return nil, err
}
return &FDBMeter{
options: opts,
metrics: metrics,
}, nil
}
func (m *FDBMeter) ServeMux() *http.ServeMux {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
currentStatus := m.status.Load()
if currentStatus == nil {
http.Error(w, "No status found", http.StatusNotFound)
return
}
if err := json.NewEncoder(w).Encode(currentStatus); err != nil {
log.Printf("Failed to encode status: %v", err)
http.Error(w, "Failed to encode status as JSON", http.StatusInternalServerError)
}
default:
http.Error(w, "Not Allowed", http.StatusMethodNotAllowed)
}
})
return mux
}
func (m *FDBMeter) Start(ctx context.Context) error {
var err error
if err = fdb.APIVersion(m.fdbApiVersion); err != nil {
return err
}
if m.db, err = fdb.OpenDatabase(m.fdbClusterFile); err != nil {
return err
}
l, err := net.Listen("tcp", m.httpListenAddr)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(ctx)
go func() {
for {
select {
case <-ctx.Done():
log.Print("Stopping fdbmeter...")
return
case <-m.statusRefreshInterval.C:
start := time.Now()
resp, err := m.db.ReadTransact(func(tr fdb.ReadTransaction) (any, error) {
return tr.Get(fdb.Key(fdbStatusJsonKey)).Get()
})
if err != nil {
log.Printf("Failed to get status: %v", err)
m.status.Store(nil)
m.metrics.notifyStatusTransactFailed(ctx)
continue
}
switch v := resp.(type) {
case []byte:
var currentStatus Status
if err = json.Unmarshal(v, ¤tStatus); err != nil {
log.Printf("Failed to decode JSON status: %v", err)
m.status.Store(nil)
m.metrics.notifyStatusDecodeFailed(ctx)
continue
}
m.status.Store(¤tStatus)
m.metrics.notifyGetStatusLatency(ctx, time.Since(start))
m.metrics.notifyStatus(ctx, currentStatus)
log.Print("Refreshed status successfully")
default:
log.Printf("Unexpected status response kind: %v", v)
m.metrics.notifyStatusDecodeFailed(ctx)
}
}
}
}()
m.server = &http.Server{
Handler: m.ServeMux(),
}
go func() { _ = m.server.Serve(l) }()
m.server.RegisterOnShutdown(cancel)
return nil
}
func (m *FDBMeter) Shutdown(ctx context.Context) error {
if m.metrics != nil {
_ = m.metrics.shutdown(ctx)
}
if m.server != nil {
return m.server.Shutdown(ctx)
}
return nil
}