152 lines
5.4 KiB
Go
152 lines
5.4 KiB
Go
// Package telemetry derives time series from genuine EasyDNS REST counter polls.
|
|
// This is panel memory, not Prometheus or an invented DNS history endpoint.
|
|
package telemetry
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
|
|
)
|
|
|
|
const PollInterval = 15 * time.Second
|
|
const MaxHistory = 24 * time.Hour
|
|
const maxSamples = int(MaxHistory/PollInterval) + 1
|
|
|
|
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:"-"`
|
|
CacheEvictions uint64 `json:"-"`
|
|
Authoritative uint64 `json:"-"`
|
|
Recursive uint64 `json:"-"`
|
|
Forward uint64 `json:"-"`
|
|
Cached 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"`
|
|
RateLimitedRate *float64 `json:"rateLimitedRate"`
|
|
SourceAuthRate *float64 `json:"authoritativeRate"`
|
|
SourceRecursiveRate *float64 `json:"recursiveRate"`
|
|
SourceForwardRate *float64 `json:"forwardRate"`
|
|
SourceCacheRate *float64 `json:"cacheSourceRate"`
|
|
CacheHitsRate *float64 `json:"cacheHitsRate"`
|
|
CacheMissesRate *float64 `json:"cacheMissesRate"`
|
|
CacheEvictionsRate *float64 `json:"cacheEvictionsRate"`
|
|
CacheHitRate *float64 `json:"cacheHitRate"`
|
|
ErrorRate *float64 `json:"errorRate"`
|
|
CacheEntries float64 `json:"cacheEntries"`
|
|
P50MS float64 `json:"p50ms"`
|
|
P95MS float64 `json:"p95ms"`
|
|
P99MS float64 `json:"p99ms"`
|
|
}
|
|
|
|
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 cachePercent(hits, misses *float64) *float64 {
|
|
if hits == nil || misses == nil || *hits+*misses <= 0 {
|
|
return nil
|
|
}
|
|
v := 100 * *hits / (*hits + *misses)
|
|
return &v
|
|
}
|
|
func errorPercent(total, servfail, refused *float64) *float64 {
|
|
if total == nil || servfail == nil || refused == nil || *total <= 0 {
|
|
return nil
|
|
}
|
|
v := 100 * (*servfail + *refused) / *total
|
|
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, CacheEvictions: s.Cache.Evictions,
|
|
Authoritative: s.Sources["authoritative"], Recursive: s.Sources["recursive"], Forward: s.Sources["forward"], Cached: s.Sources["cache"],
|
|
CacheEntries: float64(s.Cache.Entries), 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)
|
|
p.RateLimitedRate = delta(prev.RateLimited, p.RateLimited, secs)
|
|
p.SourceAuthRate = delta(prev.Authoritative, p.Authoritative, secs)
|
|
p.SourceRecursiveRate = delta(prev.Recursive, p.Recursive, secs)
|
|
p.SourceForwardRate = delta(prev.Forward, p.Forward, secs)
|
|
p.SourceCacheRate = delta(prev.Cached, p.Cached, secs)
|
|
p.CacheHitsRate = delta(prev.CacheHits, p.CacheHits, secs)
|
|
p.CacheMissesRate = delta(prev.CacheMisses, p.CacheMisses, secs)
|
|
p.CacheEvictionsRate = delta(prev.CacheEvictions, p.CacheEvictions, secs)
|
|
p.CacheHitRate = cachePercent(p.CacheHitsRate, p.CacheMissesRate)
|
|
p.ErrorRate = errorPercent(p.QueryRate, p.ServFailRate, p.RefusedRate)
|
|
}
|
|
h.samples = append(h.samples, p)
|
|
if len(h.samples) > maxSamples {
|
|
// Copy to free old backing arrays rather than retaining unbounded memory.
|
|
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()
|
|
return append([]Sample(nil), h.samples...), h.lastErr, h.lastPoll
|
|
}
|
|
func (h *History) Run(ctx context.Context, c *easydns.Client) {
|
|
poll := func() {
|
|
var stats easydns.Stats
|
|
requestCtx, cancel := context.WithTimeout(ctx, 8*time.Second)
|
|
defer cancel()
|
|
if _, err := c.Get(requestCtx, "/api/v1/stats", &stats); err != nil {
|
|
h.Failed("API sampling unavailable")
|
|
return
|
|
}
|
|
h.Record(time.Now().UTC(), stats)
|
|
}
|
|
poll()
|
|
ticker := time.NewTicker(PollInterval)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
poll()
|
|
}
|
|
}
|
|
}
|