Skip to content

Commit 0a52ff1

Browse files
authored
metricreader: fix that the metrics of CPU and health are not collected (#617)
1 parent 9a324b2 commit 0a52ff1

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

lib/util/logger/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ func CreateLoggerForTest(t *testing.T) (*zap.Logger, fmt.Stringer) {
3838
return zap.New(zapcore.NewCore(
3939
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
4040
zapcore.AddSync(log),
41-
zap.InfoLevel,
41+
zap.DebugLevel,
4242
)).Named(t.Name()), log
4343
}

pkg/balance/metricsreader/backend_reader.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ func (br *BackendReader) readFromBackends(ctx context.Context, excludeZones []st
283283
}
284284
resp, err := br.readBackendMetric(ctx, addr)
285285
if err != nil {
286-
br.lg.Error("read metrics from backend failed", zap.String("addr", addr), zap.Error(err))
286+
br.lg.Debug("read metrics from backend failed", zap.String("addr", addr), zap.Error(err))
287287
return
288288
}
289289
text := filterMetrics(hack.String(resp), allNames)
290290
mf, err := parseMetrics(text)
291291
if err != nil {
292-
br.lg.Error("parse metrics failed", zap.String("addr", addr), zap.Error(err))
292+
br.lg.Warn("parse metrics failed", zap.String("addr", addr), zap.Error(err))
293293
return
294294
}
295295
br.metric2History(mf, addr)
@@ -362,10 +362,9 @@ func (br *BackendReader) metric2History(mfs map[string]*dto.MetricFamily, backen
362362
// step 2: get the latest pair by the history and add it to step2History
363363
// E.g. calculate irate(process_cpu_seconds_total/tidb_server_maxprocs[30s])
364364
sampleValue = rule.Range2Value(beHistory.Step1History)
365-
if math.IsNaN(float64(sampleValue)) {
366-
continue
365+
if !math.IsNaN(float64(sampleValue)) {
366+
beHistory.Step2History = append(beHistory.Step2History, model.SamplePair{Timestamp: now, Value: sampleValue})
367367
}
368-
beHistory.Step2History = append(beHistory.Step2History, model.SamplePair{Timestamp: now, Value: sampleValue})
369368
ruleHistory[backend] = beHistory
370369
}
371370
}

pkg/balance/metricsreader/backend_reader_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ func TestOneRuleMultiHistory(t *testing.T) {
363363
lg, _ := logger.CreateLoggerForTest(t)
364364
br := NewBackendReader(lg, nil, nil, nil, nil, nil)
365365
var lastTs model.Time
366-
var length int
366+
var step1Len, step2Len int
367367
for i, test := range tests {
368368
br.queryRules = map[string]QueryRule{
369369
"key": {
@@ -379,17 +379,30 @@ func TestOneRuleMultiHistory(t *testing.T) {
379379
}
380380

381381
br.metric2History(mfs, "backend")
382+
history := br.history["key"]
383+
if !math.IsNaN(float64(test.step1Value)) {
384+
step1Len++
385+
}
386+
if step1Len > 0 {
387+
require.Equal(t, step1Len, len(history["backend"].Step1History), "case %d", i)
388+
}
389+
if !math.IsNaN(float64(test.step2Value)) {
390+
step2Len++
391+
}
392+
if step2Len > 0 {
393+
require.Equal(t, step2Len, len(history["backend"].Step2History), "case %d", i)
394+
}
395+
382396
res := br.history2Value("backend")
383397
value := res["key"]
384398
if math.IsNaN(float64(test.step2Value)) {
385399
continue
386400
}
387-
length++
388401
require.Equal(t, model.ValMatrix, value.Type(), "case %d", i)
389402
matrix := value.(model.Matrix)
390403
require.Len(t, matrix, 1, "case %d", i)
391404
require.Equal(t, model.LabelValue("backend"), matrix[0].Metric[LabelNameInstance], "case %d", i)
392-
require.Len(t, matrix[0].Values, length, "case %d", i)
405+
require.Len(t, matrix[0].Values, step2Len, "case %d", i)
393406
pair := matrix[0].Values[len(matrix[0].Values)-1]
394407
require.Equal(t, test.step2Value, pair.Value, "case %d", i)
395408
require.GreaterOrEqual(t, pair.Timestamp, lastTs, "case %d", i)

pkg/manager/vip/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (vm *vipManager) OnElected() {
8787
return
8888
}
8989
if hasIP {
90-
vm.lg.Info("already has VIP, do nothing")
90+
vm.lg.Debug("already has VIP, do nothing")
9191
return
9292
}
9393
if err := vm.operation.AddIP(); err != nil {
@@ -108,7 +108,7 @@ func (vm *vipManager) OnRetired() {
108108
return
109109
}
110110
if !hasIP {
111-
vm.lg.Info("does not have VIP, do nothing")
111+
vm.lg.Debug("does not have VIP, do nothing")
112112
return
113113
}
114114
if err := vm.operation.DeleteIP(); err != nil {

pkg/manager/vip/network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ func (no *networkOperation) initAddr(addressStr, linkStr string) error {
4343
}
4444
address, err := netlink.ParseAddr(addressStr)
4545
if err != nil {
46-
return errors.WithStack(err)
46+
return errors.Wrapf(errors.WithStack(err), "failed to parse address '%s'", addressStr)
4747
}
4848
no.address = address
4949
link, err := netlink.LinkByName(linkStr)
5050
if err != nil {
51-
return errors.WithStack(err)
51+
return errors.Wrapf(errors.WithStack(err), "failed to find network interface '%s'", linkStr)
5252
}
5353
no.link = link
5454
return nil

0 commit comments

Comments
 (0)