1 parent
de13d851a8
commit
a28362b82c
34 files changed
+1541
-341
No files matched your search
@@ -1,5 +1,5 @@
|
||||
// Package telemetry derives time series from real EasyDNS counter polls.
|
||||
// EasyDNS REST does not expose history; no datapoints are synthesized.
|
||||
// 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 (
|
||||
@@ -10,33 +10,47 @@ import (
|
||||
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
|
||||
)
|
||||
|
||||
const maxSamples = 1440 // six hours at a fifteen-second sampling interval
|
||||
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:"-"`
|
||||
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
|
||||
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 {
|
||||
@@ -54,8 +68,28 @@ func delta(a, b uint64, secs float64) *float64 {
|
||||
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, P50MS: s.LatencyP50S * 1000, P95MS: s.LatencyP95S * 1000, P99MS: s.LatencyP99S * 1000}
|
||||
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 {
|
||||
@@ -67,19 +101,20 @@ func (h *History) Record(t time.Time, s easydns.Stats) {
|
||||
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
|
||||
}
|
||||
}
|
||||
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
|
||||
@@ -89,22 +124,21 @@ func (h *History) Failed(msg string) { h.mu.Lock(); defer h.mu.Unlock(); h.lastE
|
||||
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
|
||||
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
|
||||
ctxReq, cancel := context.WithTimeout(ctx, 8*time.Second)
|
||||
requestCtx, cancel := context.WithTimeout(ctx, 8*time.Second)
|
||||
defer cancel()
|
||||
if _, err := c.Get(ctxReq, "/api/v1/stats", &stats); err != nil {
|
||||
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(15 * time.Second)
|
||||
ticker := time.NewTicker(PollInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
|
||||
Reference in new issue
Block a user