Files
EasyDNS-Web/internal/telemetry/history.go
T
Mateusz Gruszczyński de13d851a8
Go quality gates / test (push) Waiting to run
v0.1.1
2026-11-30 14:44:28 +01:00

118 lines
3.8 KiB
Go

// Package telemetry derives time series from real EasyDNS counter polls.
// EasyDNS REST does not expose history; no datapoints are synthesized.
package telemetry
import (
"context"
"sync"
"time"
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
)
const maxSamples = 1440 // six hours at a fifteen-second sampling interval
type Sample struct {
Time time.Time `json:"time"`
QueriesTotal uint64 `json:"-"`
NoError uint64 `json:"-"`
NXDomain uint64 `json:"-"`
ServFail uint64 `json:"-"`
Refused uint64 `json:"-"`
Blocked uint64 `json:"-"`
RateLimited uint64 `json:"-"`
CacheHits uint64 `json:"-"`
CacheMisses uint64 `json:"-"`
QueryRate *float64 `json:"queryRate"`
NoErrorRate *float64 `json:"noerror"`
NXDomainRate *float64 `json:"nxdomain"`
ServFailRate *float64 `json:"servfail"`
RefusedRate *float64 `json:"refused"`
BlockedRate *float64 `json:"blocked"`
CacheHitRate *float64 `json:"cacheHitRate"`
P50MS float64 `json:"p50ms"`
P95MS float64 `json:"p95ms"`
P99MS float64 `json:"p99ms"`
// Per-second cache increments allow mathematically weighted fleet cache
// ratios. These are derived from real counters and never sent to the browser.
cacheHitQPS *float64
cacheMissQPS *float64
}
type History struct {
mu sync.RWMutex
samples []Sample
lastErr string
lastPoll time.Time
}
func New() *History { return &History{samples: make([]Sample, 0, maxSamples)} }
func delta(a, b uint64, secs float64) *float64 {
if secs <= 0 || b < a {
return nil
}
v := float64(b-a) / secs
return &v
}
func (h *History) Record(t time.Time, s easydns.Stats) {
p := Sample{Time: t, QueriesTotal: s.QueriesTotal, NoError: s.Responses["noerror"], NXDomain: s.Responses["nxdomain"], ServFail: s.Responses["servfail"], Refused: s.Responses["refused"], Blocked: s.Blocked, RateLimited: s.RateLimited, CacheHits: s.Cache.Hits, CacheMisses: s.Cache.Misses, P50MS: s.LatencyP50S * 1000, P95MS: s.LatencyP95S * 1000, P99MS: s.LatencyP99S * 1000}
h.mu.Lock()
defer h.mu.Unlock()
if len(h.samples) > 0 {
prev := h.samples[len(h.samples)-1]
secs := t.Sub(prev.Time).Seconds()
p.QueryRate = delta(prev.QueriesTotal, p.QueriesTotal, secs)
p.NoErrorRate = delta(prev.NoError, p.NoError, secs)
p.NXDomainRate = delta(prev.NXDomain, p.NXDomain, secs)
p.ServFailRate = delta(prev.ServFail, p.ServFail, secs)
p.RefusedRate = delta(prev.Refused, p.Refused, secs)
p.BlockedRate = delta(prev.Blocked, p.Blocked, secs)
if p.CacheHits >= prev.CacheHits && p.CacheMisses >= prev.CacheMisses {
hits := p.CacheHits - prev.CacheHits
misses := p.CacheMisses - prev.CacheMisses
p.cacheHitQPS = delta(prev.CacheHits, p.CacheHits, secs)
p.cacheMissQPS = delta(prev.CacheMisses, p.CacheMisses, secs)
if hits+misses > 0 {
v := 100 * float64(hits) / float64(hits+misses)
p.CacheHitRate = &v
}
}
}
h.samples = append(h.samples, p)
if len(h.samples) > maxSamples {
h.samples = append([]Sample(nil), h.samples[len(h.samples)-maxSamples:]...)
}
h.lastPoll = t
h.lastErr = ""
}
func (h *History) Failed(msg string) { h.mu.Lock(); defer h.mu.Unlock(); h.lastErr = msg }
func (h *History) Snapshot() ([]Sample, string, time.Time) {
h.mu.RLock()
defer h.mu.RUnlock()
v := append([]Sample(nil), h.samples...)
return v, h.lastErr, h.lastPoll
}
func (h *History) Run(ctx context.Context, c *easydns.Client) {
poll := func() {
var stats easydns.Stats
ctxReq, cancel := context.WithTimeout(ctx, 8*time.Second)
defer cancel()
if _, err := c.Get(ctxReq, "/api/v1/stats", &stats); err != nil {
h.Failed("API sampling unavailable")
return
}
h.Record(time.Now().UTC(), stats)
}
poll()
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
poll()
}
}
}