Skip to content

Commit

Permalink
server: Add metrics (pingcap#1729)
Browse files Browse the repository at this point in the history
Add metrics for server package.
Update gitcookie.sh.
  • Loading branch information
shenli authored Sep 17, 2016
1 parent b84aed4 commit 999fa02
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
5 changes: 3 additions & 2 deletions gitcookie.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ chmod 0600 ~/.gitcookies
git config --global http.cookiefile ~/.gitcookies

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

15 changes: 13 additions & 2 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import (
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/arena"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/metrics"
)

var defaultCapability = mysql.ClientLongPassword | mysql.ClientLongFlag |
Expand Down Expand Up @@ -113,6 +112,7 @@ func (cc *clientConn) Close() error {
delete(cc.server.clients, cc.connectionID)
cc.server.rwlock.Unlock()
cc.conn.Close()
connGauge.Dec()
if cc.ctx != nil {
return cc.ctx.Close()
}
Expand Down Expand Up @@ -305,6 +305,8 @@ func (cc *clientConn) Run() {
cc.Close()
}()

connGauge.Inc()

for {
cc.alloc.Reset()
data, err := cc.readPacket()
Expand Down Expand Up @@ -530,6 +532,16 @@ func (cc *clientConn) handleLoadData(loadDataInfo *executor.LoadDataInfo) error

func (cc *clientConn) handleQuery(sql string) (err error) {
startTs := time.Now()
defer func() {
// Add metrics
queryHistgram.Observe(time.Since(startTs).Seconds())
label := querySucc
if err != nil {
label = queryFailed
}
queryCounter.WithLabelValues(label).Inc()
}()

rs, err := cc.ctx.Execute(sql)
if err != nil {
return errors.Trace(err)
Expand Down Expand Up @@ -558,7 +570,6 @@ func (cc *clientConn) handleQuery(sql string) (err error) {
} else {
log.Warnf("[TIME_QUERY] %v %s", costTime, sql)
}
metrics.Query(costTime)
return errors.Trace(err)
}

Expand Down
37 changes: 26 additions & 11 deletions util/metrics/metrics.go → server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,45 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package metrics
package server

import (
"time"

"github.com/prometheus/client_golang/prometheus"
)

var (
queryMetric = prometheus.NewHistogram(
queryHistgram = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: "tidb",
Subsystem: "query",
Subsystem: "server",
Name: "handle_query_duration_seconds",
Help: "Bucketed histogram of processing time (s) of handled queries.",
Buckets: prometheus.ExponentialBuckets(0.0005, 2, 13),
})
)

// Query is used for add query cost time into metrics.
func Query(costTime time.Duration) {
queryMetric.Observe(float64(costTime))
}
queryCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "query_total",
Help: "Counter of queries.",
}, []string{"type"})

// Query handle result status.
querySucc = "query_succ"
queryFailed = "query_failed"

connGauge = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "connections",
Help: "Number of connections.",
})
)

func init() {
prometheus.MustRegister(queryMetric)
prometheus.MustRegister(queryHistgram)
prometheus.MustRegister(queryCounter)
prometheus.MustRegister(connGauge)
}
2 changes: 0 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ import (
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/arena"
"github.com/pingcap/tidb/util/printer"
// For prometheus init
_ "github.com/pingcap/tidb/util/metrics"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down

0 comments on commit 999fa02

Please sign in to comment.