Skip to content

Commit 390b8ab

Browse files
authored
metrics, vip: add metric to show the VIP owner (#594)
1 parent be81d8a commit 390b8ab

File tree

6 files changed

+145
-23
lines changed

6 files changed

+145
-23
lines changed

pkg/manager/vip/manager.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/pingcap/tiproxy/lib/config"
1010
"github.com/pingcap/tiproxy/pkg/manager/elect"
11+
"github.com/pingcap/tiproxy/pkg/metrics"
1112
clientv3 "go.etcd.io/etcd/client/v3"
1213
"go.uber.org/zap"
1314
)
@@ -83,6 +84,7 @@ func (vm *vipManager) Start(ctx context.Context, etcdCli *clientv3.Client) error
8384
}
8485

8586
func (vm *vipManager) OnElected() {
87+
metrics.VIPGauge.Set(1)
8688
hasIP, err := vm.operation.HasIP()
8789
if err != nil {
8890
vm.lg.Error("checking addresses failed", zap.Error(err))
@@ -104,6 +106,7 @@ func (vm *vipManager) OnElected() {
104106
}
105107

106108
func (vm *vipManager) OnRetired() {
109+
metrics.VIPGauge.Set(0)
107110
hasIP, err := vm.operation.HasIP()
108111
if err != nil {
109112
vm.lg.Error("checking addresses failed", zap.Error(err))

pkg/manager/vip/manager_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/pingcap/tiproxy/lib/config"
1313
"github.com/pingcap/tiproxy/lib/util/logger"
14+
"github.com/pingcap/tiproxy/pkg/metrics"
1415
"github.com/stretchr/testify/require"
1516
)
1617

@@ -138,6 +139,14 @@ func TestNetworkOperation(t *testing.T) {
138139
return strings.Contains(text.String()[logIdx:], test.expectedLog)
139140
}, 3*time.Second, 10*time.Millisecond, "case %d", i)
140141
logIdx = len(text.String())
142+
143+
expectedVIPGauge := 0
144+
if test.eventType == eventTypeElected {
145+
expectedVIPGauge = 1
146+
}
147+
vipGauge, err := metrics.ReadGauge(metrics.VIPGauge)
148+
require.NoError(t, err)
149+
require.EqualValues(t, expectedVIPGauge, vipGauge, "case %d", i)
141150
}
142151
cancel()
143152
vm.Close()

pkg/metrics/grafana/tiproxy_summary.json

Lines changed: 109 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,92 @@
660660
"show": true
661661
}
662662
]
663+
},
664+
{
665+
"aliasColors": { },
666+
"bars": false,
667+
"dashLength": 10,
668+
"dashes": false,
669+
"datasource": "${DS_TEST-CLUSTER}",
670+
"description": "1 indicates the VIP owner.",
671+
"fill": 1,
672+
"fillGradient": 0,
673+
"gridPos": {
674+
"h": 6,
675+
"w": 12,
676+
"x": 12,
677+
"y": 0
678+
},
679+
"id": 10,
680+
"legend": {
681+
"alignAsTable": false,
682+
"avg": false,
683+
"current": false,
684+
"max": false,
685+
"min": false,
686+
"rightSide": true,
687+
"show": true,
688+
"sideWidth": null,
689+
"total": false,
690+
"values": false
691+
},
692+
"lines": true,
693+
"linewidth": 1,
694+
"links": [ ],
695+
"nullPointMode": "null",
696+
"percentage": false,
697+
"pointradius": 5,
698+
"points": false,
699+
"renderer": "flot",
700+
"repeat": null,
701+
"seriesOverrides": [ ],
702+
"spaceLength": 10,
703+
"stack": false,
704+
"steppedLine": false,
705+
"targets": [
706+
{
707+
"expr": "tiproxy_server_vip{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\", instance=~\"$instance\"}",
708+
"format": "time_series",
709+
"intervalFactor": 2,
710+
"legendFormat": "{{instance}}",
711+
"refId": "A"
712+
}
713+
],
714+
"thresholds": [ ],
715+
"timeFrom": null,
716+
"timeShift": null,
717+
"title": "VIP Owner",
718+
"tooltip": {
719+
"shared": true,
720+
"sort": 0,
721+
"value_type": "individual"
722+
},
723+
"type": "graph",
724+
"xaxis": {
725+
"buckets": null,
726+
"mode": "time",
727+
"name": null,
728+
"show": true,
729+
"values": [ ]
730+
},
731+
"yaxes": [
732+
{
733+
"format": "short",
734+
"label": null,
735+
"logBase": 1,
736+
"max": null,
737+
"min": null,
738+
"show": true
739+
},
740+
{
741+
"format": "short",
742+
"label": null,
743+
"logBase": 1,
744+
"max": null,
745+
"min": null,
746+
"show": true
747+
}
748+
]
663749
}
664750
],
665751
"repeat": null,
@@ -679,7 +765,7 @@
679765
"x": 0,
680766
"y": 0
681767
},
682-
"id": 10,
768+
"id": 11,
683769
"panels": [
684770
{
685771
"aliasColors": { },
@@ -696,7 +782,7 @@
696782
"x": 0,
697783
"y": 0
698784
},
699-
"id": 11,
785+
"id": 12,
700786
"legend": {
701787
"alignAsTable": false,
702788
"avg": false,
@@ -796,7 +882,7 @@
796882
"x": 12,
797883
"y": 0
798884
},
799-
"id": 12,
885+
"id": 13,
800886
"legend": {
801887
"alignAsTable": false,
802888
"avg": false,
@@ -882,7 +968,7 @@
882968
"x": 0,
883969
"y": 0
884970
},
885-
"id": 13,
971+
"id": 14,
886972
"legend": {
887973
"alignAsTable": false,
888974
"avg": false,
@@ -968,7 +1054,7 @@
9681054
"x": 12,
9691055
"y": 0
9701056
},
971-
"id": 14,
1057+
"id": 15,
9721058
"legend": {
9731059
"alignAsTable": false,
9741060
"avg": false,
@@ -1054,7 +1140,7 @@
10541140
"x": 0,
10551141
"y": 0
10561142
},
1057-
"id": 15,
1143+
"id": 16,
10581144
"legend": {
10591145
"alignAsTable": false,
10601146
"avg": false,
@@ -1140,7 +1226,7 @@
11401226
"x": 12,
11411227
"y": 0
11421228
},
1143-
"id": 16,
1229+
"id": 17,
11441230
"legend": {
11451231
"alignAsTable": false,
11461232
"avg": false,
@@ -1226,7 +1312,7 @@
12261312
"x": 0,
12271313
"y": 0
12281314
},
1229-
"id": 17,
1315+
"id": 18,
12301316
"legend": {
12311317
"alignAsTable": false,
12321318
"avg": false,
@@ -1329,7 +1415,7 @@
13291415
"x": 0,
13301416
"y": 0
13311417
},
1332-
"id": 18,
1418+
"id": 19,
13331419
"panels": [
13341420
{
13351421
"aliasColors": { },
@@ -1346,7 +1432,7 @@
13461432
"x": 0,
13471433
"y": 0
13481434
},
1349-
"id": 19,
1435+
"id": 20,
13501436
"legend": {
13511437
"alignAsTable": false,
13521438
"avg": false,
@@ -1432,7 +1518,7 @@
14321518
"x": 12,
14331519
"y": 0
14341520
},
1435-
"id": 20,
1521+
"id": 21,
14361522
"legend": {
14371523
"alignAsTable": false,
14381524
"avg": false,
@@ -1518,7 +1604,7 @@
15181604
"x": 0,
15191605
"y": 0
15201606
},
1521-
"id": 21,
1607+
"id": 22,
15221608
"legend": {
15231609
"alignAsTable": false,
15241610
"avg": false,
@@ -1618,7 +1704,7 @@
16181704
"x": 12,
16191705
"y": 0
16201706
},
1621-
"id": 22,
1707+
"id": 23,
16221708
"legend": {
16231709
"alignAsTable": false,
16241710
"avg": false,
@@ -1707,7 +1793,7 @@
17071793
"x": 0,
17081794
"y": 0
17091795
},
1710-
"id": 23,
1796+
"id": 24,
17111797
"panels": [
17121798
{
17131799
"aliasColors": { },
@@ -1724,7 +1810,7 @@
17241810
"x": 0,
17251811
"y": 0
17261812
},
1727-
"id": 24,
1813+
"id": 25,
17281814
"legend": {
17291815
"alignAsTable": false,
17301816
"avg": false,
@@ -1824,7 +1910,7 @@
18241910
"x": 12,
18251911
"y": 0
18261912
},
1827-
"id": 25,
1913+
"id": 26,
18281914
"legend": {
18291915
"alignAsTable": false,
18301916
"avg": false,
@@ -1910,7 +1996,7 @@
19101996
"x": 0,
19111997
"y": 0
19121998
},
1913-
"id": 26,
1999+
"id": 27,
19142000
"legend": {
19152001
"alignAsTable": false,
19162002
"avg": false,
@@ -1999,7 +2085,7 @@
19992085
"x": 0,
20002086
"y": 0
20012087
},
2002-
"id": 27,
2088+
"id": 28,
20032089
"panels": [
20042090
{
20052091
"aliasColors": { },
@@ -2016,7 +2102,7 @@
20162102
"x": 0,
20172103
"y": 0
20182104
},
2019-
"id": 28,
2105+
"id": 29,
20202106
"legend": {
20212107
"alignAsTable": false,
20222108
"avg": false,
@@ -2102,7 +2188,7 @@
21022188
"x": 12,
21032189
"y": 0
21042190
},
2105-
"id": 29,
2191+
"id": 30,
21062192
"legend": {
21072193
"alignAsTable": false,
21082194
"avg": false,
@@ -2188,7 +2274,7 @@
21882274
"x": 0,
21892275
"y": 0
21902276
},
2191-
"id": 30,
2277+
"id": 31,
21922278
"legend": {
21932279
"alignAsTable": false,
21942280
"avg": false,
@@ -2274,7 +2360,7 @@
22742360
"x": 12,
22752361
"y": 0
22762362
},
2277-
"id": 31,
2363+
"id": 32,
22782364
"legend": {
22792365
"alignAsTable": false,
22802366
"avg": false,
@@ -2360,7 +2446,7 @@
23602446
"x": 0,
23612447
"y": 0
23622448
},
2363-
"id": 32,
2449+
"id": 33,
23642450
"legend": {
23652451
"alignAsTable": false,
23662452
"avg": false,

pkg/metrics/grafana/tiproxy_summary.jsonnet

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,20 @@ local uptimeP = graphPanel.new(
209209
)
210210
);
211211

212+
local vipP = graphPanel.new(
213+
title='VIP Owner',
214+
datasource=myDS,
215+
legend_rightSide=true,
216+
description='1 indicates the VIP owner.',
217+
format='short',
218+
)
219+
.addTarget(
220+
prometheus.target(
221+
'tiproxy_server_vip{k8s_cluster="$k8s_cluster", tidb_cluster="$tidb_cluster", instance=~"$instance"}',
222+
legendFormat='{{instance}}',
223+
)
224+
);
225+
212226
// Query Summary
213227
local queryRow = row.new(collapse=true, title='Query Summary');
214228
local durationP = graphPanel.new(
@@ -565,6 +579,7 @@ newDash
565579
.addPanel(createConnP, gridPos=leftPanelPos)
566580
.addPanel(disconnP, gridPos=rightPanelPos)
567581
.addPanel(goroutineP, gridPos=leftPanelPos)
582+
.addPanel(vipP, gridPos=rightPanelPos)
568583
,
569584
gridPos=rowPos
570585
)

pkg/metrics/metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func init() {
9999
CreateConnCounter,
100100
DisConnCounter,
101101
MaxProcsGauge,
102+
VIPGauge,
102103
ServerEventCounter,
103104
ServerErrCounter,
104105
TimeJumpBackCounter,

pkg/metrics/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ var (
4242
Help: "Number of disconnections.",
4343
}, []string{LblType})
4444

45+
VIPGauge = prometheus.NewGauge(
46+
prometheus.GaugeOpts{
47+
Namespace: ModuleProxy,
48+
Subsystem: LabelServer,
49+
Name: "vip",
50+
Help: "VIP owner.",
51+
})
52+
4553
MaxProcsGauge = prometheus.NewGauge(
4654
prometheus.GaugeOpts{
4755
Namespace: ModuleProxy,

0 commit comments

Comments
 (0)