Skip to content

Commit ae42a7d

Browse files
authored
Merge pull request kubernetes-sigs#177 from steven-sheehy/scrape-on-startup
Scrape metrics on startup
2 parents b1ff6d5 + 669f4b4 commit ae42a7d

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

pkg/manager/manager.go

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -82,58 +82,61 @@ func (rm *Manager) RunUntil(stopCh <-chan struct{}) {
8282
go func() {
8383
ticker := time.NewTicker(rm.resolution)
8484
defer ticker.Stop()
85+
rm.Collect(time.Now())
8586

8687
for {
87-
func() {
88-
select {
88+
select {
8989
case startTime := <-ticker.C:
90-
rm.healthMu.Lock()
91-
rm.lastTickStart = startTime
92-
rm.healthMu.Unlock()
93-
94-
healthyTick := true
95-
96-
ctx, cancelTimeout := context.WithTimeout(context.Background(), rm.resolution)
97-
defer cancelTimeout()
98-
99-
glog.V(6).Infof("Beginning cycle, collecting metrics...")
100-
data, collectErr := rm.source.Collect(ctx)
101-
if collectErr != nil {
102-
glog.Errorf("unable to fully collect metrics: %v", collectErr)
103-
104-
// only consider this an indication of bad health if we
105-
// couldn't collect from any nodes -- one node going down
106-
// shouldn't indicate that metrics-server is unhealthy
107-
if len(data.Nodes) == 0 {
108-
healthyTick = false
109-
}
110-
111-
// NB: continue on so that we don't lose all metrics
112-
// if one node goes down
113-
}
114-
115-
glog.V(6).Infof("...Storing metrics...")
116-
recvErr := rm.sink.Receive(data)
117-
if recvErr != nil {
118-
glog.Errorf("unable to save metrics: %v", recvErr)
119-
120-
// any failure to save means we're unhealthy
121-
healthyTick = false
122-
}
123-
124-
collectTime := time.Now().Sub(startTime)
125-
tickDuration.Observe(float64(collectTime) / float64(time.Second))
126-
glog.V(6).Infof("...Cycle complete")
127-
128-
rm.healthMu.Lock()
129-
rm.lastOk = healthyTick
130-
rm.healthMu.Unlock()
90+
rm.Collect(startTime)
13191
case <-stopCh:
13292
return
133-
}
134-
}()
93+
}
94+
}
95+
}()
96+
}
97+
98+
func (rm *Manager) Collect(startTime time.Time) {
99+
rm.healthMu.Lock()
100+
rm.lastTickStart = startTime
101+
rm.healthMu.Unlock()
102+
103+
healthyTick := true
104+
105+
ctx, cancelTimeout := context.WithTimeout(context.Background(), rm.resolution)
106+
defer cancelTimeout()
107+
108+
glog.V(6).Infof("Beginning cycle, collecting metrics...")
109+
data, collectErr := rm.source.Collect(ctx)
110+
if collectErr != nil {
111+
glog.Errorf("unable to fully collect metrics: %v", collectErr)
112+
113+
// only consider this an indication of bad health if we
114+
// couldn't collect from any nodes -- one node going down
115+
// shouldn't indicate that metrics-server is unhealthy
116+
if len(data.Nodes) == 0 {
117+
healthyTick = false
135118
}
136-
}()
119+
120+
// NB: continue on so that we don't lose all metrics
121+
// if one node goes down
122+
}
123+
124+
glog.V(6).Infof("...Storing metrics...")
125+
recvErr := rm.sink.Receive(data)
126+
if recvErr != nil {
127+
glog.Errorf("unable to save metrics: %v", recvErr)
128+
129+
// any failure to save means we're unhealthy
130+
healthyTick = false
131+
}
132+
133+
collectTime := time.Now().Sub(startTime)
134+
tickDuration.Observe(float64(collectTime) / float64(time.Second))
135+
glog.V(6).Infof("...Cycle complete")
136+
137+
rm.healthMu.Lock()
138+
rm.lastOk = healthyTick
139+
rm.healthMu.Unlock()
137140
}
138141

139142
// CheckHealth checks the health of the manager by looking at tick times,

0 commit comments

Comments
 (0)