Skip to content

Commit 98f32a2

Browse files
authored
metricreader: fix backend label value doesn't match Prometheus in k8s (#635)
1 parent cff6c4e commit 98f32a2

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

pkg/balance/metricsreader/backend_reader.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,12 @@ func (br *BackendReader) readFromBackends(ctx context.Context, excludeZones []st
272272
return nil
273273
}
274274

275+
backendLabels := make([]string, 0, len(addrs))
275276
for _, addr := range addrs {
276-
func(addr string) {
277+
backendLabels = append(backendLabels, getLabel4Addr(addr))
278+
}
279+
for i := range addrs {
280+
func(addr, label string) {
277281
br.wgp.RunWithRecover(func() {
278282
if ctx.Err() != nil {
279283
return
@@ -289,15 +293,15 @@ func (br *BackendReader) readFromBackends(ctx context.Context, excludeZones []st
289293
br.lg.Warn("parse metrics failed", zap.String("addr", addr), zap.Error(err))
290294
return
291295
}
292-
br.metric2History(mf, addr)
296+
br.metric2History(mf, label)
293297
}, nil, br.lg)
294-
}(addr)
298+
}(addrs[i], backendLabels[i])
295299
}
296300
br.wgp.Wait()
297301

298302
br.history2QueryResult()
299-
if err := br.marshalHistory(addrs); err != nil {
300-
br.lg.Error("marshal backend history failed", zap.Any("addrs", addrs), zap.Error(err))
303+
if err := br.marshalHistory(backendLabels); err != nil {
304+
br.lg.Error("marshal backend history failed", zap.Any("addrs", backendLabels), zap.Error(err))
301305
}
302306
return nil
303307
}

pkg/balance/metricsreader/query_result.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,13 @@ func getLabel4Backend(backend policy.BackendCtx) string {
125125
backendInfo := backend.GetBackendInfo()
126126
return net.JoinHostPort(backendInfo.IP, strconv.Itoa(int(backendInfo.StatusPort)))
127127
}
128+
129+
// addr is the address of the backend status port.
130+
func getLabel4Addr(addr string) string {
131+
if strings.Contains(addr, ".svc:") {
132+
// In operator deployment, the label value of `instance` is the pod name.
133+
return addr[:strings.Index(addr, ".")]
134+
}
135+
// In tiup deployment, the label value of `instance` is hostname:statusPort.
136+
return addr
137+
}

pkg/balance/metricsreader/query_result_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,23 @@ func TestVectorMatchLabel(t *testing.T) {
174174
require.Equal(t, test.expectedSample.Value, sample.Value, "test index %d", i)
175175
}
176176
}
177+
178+
func TestAddrMatchLabel(t *testing.T) {
179+
tests := []struct {
180+
addr string
181+
label string
182+
}{
183+
{
184+
addr: "10.10.11.1:3080",
185+
label: "10.10.11.1:3080",
186+
},
187+
{
188+
addr: "tc-tidb-0.tc-tidb-peer.ns.svc:3080",
189+
label: "tc-tidb-0",
190+
},
191+
}
192+
193+
for i, test := range tests {
194+
require.Equal(t, test.label, getLabel4Addr(test.addr), "test index %d", i)
195+
}
196+
}

0 commit comments

Comments
 (0)