Skip to content

Commit 999fa02

Browse files
authored
server: Add metrics (pingcap#1729)
Add metrics for server package. Update gitcookie.sh.
1 parent b84aed4 commit 999fa02

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

gitcookie.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ chmod 0600 ~/.gitcookies
44
git config --global http.cookiefile ~/.gitcookies
55

66
tr , \\t <<\__END__ >>~/.gitcookies
7-
go.googlesource.com,FALSE,/,TRUE,2147483647,o,git-z.pingcap.com=1/Xv6CBlnVpdrhYBXT5i_VexGocQcbgkKsrW938zgjqx0
8-
go-review.googlesource.com,FALSE,/,TRUE,2147483647,o,git-z.pingcap.com=1/Xv6CBlnVpdrhYBXT5i_VexGocQcbgkKsrW938zgjqx0
7+
go.googlesource.com,FALSE,/,TRUE,2147483647,o,git-shenli.pingcap.com=1/rGvVlvFq_x9rxOmXqQe_rfcrjbOk6NSOHIQKhhsfidM
8+
go-review.googlesource.com,FALSE,/,TRUE,2147483647,o,git-shenli.pingcap.com=1/rGvVlvFq_x9rxOmXqQe_rfcrjbOk6NSOHIQKhhsfidM
99
__END__
10+

server/conn.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import (
5151
"github.com/pingcap/tidb/terror"
5252
"github.com/pingcap/tidb/util/arena"
5353
"github.com/pingcap/tidb/util/hack"
54-
"github.com/pingcap/tidb/util/metrics"
5554
)
5655

5756
var defaultCapability = mysql.ClientLongPassword | mysql.ClientLongFlag |
@@ -113,6 +112,7 @@ func (cc *clientConn) Close() error {
113112
delete(cc.server.clients, cc.connectionID)
114113
cc.server.rwlock.Unlock()
115114
cc.conn.Close()
115+
connGauge.Dec()
116116
if cc.ctx != nil {
117117
return cc.ctx.Close()
118118
}
@@ -305,6 +305,8 @@ func (cc *clientConn) Run() {
305305
cc.Close()
306306
}()
307307

308+
connGauge.Inc()
309+
308310
for {
309311
cc.alloc.Reset()
310312
data, err := cc.readPacket()
@@ -530,6 +532,16 @@ func (cc *clientConn) handleLoadData(loadDataInfo *executor.LoadDataInfo) error
530532

531533
func (cc *clientConn) handleQuery(sql string) (err error) {
532534
startTs := time.Now()
535+
defer func() {
536+
// Add metrics
537+
queryHistgram.Observe(time.Since(startTs).Seconds())
538+
label := querySucc
539+
if err != nil {
540+
label = queryFailed
541+
}
542+
queryCounter.WithLabelValues(label).Inc()
543+
}()
544+
533545
rs, err := cc.ctx.Execute(sql)
534546
if err != nil {
535547
return errors.Trace(err)
@@ -558,7 +570,6 @@ func (cc *clientConn) handleQuery(sql string) (err error) {
558570
} else {
559571
log.Warnf("[TIME_QUERY] %v %s", costTime, sql)
560572
}
561-
metrics.Query(costTime)
562573
return errors.Trace(err)
563574
}
564575

util/metrics/metrics.go server/metrics.go

+26-11
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,45 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package metrics
14+
package server
1515

1616
import (
17-
"time"
18-
1917
"github.com/prometheus/client_golang/prometheus"
2018
)
2119

2220
var (
23-
queryMetric = prometheus.NewHistogram(
21+
queryHistgram = prometheus.NewHistogram(
2422
prometheus.HistogramOpts{
2523
Namespace: "tidb",
26-
Subsystem: "query",
24+
Subsystem: "server",
2725
Name: "handle_query_duration_seconds",
2826
Help: "Bucketed histogram of processing time (s) of handled queries.",
2927
Buckets: prometheus.ExponentialBuckets(0.0005, 2, 13),
3028
})
31-
)
3229

33-
// Query is used for add query cost time into metrics.
34-
func Query(costTime time.Duration) {
35-
queryMetric.Observe(float64(costTime))
36-
}
30+
queryCounter = prometheus.NewCounterVec(
31+
prometheus.CounterOpts{
32+
Namespace: "tidb",
33+
Subsystem: "server",
34+
Name: "query_total",
35+
Help: "Counter of queries.",
36+
}, []string{"type"})
37+
38+
// Query handle result status.
39+
querySucc = "query_succ"
40+
queryFailed = "query_failed"
41+
42+
connGauge = prometheus.NewGauge(
43+
prometheus.GaugeOpts{
44+
Namespace: "tidb",
45+
Subsystem: "server",
46+
Name: "connections",
47+
Help: "Number of connections.",
48+
})
49+
)
3750

3851
func init() {
39-
prometheus.MustRegister(queryMetric)
52+
prometheus.MustRegister(queryHistgram)
53+
prometheus.MustRegister(queryCounter)
54+
prometheus.MustRegister(connGauge)
4055
}

server/server.go

-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ import (
4545
"github.com/pingcap/tidb/terror"
4646
"github.com/pingcap/tidb/util/arena"
4747
"github.com/pingcap/tidb/util/printer"
48-
// For prometheus init
49-
_ "github.com/pingcap/tidb/util/metrics"
5048
"github.com/prometheus/client_golang/prometheus"
5149
)
5250

0 commit comments

Comments
 (0)