Skip to content

Commit 606669f

Browse files
metricreader: fix TiProxy fails to query from Prometheus in operator deployment (#835) (#836)
Co-authored-by: djshow832 <[email protected]>
1 parent 8e2a106 commit 606669f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

pkg/balance/metricsreader/query_result.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (qr QueryResult) GetSample4Backend(backend policy.BackendCtx) *model.Sample
117117

118118
func getLabel4Backend(backend policy.BackendCtx) string {
119119
addr := backend.Addr()
120-
if strings.Contains(addr, ".svc:") {
120+
if isOperatorDeployed(addr) {
121121
// In operator deployment, the label value of `instance` is the pod name.
122122
return addr[:strings.Index(addr, ".")]
123123
}
@@ -128,10 +128,24 @@ func getLabel4Backend(backend policy.BackendCtx) string {
128128

129129
// addr is the address of the backend status port.
130130
func getLabel4Addr(addr string) string {
131-
if strings.Contains(addr, ".svc:") {
131+
if isOperatorDeployed(addr) {
132132
// In operator deployment, the label value of `instance` is the pod name.
133133
return addr[:strings.Index(addr, ".")]
134134
}
135135
// In tiup deployment, the label value of `instance` is hostname:statusPort.
136136
return addr
137137
}
138+
139+
func isOperatorDeployed(addr string) bool {
140+
// (.+-tidb-[0-9]+).*peer.*.svc.*")
141+
idx := strings.Index(addr, "-tidb-")
142+
if idx < 0 {
143+
return false
144+
}
145+
idx = strings.Index(addr[idx:], "peer")
146+
if idx < 0 {
147+
return false
148+
}
149+
idx = strings.Index(addr[idx:], ".svc")
150+
return idx >= 0
151+
}

pkg/balance/metricsreader/query_result_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ func TestMatrixMatchLabel(t *testing.T) {
8484
port: 10080,
8585
expectedPairs: []model.SamplePair{{Timestamp: 1712700000000, Value: 100}},
8686
},
87+
{
88+
jsonRes: `[{"metric":{"__name__":"process_cpu_seconds_total","instance":"tc-tidb-0","job":"tidb"},"values":[[1712700000,"100"]]}, {"metric":{"__name__":"process_cpu_seconds_total","instance":"tc-tidb-1","job":"tidb"},"values":[[1712700000,"200"]]}]`,
89+
addr: "tc-tidb-0.tc-tidb-peer.ns.svc.cluster.local:4000",
90+
ip: "tc-tidb-0.tc-tidb-peer.ns.svc.cluster.local",
91+
port: 10080,
92+
expectedPairs: []model.SamplePair{{Timestamp: 1712700000000, Value: 100}},
93+
},
8794
{
8895
jsonRes: `[]`,
8996
addr: "tc-tidb-0.tc-tidb-peer.ns.svc:4000",
@@ -148,6 +155,13 @@ func TestVectorMatchLabel(t *testing.T) {
148155
port: 10080,
149156
expectedSample: &model.Sample{Timestamp: 1712700000000, Value: 100},
150157
},
158+
{
159+
jsonRes: `[{"metric":{"__name__":"process_cpu_seconds_total","instance":"tc-tidb-0","job":"tidb"},"value":[1712700000,"100"]}, {"metric":{"__name__":"process_cpu_seconds_total","instance":"tc-tidb-1","job":"tidb"},"value":[1712700000,"200"]}]`,
160+
addr: "tc-tidb-0.tc-tidb-peer.ns.svc.cluster.local:4000",
161+
ip: "tc-tidb-0.tc-tidb-peer.ns.svc.cluster.local",
162+
port: 10080,
163+
expectedSample: &model.Sample{Timestamp: 1712700000000, Value: 100},
164+
},
151165
{
152166
jsonRes: `[]`,
153167
addr: "tc-tidb-0.tc-tidb-peer.ns.svc:4000",
@@ -188,6 +202,10 @@ func TestAddrMatchLabel(t *testing.T) {
188202
addr: "tc-tidb-0.tc-tidb-peer.ns.svc:3080",
189203
label: "tc-tidb-0",
190204
},
205+
{
206+
addr: "tc-tidb-0.tc-tidb-peer.ns.svc.cluster.local:3080",
207+
label: "tc-tidb-0",
208+
},
191209
}
192210

193211
for i, test := range tests {

0 commit comments

Comments
 (0)