aboutsummaryrefslogtreecommitdiff
path: root/internal/api/observability.go
blob: ebd7711cf0612e1f50aa50455e1785261e2f48e2 (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package api

import (
	"net"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"time"

	"github.com/Fuwn/plutia/internal/config"
	"github.com/Fuwn/plutia/internal/ingest"
	"github.com/Fuwn/plutia/internal/storage"
	"github.com/Fuwn/plutia/internal/types"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

type BuildInfo struct {
	Version   string `json:"version"`
	Commit    string `json:"commit"`
	BuildDate string `json:"build_date"`
	GoVersion string `json:"go_version"`
}

type serverMetrics struct {
	registry           *prometheus.Registry
	checkpointDuration prometheus.Histogram
	checkpointSequence prometheus.Gauge
}

func newServerMetrics(cfg config.Config, store storage.Store, ingestor *ingest.Service) *serverMetrics {
	reg := prometheus.NewRegistry()
	var diskMu sync.Mutex
	var diskCached int64
	var diskCachedAt time.Time
	m := &serverMetrics{
		registry: reg,
		checkpointDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
			Name:    "checkpoint_duration_seconds",
			Help:    "Time spent generating and signing checkpoints.",
			Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 20, 60},
		}),
		checkpointSequence: prometheus.NewGauge(prometheus.GaugeOpts{
			Name: "checkpoint_sequence",
			Help: "Latest checkpoint sequence generated by this mirror.",
		}),
	}
	reg.MustRegister(m.checkpointDuration, m.checkpointSequence)
	reg.MustRegister(prometheus.NewCounterFunc(
		prometheus.CounterOpts{
			Name: "ingest_ops_total",
			Help: "Total operations persisted by the mirror.",
		},
		func() float64 {
			seq, err := store.GetGlobalSeq()
			if err != nil {
				return 0
			}
			return float64(seq)
		},
	))
	reg.MustRegister(prometheus.NewGaugeFunc(
		prometheus.GaugeOpts{
			Name: "ingest_ops_per_second",
			Help: "Ingestion operations per second (process-average).",
		},
		func() float64 {
			if ingestor == nil {
				return 0
			}
			return ingestor.Stats().IngestOpsPerSec
		},
	))
	reg.MustRegister(prometheus.NewGaugeFunc(
		prometheus.GaugeOpts{
			Name: "ingest_lag_ops",
			Help: "Difference between latest observed upstream sequence and committed global sequence.",
		},
		func() float64 {
			if ingestor == nil {
				return 0
			}
			return float64(ingestor.Stats().LagOps)
		},
	))
	reg.MustRegister(prometheus.NewCounterFunc(
		prometheus.CounterOpts{
			Name: "verify_failures_total",
			Help: "Total signature/link verification failures seen during ingestion.",
		},
		func() float64 {
			if ingestor == nil {
				return 0
			}
			return float64(ingestor.Stats().VerifyFailures)
		},
	))
	reg.MustRegister(prometheus.NewGaugeFunc(
		prometheus.GaugeOpts{
			Name: "disk_bytes_total",
			Help: "Total bytes used by the configured data directory.",
		},
		func() float64 {
			diskMu.Lock()
			defer diskMu.Unlock()
			if diskCachedAt.IsZero() || time.Since(diskCachedAt) > 5*time.Second {
				size, err := dirSize(cfg.DataDir)
				if err == nil {
					diskCached = size
					diskCachedAt = time.Now()
				}
			}
			return float64(diskCached)
		},
	))
	reg.MustRegister(prometheus.NewGaugeFunc(
		prometheus.GaugeOpts{
			Name: "did_count",
			Help: "Number of DIDs currently materialized in state storage.",
		},
		func() float64 {
			if ingestor != nil {
				return float64(ingestor.Stats().DIDCount)
			}
			count := uint64(0)
			_ = store.ForEachState(func(_ types.StateV1) error {
				count++
				return nil
			})
			return float64(count)
		},
	))
	return m
}

func (m *serverMetrics) Handler() http.Handler {
	return promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{
		EnableOpenMetrics: true,
	})
}

func (m *serverMetrics) ObserveCheckpoint(duration time.Duration, sequence uint64) {
	m.checkpointDuration.Observe(duration.Seconds())
	m.checkpointSequence.Set(float64(sequence))
}

type serverOption func(*Server)

func WithBuildInfo(info BuildInfo) serverOption {
	return func(s *Server) {
		s.build = info
	}
}

type limiterClass int

const (
	limiterResolve limiterClass = iota
	limiterProof
)

type tokenBucket struct {
	tokens   float64
	last     time.Time
	lastSeen time.Time
}

type endpointPolicy struct {
	rps   float64
	burst float64
}

type ipRateLimiter struct {
	mu        sync.Mutex
	buckets   map[string]*tokenBucket
	resolve   endpointPolicy
	proof     endpointPolicy
	lastSweep time.Time
}

func newIPRateLimiter(cfg config.RateLimit) *ipRateLimiter {
	def := config.Default().RateLimit
	if cfg.ResolveRPS <= 0 {
		cfg.ResolveRPS = def.ResolveRPS
	}
	if cfg.ResolveBurst <= 0 {
		cfg.ResolveBurst = def.ResolveBurst
	}
	if cfg.ProofRPS <= 0 {
		cfg.ProofRPS = def.ProofRPS
	}
	if cfg.ProofBurst <= 0 {
		cfg.ProofBurst = def.ProofBurst
	}
	return &ipRateLimiter{
		buckets: map[string]*tokenBucket{},
		resolve: endpointPolicy{
			rps:   cfg.ResolveRPS,
			burst: float64(cfg.ResolveBurst),
		},
		proof: endpointPolicy{
			rps:   cfg.ProofRPS,
			burst: float64(cfg.ProofBurst),
		},
		lastSweep: time.Now(),
	}
}

func (l *ipRateLimiter) Allow(ip string, class limiterClass) bool {
	now := time.Now()
	l.mu.Lock()
	defer l.mu.Unlock()

	if now.Sub(l.lastSweep) > 2*time.Minute {
		for key, bucket := range l.buckets {
			if now.Sub(bucket.lastSeen) > 15*time.Minute {
				delete(l.buckets, key)
			}
		}
		l.lastSweep = now
	}

	policy := l.resolve
	routeKey := "resolve"
	if class == limiterProof {
		policy = l.proof
		routeKey = "proof"
	}
	key := routeKey + "|" + ip
	b, ok := l.buckets[key]
	if !ok {
		l.buckets[key] = &tokenBucket{
			tokens:   policy.burst - 1,
			last:     now,
			lastSeen: now,
		}
		return true
	}
	elapsed := now.Sub(b.last).Seconds()
	if elapsed > 0 {
		b.tokens += elapsed * policy.rps
		if b.tokens > policy.burst {
			b.tokens = policy.burst
		}
	}
	b.last = now
	b.lastSeen = now
	if b.tokens < 1 {
		return false
	}
	b.tokens--
	return true
}

func clientIP(r *http.Request) string {
	if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-For")); forwarded != "" {
		parts := strings.Split(forwarded, ",")
		if len(parts) > 0 {
			if ip := strings.TrimSpace(parts[0]); ip != "" {
				return ip
			}
		}
	}
	if realIP := strings.TrimSpace(r.Header.Get("X-Real-IP")); realIP != "" {
		return realIP
	}
	host, _, err := net.SplitHostPort(r.RemoteAddr)
	if err == nil && host != "" {
		return host
	}
	return r.RemoteAddr
}

func dirSize(path string) (int64, error) {
	var total int64
	err := filepath.WalkDir(path, func(_ string, d os.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.IsDir() {
			return nil
		}
		info, err := d.Info()
		if err != nil {
			return err
		}
		total += info.Size()
		return nil
	})
	return total, err
}