1 parent
de13d851a8
commit
a28362b82c
34 files changed
+1541
-341
No files matched your search
+47
-17
@@ -20,15 +20,25 @@ type FleetNode struct {
|
||||
// valid. Percentile latency is intentionally excluded: it cannot be computed
|
||||
// from separately reported per-node p50/p95/p99 values.
|
||||
type FleetPoint struct {
|
||||
Time time.Time `json:"time"`
|
||||
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"`
|
||||
Contributors int `json:"contributors"`
|
||||
Time time.Time `json:"time"`
|
||||
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"`
|
||||
CacheEntries *float64 `json:"cacheEntries"`
|
||||
ErrorRate *float64 `json:"errorRate"`
|
||||
CacheHitRate *float64 `json:"cacheHitRate"`
|
||||
Contributors int `json:"contributors"`
|
||||
}
|
||||
|
||||
const fleetStep = 15 * time.Second
|
||||
@@ -63,7 +73,7 @@ func AggregateFleet(nodes []FleetNode) []FleetPoint {
|
||||
if end.Before(start) {
|
||||
return nil
|
||||
}
|
||||
// Bound comparison data to the same six-hour, 1440-poll memory horizon as
|
||||
// Bound comparison data to the same 24-hour rolling memory horizon as
|
||||
// each node's History. Never add empty points before the first poll.
|
||||
if end.Sub(start) > time.Duration(maxSamples-1)*fleetStep {
|
||||
start = end.Add(-time.Duration(maxSamples-1) * fleetStep)
|
||||
@@ -73,8 +83,10 @@ func AggregateFleet(nodes []FleetNode) []FleetPoint {
|
||||
for t := start; !t.After(end); t = t.Add(fleetStep) {
|
||||
point := FleetPoint{Time: t}
|
||||
var query, noerror, nxdomain, servfail, refused, blocked float64
|
||||
var hitQPS, missQPS float64
|
||||
var hitQPS, missQPS, evictQPS, cacheEntries float64
|
||||
var limit, auth, recursive, forward, cached float64
|
||||
cacheComplete := true
|
||||
extraComplete := true
|
||||
for i, n := range nodes {
|
||||
for index[i] < len(n.Samples) && !n.Samples[index[i]].Time.After(t) {
|
||||
index[i]++
|
||||
@@ -95,20 +107,38 @@ func AggregateFleet(nodes []FleetNode) []FleetPoint {
|
||||
servfail += *sample.ServFailRate
|
||||
refused += *sample.RefusedRate
|
||||
blocked += *sample.BlockedRate
|
||||
if sample.cacheHitQPS == nil || sample.cacheMissQPS == nil {
|
||||
if sample.CacheHitsRate == nil || sample.CacheMissesRate == nil {
|
||||
cacheComplete = false
|
||||
} else {
|
||||
hitQPS += *sample.cacheHitQPS
|
||||
missQPS += *sample.cacheMissQPS
|
||||
hitQPS += *sample.CacheHitsRate
|
||||
missQPS += *sample.CacheMissesRate
|
||||
}
|
||||
if sample.RateLimitedRate == nil || sample.SourceAuthRate == nil || sample.SourceRecursiveRate == nil || sample.SourceForwardRate == nil || sample.SourceCacheRate == nil || sample.CacheEvictionsRate == nil {
|
||||
extraComplete = false
|
||||
} else {
|
||||
limit += *sample.RateLimitedRate
|
||||
auth += *sample.SourceAuthRate
|
||||
recursive += *sample.SourceRecursiveRate
|
||||
forward += *sample.SourceForwardRate
|
||||
cached += *sample.SourceCacheRate
|
||||
evictQPS += *sample.CacheEvictionsRate
|
||||
}
|
||||
cacheEntries += sample.CacheEntries
|
||||
}
|
||||
if point.Contributors == len(nodes) {
|
||||
point.QueryRate, point.NoErrorRate, point.NXDomainRate = &query, &noerror, &nxdomain
|
||||
point.ServFailRate, point.RefusedRate, point.BlockedRate = &servfail, &refused, &blocked
|
||||
if cacheComplete && hitQPS+missQPS > 0 {
|
||||
ratio := 100 * hitQPS / (hitQPS + missQPS)
|
||||
point.CacheHitRate = &ratio
|
||||
if cacheComplete {
|
||||
point.CacheHitsRate, point.CacheMissesRate = &hitQPS, &missQPS
|
||||
point.CacheHitRate = cachePercent(point.CacheHitsRate, point.CacheMissesRate)
|
||||
}
|
||||
point.CacheEntries = &cacheEntries
|
||||
if extraComplete {
|
||||
point.RateLimitedRate, point.SourceAuthRate = &limit, &auth
|
||||
point.SourceRecursiveRate, point.SourceForwardRate = &recursive, &forward
|
||||
point.SourceCacheRate, point.CacheEvictionsRate = &cached, &evictQPS
|
||||
}
|
||||
point.ErrorRate = errorPercent(point.QueryRate, point.ServFailRate, point.RefusedRate)
|
||||
}
|
||||
points = append(points, point)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
package telemetry
|
||||
|
||||
import "time"
|
||||
|
||||
// Window specifies only measured history from the Go panel's authenticated
|
||||
// telemetry endpoint. An explicit step enables server-side downsampling.
|
||||
// The legacy API with no step parameter keeps its original raw 15 s samples.
|
||||
type Window struct {
|
||||
Range string `json:"range"`
|
||||
Step string `json:"step"`
|
||||
Span time.Duration `json:"-"`
|
||||
Bucket time.Duration `json:"-"`
|
||||
From time.Time `json:"from"`
|
||||
To time.Time `json:"to"`
|
||||
StepSeconds int `json:"stepSeconds"`
|
||||
}
|
||||
|
||||
func ParseWindow(rangeValue, stepValue string, now time.Time) Window {
|
||||
spans := map[string]time.Duration{"15m": 15 * time.Minute, "1h": time.Hour, "6h": 6 * time.Hour, "12h": 12 * time.Hour, "24h": 24 * time.Hour}
|
||||
span, ok := spans[rangeValue]
|
||||
if !ok {
|
||||
rangeValue = "24h"
|
||||
span = 24 * time.Hour
|
||||
}
|
||||
steps := map[string]time.Duration{"15s": 15 * time.Second, "30s": 30 * time.Second, "1m": time.Minute, "5m": 5 * time.Minute, "15m": 15 * time.Minute, "1h": time.Hour}
|
||||
if stepValue == "" {
|
||||
stepValue = "15s"
|
||||
} // preserve old /ui/telemetry callers
|
||||
bucket, ok := steps[stepValue]
|
||||
if stepValue == "auto" {
|
||||
switch {
|
||||
case span <= time.Hour:
|
||||
bucket = 15 * time.Second
|
||||
case span <= 6*time.Hour:
|
||||
bucket = time.Minute
|
||||
default:
|
||||
bucket = 5 * time.Minute
|
||||
}
|
||||
} else if !ok {
|
||||
stepValue = "auto"
|
||||
bucket = 5 * time.Minute
|
||||
if span <= 6*time.Hour {
|
||||
bucket = time.Minute
|
||||
}
|
||||
if span <= time.Hour {
|
||||
bucket = 15 * time.Second
|
||||
}
|
||||
}
|
||||
if bucket < PollInterval {
|
||||
bucket = PollInterval
|
||||
}
|
||||
return Window{Range: rangeValue, Step: stepValue, Span: span, Bucket: bucket, From: now.Add(-span).UTC(), To: now.UTC(), StepSeconds: int(bucket / time.Second)}
|
||||
}
|
||||
|
||||
func WindowSamples(samples []Sample, w Window) []Sample {
|
||||
// Do not exclude a positive clock skew in future samples: the window
|
||||
// is a lower bound. Visual time clipping uses the independently returned
|
||||
// 'to' value, so future clocks cannot extend the drawn chart backwards.
|
||||
filtered := make([]Sample, 0, len(samples))
|
||||
for _, p := range samples {
|
||||
if !p.Time.Before(w.From) {
|
||||
filtered = append(filtered, p)
|
||||
}
|
||||
}
|
||||
if w.Bucket <= PollInterval {
|
||||
return filtered
|
||||
}
|
||||
out := make([]Sample, 0, len(filtered))
|
||||
for start := 0; start < len(filtered); {
|
||||
end := start + 1
|
||||
bucket := filtered[start].Time.UnixNano() / w.Bucket.Nanoseconds()
|
||||
for end < len(filtered) && filtered[end].Time.UnixNano()/w.Bucket.Nanoseconds() == bucket {
|
||||
end++
|
||||
}
|
||||
group := filtered[start:end]
|
||||
point := group[len(group)-1]
|
||||
avg := func(pick func(Sample) *float64) *float64 {
|
||||
sum := 0.0
|
||||
for _, p := range group {
|
||||
v := pick(p)
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
sum += *v
|
||||
}
|
||||
value := sum / float64(len(group))
|
||||
return &value
|
||||
}
|
||||
point.QueryRate = avg(func(p Sample) *float64 { return p.QueryRate })
|
||||
point.NoErrorRate = avg(func(p Sample) *float64 { return p.NoErrorRate })
|
||||
point.NXDomainRate = avg(func(p Sample) *float64 { return p.NXDomainRate })
|
||||
point.ServFailRate = avg(func(p Sample) *float64 { return p.ServFailRate })
|
||||
point.RefusedRate = avg(func(p Sample) *float64 { return p.RefusedRate })
|
||||
point.BlockedRate = avg(func(p Sample) *float64 { return p.BlockedRate })
|
||||
point.RateLimitedRate = avg(func(p Sample) *float64 { return p.RateLimitedRate })
|
||||
point.SourceAuthRate = avg(func(p Sample) *float64 { return p.SourceAuthRate })
|
||||
point.SourceRecursiveRate = avg(func(p Sample) *float64 { return p.SourceRecursiveRate })
|
||||
point.SourceForwardRate = avg(func(p Sample) *float64 { return p.SourceForwardRate })
|
||||
point.SourceCacheRate = avg(func(p Sample) *float64 { return p.SourceCacheRate })
|
||||
point.CacheHitsRate = avg(func(p Sample) *float64 { return p.CacheHitsRate })
|
||||
point.CacheMissesRate = avg(func(p Sample) *float64 { return p.CacheMissesRate })
|
||||
point.CacheEvictionsRate = avg(func(p Sample) *float64 { return p.CacheEvictionsRate })
|
||||
point.CacheHitRate = cachePercent(point.CacheHitsRate, point.CacheMissesRate)
|
||||
point.ErrorRate = errorPercent(point.QueryRate, point.ServFailRate, point.RefusedRate)
|
||||
for _, p := range group {
|
||||
if p.P50MS > point.P50MS {
|
||||
point.P50MS = p.P50MS
|
||||
}
|
||||
if p.P95MS > point.P95MS {
|
||||
point.P95MS = p.P95MS
|
||||
}
|
||||
if p.P99MS > point.P99MS {
|
||||
point.P99MS = p.P99MS
|
||||
}
|
||||
}
|
||||
out = append(out, point)
|
||||
start = end
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func WindowFleet(points []FleetPoint, w Window) []FleetPoint {
|
||||
filtered := make([]FleetPoint, 0, len(points))
|
||||
for _, p := range points {
|
||||
if !p.Time.Before(w.From) {
|
||||
filtered = append(filtered, p)
|
||||
}
|
||||
}
|
||||
if w.Bucket <= PollInterval {
|
||||
return filtered
|
||||
}
|
||||
out := make([]FleetPoint, 0, len(filtered))
|
||||
for start := 0; start < len(filtered); {
|
||||
end := start + 1
|
||||
b := filtered[start].Time.UnixNano() / w.Bucket.Nanoseconds()
|
||||
for end < len(filtered) && filtered[end].Time.UnixNano()/w.Bucket.Nanoseconds() == b {
|
||||
end++
|
||||
}
|
||||
group := filtered[start:end]
|
||||
p := group[len(group)-1]
|
||||
avg := func(pick func(FleetPoint) *float64) *float64 {
|
||||
sum := 0.0
|
||||
for _, q := range group {
|
||||
v := pick(q)
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
sum += *v
|
||||
}
|
||||
v := sum / float64(len(group))
|
||||
return &v
|
||||
}
|
||||
p.QueryRate = avg(func(s FleetPoint) *float64 { return s.QueryRate })
|
||||
p.NoErrorRate = avg(func(s FleetPoint) *float64 { return s.NoErrorRate })
|
||||
p.NXDomainRate = avg(func(s FleetPoint) *float64 { return s.NXDomainRate })
|
||||
p.ServFailRate = avg(func(s FleetPoint) *float64 { return s.ServFailRate })
|
||||
p.RefusedRate = avg(func(s FleetPoint) *float64 { return s.RefusedRate })
|
||||
p.BlockedRate = avg(func(s FleetPoint) *float64 { return s.BlockedRate })
|
||||
p.RateLimitedRate = avg(func(s FleetPoint) *float64 { return s.RateLimitedRate })
|
||||
p.SourceAuthRate = avg(func(s FleetPoint) *float64 { return s.SourceAuthRate })
|
||||
p.SourceRecursiveRate = avg(func(s FleetPoint) *float64 { return s.SourceRecursiveRate })
|
||||
p.SourceForwardRate = avg(func(s FleetPoint) *float64 { return s.SourceForwardRate })
|
||||
p.SourceCacheRate = avg(func(s FleetPoint) *float64 { return s.SourceCacheRate })
|
||||
p.CacheHitsRate = avg(func(s FleetPoint) *float64 { return s.CacheHitsRate })
|
||||
p.CacheMissesRate = avg(func(s FleetPoint) *float64 { return s.CacheMissesRate })
|
||||
p.CacheEvictionsRate = avg(func(s FleetPoint) *float64 { return s.CacheEvictionsRate })
|
||||
p.CacheEntries = avg(func(s FleetPoint) *float64 { return s.CacheEntries })
|
||||
p.CacheHitRate = cachePercent(p.CacheHitsRate, p.CacheMissesRate)
|
||||
p.ErrorRate = errorPercent(p.QueryRate, p.ServFailRate, p.RefusedRate)
|
||||
for _, q := range group {
|
||||
if q.Contributors < p.Contributors {
|
||||
p.Contributors = q.Contributors
|
||||
}
|
||||
}
|
||||
out = append(out, p)
|
||||
start = end
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
|
||||
)
|
||||
|
||||
func TestWindowParsingAndBoundedSampling(t *testing.T) {
|
||||
now := time.Date(2026, 11, 30, 18, 0, 0, 0, time.UTC)
|
||||
cases := []struct {
|
||||
span, step string
|
||||
seconds int
|
||||
}{
|
||||
{"15m", "auto", 15}, {"1h", "auto", 15}, {"6h", "auto", 60}, {"12h", "auto", 300}, {"24h", "auto", 300}, {"24h", "1h", 3600}, {"invalid", "invalid", 300},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
w := ParseWindow(tc.span, tc.step, now)
|
||||
if w.StepSeconds != tc.seconds || w.Span > 24*time.Hour || w.Span < 15*time.Minute || w.From.After(w.To) {
|
||||
t.Errorf("bad window %+v", w)
|
||||
}
|
||||
}
|
||||
// An old telemetry consumer without step= keeps the original measured rows.
|
||||
if v := ParseWindow("", "", now); v.StepSeconds != 15 {
|
||||
t.Fatalf("legacy endpoint changed resolution: %+v", v)
|
||||
}
|
||||
h := New()
|
||||
stats := easydns.Stats{Responses: map[string]uint64{}, Sources: map[string]uint64{}}
|
||||
for i := 0; i < maxSamples+12; i++ {
|
||||
stats.QueriesTotal += 15
|
||||
h.Record(now.Add(time.Duration(i)*PollInterval), stats)
|
||||
}
|
||||
samples, _, _ := h.Snapshot()
|
||||
if len(samples) != maxSamples {
|
||||
t.Fatalf("bounded history retained %d not %d", len(samples), maxSamples)
|
||||
}
|
||||
}
|
||||
func TestDownsampleOnlyMeasuredBucketsAndWeightedCache(t *testing.T) {
|
||||
h := New()
|
||||
start := time.Date(2026, 11, 30, 10, 0, 0, 0, time.UTC)
|
||||
mk := func(q, hits, miss uint64, p95 float64) easydns.Stats {
|
||||
return easydns.Stats{QueriesTotal: q, Responses: map[string]uint64{"noerror": q}, Sources: map[string]uint64{"recursive": q / 2}, Cache: easydns.Cache{Hits: hits, Misses: miss, Entries: 12}, LatencyP95S: p95}
|
||||
}
|
||||
h.Record(start.Add(-15*time.Second), mk(100, 0, 0, .005))
|
||||
h.Record(start, mk(115, 15, 0, .015)) // 1 QPS, cache 100%
|
||||
h.Record(start.Add(15*time.Second), mk(145, 15, 30, .030)) // 2 QPS, cache 0%
|
||||
h.Record(start.Add(61*time.Second), mk(177, 15, 62, .015)) // measured later than 30s
|
||||
w := ParseWindow("1h", "1m", start.Add(2*time.Minute))
|
||||
pts := WindowSamples(historySamples(h), w)
|
||||
if len(pts) != 3 {
|
||||
t.Fatalf("expected three observed buckets, got %d: %+v", len(pts), pts)
|
||||
}
|
||||
p := pts[1]
|
||||
if p.QueryRate == nil || math.Abs(*p.QueryRate-1.5) > 1e-8 {
|
||||
t.Fatalf("downsample must average actual rates, got %+v", p.QueryRate)
|
||||
}
|
||||
if p.CacheHitRate == nil || math.Abs(*p.CacheHitRate-100.0/3) > 1e-8 {
|
||||
t.Fatalf("cache bucket must use weighted hits/misses, got %+v", p.CacheHitRate)
|
||||
}
|
||||
if p.P95MS != 30 {
|
||||
t.Fatalf("peak p95 within bucket lost, got %v", p.P95MS)
|
||||
}
|
||||
if p.Time != start.Add(15*time.Second) {
|
||||
t.Fatalf("bucket fabricated a timestamp: %v", p.Time)
|
||||
}
|
||||
// A counter reset marks its bucket as missing, not negative or zero.
|
||||
h.Record(start.Add(2*time.Minute), mk(0, 0, 0, .005))
|
||||
pts = WindowSamples(historySamples(h), w)
|
||||
for _, pt := range pts {
|
||||
if pt.Time == start.Add(2*time.Minute) && pt.QueryRate != nil {
|
||||
t.Fatal("counter reset was turned into a rate")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestFleetDownsamplingPreservesIncompleteBuckets(t *testing.T) {
|
||||
t0 := time.Date(2026, 11, 30, 10, 0, 0, 0, time.UTC)
|
||||
x := 1.0
|
||||
points := []FleetPoint{{Time: t0, QueryRate: &x, Contributors: 2}, {Time: t0.Add(15 * time.Second), QueryRate: nil, Contributors: 1}}
|
||||
w := ParseWindow("1h", "1m", t0.Add(time.Minute))
|
||||
reduced := WindowFleet(points, w)
|
||||
if len(reduced) != 1 || reduced[0].QueryRate != nil || reduced[0].Contributors != 1 {
|
||||
t.Fatalf("missing node hidden by downsample: %+v", reduced)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user