@@ -30,11 +30,13 @@ import (
30
30
"testing"
31
31
"time"
32
32
33
+ "github.com/pingcap/errors"
33
34
"github.com/pingcap/failpoint"
34
35
"github.com/pingcap/kvproto/pkg/metapb"
35
36
"github.com/pingcap/tidb/pkg/config"
36
37
"github.com/pingcap/tidb/pkg/domain"
37
38
"github.com/pingcap/tidb/pkg/extension"
39
+ "github.com/pingcap/tidb/pkg/metrics"
38
40
"github.com/pingcap/tidb/pkg/parser/auth"
39
41
"github.com/pingcap/tidb/pkg/parser/mysql"
40
42
"github.com/pingcap/tidb/pkg/server/internal"
@@ -54,6 +56,7 @@ import (
54
56
"github.com/pingcap/tidb/pkg/util/dbterror/exeerrors"
55
57
"github.com/pingcap/tidb/pkg/util/plancodec"
56
58
"github.com/pingcap/tidb/pkg/util/sqlkiller"
59
+ promtestutils "github.com/prometheus/client_golang/prometheus/testutil"
57
60
"github.com/stretchr/testify/require"
58
61
tikverr "github.com/tikv/client-go/v2/error"
59
62
"github.com/tikv/client-go/v2/testutils"
@@ -2085,3 +2088,40 @@ func TestCloseConn(t *testing.T) {
2085
2088
}
2086
2089
wg .Wait ()
2087
2090
}
2091
+
2092
+ func TestConnAddMetrics (t * testing.T ) {
2093
+ store := testkit .CreateMockStore (t )
2094
+ re := require .New (t )
2095
+ tk := testkit .NewTestKit (t , store )
2096
+ cc := & clientConn {
2097
+ alloc : arena .NewAllocator (1024 ),
2098
+ chunkAlloc : chunk .NewAllocator (),
2099
+ pkt : internal .NewPacketIOForTest (bufio .NewWriter (bytes .NewBuffer (nil ))),
2100
+ }
2101
+ tk .MustExec ("use test" )
2102
+ cc .SetCtx (& TiDBContext {Session : tk .Session (), stmts : make (map [int ]* TiDBStatement )})
2103
+
2104
+ // default
2105
+ cc .addMetrics (mysql .ComQuery , time .Now (), nil )
2106
+ counter := metrics .QueryTotalCounter
2107
+ v := promtestutils .ToFloat64 (counter .WithLabelValues ("Query" , "OK" , "default" ))
2108
+ require .Equal (t , 1.0 , v )
2109
+
2110
+ // rg1
2111
+ cc .getCtx ().GetSessionVars ().ResourceGroupName = "test_rg1"
2112
+ cc .addMetrics (mysql .ComQuery , time .Now (), nil )
2113
+ re .Equal (promtestutils .ToFloat64 (counter .WithLabelValues ("Query" , "OK" , "default" )), 1.0 )
2114
+ re .Equal (promtestutils .ToFloat64 (counter .WithLabelValues ("Query" , "OK" , "test_rg1" )), 1.0 )
2115
+ /// inc the counter again
2116
+ cc .addMetrics (mysql .ComQuery , time .Now (), nil )
2117
+ re .Equal (promtestutils .ToFloat64 (counter .WithLabelValues ("Query" , "OK" , "test_rg1" )), 2.0 )
2118
+
2119
+ // rg2
2120
+ cc .getCtx ().GetSessionVars ().ResourceGroupName = "test_rg2"
2121
+ // error
2122
+ cc .addMetrics (mysql .ComQuery , time .Now (), errors .New ("unknown error" ))
2123
+ re .Equal (promtestutils .ToFloat64 (counter .WithLabelValues ("Query" , "Error" , "test_rg2" )), 1.0 )
2124
+ // ok
2125
+ cc .addMetrics (mysql .ComStmtExecute , time .Now (), nil )
2126
+ re .Equal (promtestutils .ToFloat64 (counter .WithLabelValues ("StmtExecute" , "OK" , "test_rg2" )), 1.0 )
2127
+ }
0 commit comments