mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 18:43:38 +01:00
Distinguish a held post-plan slot from a blocked provider roster on a Prometheus scrape, without taking queue or state locks. Change-source: pulse-maintainer
162 lines
3.9 KiB
Go
162 lines
3.9 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
type pollWorkStage uint8
|
|
|
|
const (
|
|
pollWorkWaitingForSlot pollWorkStage = iota
|
|
pollWorkRunning
|
|
pollWorkStageCount
|
|
)
|
|
|
|
var pollWorkStageNames = [pollWorkStageCount]string{"waiting_for_slot", "running"}
|
|
|
|
type pollWorkDeadline struct {
|
|
stage pollWorkStage
|
|
deadline time.Time
|
|
}
|
|
|
|
// pollWorkProgress tracks active cycle phases and claimed tasks, never
|
|
// the scheduled inventory. Its lock is not held during queue, provider
|
|
// or state work. A scrape can measure work even when it never returns.
|
|
type pollWorkProgress struct {
|
|
overdue *prometheus.Desc
|
|
planningAge *prometheus.Desc
|
|
postPlanAge *prometheus.Desc
|
|
mu sync.Mutex
|
|
lastID uint64
|
|
active map[uint64]pollWorkDeadline
|
|
planning map[uint64]time.Time
|
|
postPlan map[uint64]time.Time
|
|
}
|
|
|
|
func newPollWorkProgress() *pollWorkProgress {
|
|
return &pollWorkProgress{
|
|
overdue: prometheus.NewDesc(
|
|
"pulse_scheduler_claimed_poll_overdue_seconds",
|
|
"Seconds past one poll task timeout for the most overdue claimed poll in each stage; zero if none.",
|
|
[]string{"stage"}, nil,
|
|
),
|
|
planningAge: prometheus.NewDesc(
|
|
"pulse_scheduler_poll_planning_age_seconds",
|
|
"Age in seconds of the oldest poll planning pass still running; zero if none.",
|
|
nil, nil,
|
|
),
|
|
postPlanAge: prometheus.NewDesc(
|
|
"pulse_scheduler_poll_post_plan_age_seconds",
|
|
"Age in seconds of the oldest poll cycle still in post-plan work (client count, statistics or alert state sync); zero if none.",
|
|
nil, nil,
|
|
),
|
|
active: make(map[uint64]pollWorkDeadline),
|
|
planning: make(map[uint64]time.Time),
|
|
postPlan: make(map[uint64]time.Time),
|
|
}
|
|
}
|
|
|
|
func (p *pollWorkProgress) beginPlanning(at time.Time) uint64 {
|
|
if p == nil {
|
|
return 0
|
|
}
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
p.lastID++
|
|
p.planning[p.lastID] = at
|
|
return p.lastID
|
|
}
|
|
|
|
func (p *pollWorkProgress) finishPlanning(id uint64) {
|
|
if p == nil {
|
|
return
|
|
}
|
|
p.mu.Lock()
|
|
delete(p.planning, id)
|
|
p.mu.Unlock()
|
|
}
|
|
|
|
func (p *pollWorkProgress) beginPostPlan(at time.Time) uint64 {
|
|
if p == nil {
|
|
return 0
|
|
}
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
p.lastID++
|
|
p.postPlan[p.lastID] = at
|
|
return p.lastID
|
|
}
|
|
|
|
func (p *pollWorkProgress) finishPostPlan(id uint64) {
|
|
if p == nil {
|
|
return
|
|
}
|
|
p.mu.Lock()
|
|
delete(p.postPlan, id)
|
|
p.mu.Unlock()
|
|
}
|
|
|
|
func (p *pollWorkProgress) begin(stage pollWorkStage, deadline time.Time) uint64 {
|
|
if p == nil {
|
|
return 0
|
|
}
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
p.lastID++
|
|
p.active[p.lastID] = pollWorkDeadline{stage: stage, deadline: deadline}
|
|
return p.lastID
|
|
}
|
|
|
|
func (p *pollWorkProgress) start(id uint64, deadline time.Time) {
|
|
if p == nil {
|
|
return
|
|
}
|
|
p.mu.Lock()
|
|
p.active[id] = pollWorkDeadline{stage: pollWorkRunning, deadline: deadline}
|
|
p.mu.Unlock()
|
|
}
|
|
|
|
func (p *pollWorkProgress) finish(id uint64) {
|
|
if p == nil {
|
|
return
|
|
}
|
|
p.mu.Lock()
|
|
delete(p.active, id)
|
|
p.mu.Unlock()
|
|
}
|
|
|
|
func (p *pollWorkProgress) Describe(ch chan<- *prometheus.Desc) {
|
|
ch <- p.overdue
|
|
ch <- p.planningAge
|
|
ch <- p.postPlanAge
|
|
}
|
|
|
|
func (p *pollWorkProgress) Collect(ch chan<- prometheus.Metric) {
|
|
var overdue [pollWorkStageCount]time.Duration
|
|
p.mu.Lock()
|
|
now := time.Now()
|
|
for _, work := range p.active {
|
|
overdue[work.stage] = max(overdue[work.stage], now.Sub(work.deadline))
|
|
}
|
|
planningAge := oldestPhaseAge(now, p.planning)
|
|
postPlanAge := oldestPhaseAge(now, p.postPlan)
|
|
p.mu.Unlock()
|
|
|
|
for stage, wait := range overdue {
|
|
ch <- prometheus.MustNewConstMetric(p.overdue, prometheus.GaugeValue, wait.Seconds(), pollWorkStageNames[stage])
|
|
}
|
|
ch <- prometheus.MustNewConstMetric(p.planningAge, prometheus.GaugeValue, planningAge.Seconds())
|
|
ch <- prometheus.MustNewConstMetric(p.postPlanAge, prometheus.GaugeValue, postPlanAge.Seconds())
|
|
}
|
|
|
|
func oldestPhaseAge(now time.Time, active map[uint64]time.Time) time.Duration {
|
|
var age time.Duration
|
|
for _, started := range active {
|
|
age = max(age, now.Sub(started))
|
|
}
|
|
return age
|
|
}
|