v0.1.1
Go quality gates / test (push) Waiting to run

This commit is contained in:
Mateusz Gruszczyński committed 2026-11-30 14:44:28 +01:00
1 parent 8c4d463f1e
commit de13d851a8
75 files changed
+4656 -442

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
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"`
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 six-hour, 1440-poll 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 float64
cacheComplete := 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.cacheHitQPS == nil || sample.cacheMissQPS == nil {
cacheComplete = false
} else {
hitQPS += *sample.cacheHitQPS
missQPS += *sample.cacheMissQPS
}
}
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
}
}
points = append(points, point)
}
return points
}