commit
8c4d463f1e
96 files changed
+3319
No files matched your search
@@ -0,0 +1,111 @@
|
||||
// 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"`
|
||||
}
|
||||
|
||||
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
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSamplesAreMeasuredOnly(t *testing.T) {
|
||||
h := New()
|
||||
t0 := time.Unix(1000000, 0)
|
||||
h.Record(t0, easydns.Stats{QueriesTotal: 100, Responses: map[string]uint64{"noerror": 90}, Cache: easydns.Cache{Hits: 50, Misses: 10}, LatencyP95S: 0.014})
|
||||
sm, _, _ := h.Snapshot()
|
||||
if len(sm) != 1 || sm[0].QueryRate != nil {
|
||||
t.Fatal("must not fake initial rate")
|
||||
}
|
||||
h.Record(t0.Add(10*time.Second), easydns.Stats{QueriesTotal: 150, Responses: map[string]uint64{"noerror": 136}, Cache: easydns.Cache{Hits: 90, Misses: 20}, LatencyP95S: 0.010})
|
||||
sm, _, _ = h.Snapshot()
|
||||
if len(sm) != 2 || sm[1].QueryRate == nil || *sm[1].QueryRate != 5 {
|
||||
t.Fatalf("bad query rate: %+v", sm)
|
||||
}
|
||||
if sm[1].CacheHitRate == nil || *sm[1].CacheHitRate != 80 {
|
||||
t.Fatalf("bad cache ratio: %+v", sm[1].CacheHitRate)
|
||||
}
|
||||
if sm[1].P95MS != 10 {
|
||||
t.Fatalf("bad latency: %v", sm[1].P95MS)
|
||||
}
|
||||
h.Record(t0.Add(20*time.Second), easydns.Stats{QueriesTotal: 0, Responses: map[string]uint64{}, Cache: easydns.Cache{Hits: 0, Misses: 0}})
|
||||
sm, _, _ = h.Snapshot()
|
||||
if sm[2].QueryRate != nil || sm[2].CacheHitRate != nil {
|
||||
t.Fatal("counter reset must not produce negative/wrong rates")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user