Files
EasyDNS-Web/internal/telemetry/fleet.go
T
Mateusz Gruszczyński a28362b82c
Go quality gates / test (push) Waiting to run
v0.1.2
2026-12-01 00:16:15 +01:00

147 lines
5.6 KiB
Go

package telemetry
import (
"time"
)
// FleetNode is an authenticated panel-only view of measured samples. It
// contains no credentials or server configuration mutation capabilities.
// Each node owns a separate history; no synthetic data is produced.
type FleetNode struct {
ID string `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
Samples []Sample `json:"samples"`
Error string `json:"error,omitempty"`
LastPoll time.Time `json:"lastPoll"`
}
// FleetPoint contains only quantities for which across-node arithmetic is
// 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"`
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
const fleetMaxAge = 23 * time.Second
// AggregateFleet produces timestamp-aligned, fully-covered sum-of-rates
// points for all registered servers. Each node contributes its latest real
// reading at or before the UTC grid timestamp, no older than 23 seconds.
// Missing/reset/stale samples produce a gap (null), never a misleading 0 or a
// reduced total disguised as a whole-fleet total. Cache ratio is weighted by
// cache hit/miss operations per second, not averaged from percentages.
func AggregateFleet(nodes []FleetNode) []FleetPoint {
if len(nodes) == 0 {
return nil
}
var earliest, latest time.Time
for _, n := range nodes {
for _, p := range n.Samples {
if earliest.IsZero() || p.Time.Before(earliest) {
earliest = p.Time
}
if latest.IsZero() || p.Time.After(latest) {
latest = p.Time
}
}
}
if earliest.IsZero() {
return nil
}
start := earliest.Truncate(fleetStep).Add(fleetStep)
end := latest.Truncate(fleetStep).Add(fleetStep)
if end.Before(start) {
return nil
}
// 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)
}
points := make([]FleetPoint, 0, int(end.Sub(start)/fleetStep)+1)
index := make([]int, len(nodes))
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, 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]++
}
if index[i] == 0 {
cacheComplete = false
continue
}
sample := n.Samples[index[i]-1]
if age := t.Sub(sample.Time); age < 0 || age > fleetMaxAge || sample.QueryRate == nil || sample.NoErrorRate == nil || sample.NXDomainRate == nil || sample.ServFailRate == nil || sample.RefusedRate == nil || sample.BlockedRate == nil {
cacheComplete = false
continue
}
point.Contributors++
query += *sample.QueryRate
noerror += *sample.NoErrorRate
nxdomain += *sample.NXDomainRate
servfail += *sample.ServFailRate
refused += *sample.RefusedRate
blocked += *sample.BlockedRate
if sample.CacheHitsRate == nil || sample.CacheMissesRate == nil {
cacheComplete = false
} else {
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 {
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)
}
return points
}