180 lines
6.6 KiB
Go
180 lines
6.6 KiB
Go
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
|
|
}
|