Skip to content

Commit

Permalink
stats: add metrics for auto analyze (#4793)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx committed Oct 17, 2017
1 parent e9949b4 commit 6e63f38
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
42 changes: 42 additions & 0 deletions statistics/metrics.go
@@ -0,0 +1,42 @@
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package statistics

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

var (
autoAnalyzeHistgram = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: "tidb",
Subsystem: "statistics",
Name: "auto_analyze_duration",
Help: "Bucketed histogram of processing time (s) of auto analyze.",
Buckets: prometheus.ExponentialBuckets(0.01, 2, 20),
})

autoAnalyzeCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "statistics",
Name: "auto_analyze_total",
Help: "Counter of auto analyze.",
}, []string{"type"})
)

func init() {
prometheus.MustRegister(autoAnalyzeHistgram)
prometheus.MustRegister(autoAnalyzeCounter)
}
18 changes: 14 additions & 4 deletions statistics/update.go
Expand Up @@ -181,8 +181,7 @@ func (h *Handle) HandleAutoAnalyze(is infoschema.InfoSchema) error {
if needAnalyzeTable(statsTbl, 20*h.Lease) {
sql := fmt.Sprintf("analyze table %s", tblName)
log.Infof("[stats] auto analyze table %s now", tblName)
_, _, err := h.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(h.ctx, sql)
return errors.Trace(err)
return errors.Trace(h.execAutoAnalyze(sql))
}
for _, idx := range tblInfo.Indices {
if idx.State != model.StatePublic {
Expand All @@ -191,11 +190,22 @@ func (h *Handle) HandleAutoAnalyze(is infoschema.InfoSchema) error {
if _, ok := statsTbl.Indices[idx.ID]; !ok {
sql := fmt.Sprintf("analyze table %s index `%s`", tblName, idx.Name.O)
log.Infof("[stats] auto analyze index `%s` for table %s now", idx.Name.O, tblName)
_, _, err := h.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(h.ctx, sql)
return errors.Trace(err)
return errors.Trace(h.execAutoAnalyze(sql))
}
}
}
}
return nil
}

func (h *Handle) execAutoAnalyze(sql string) error {
startTime := time.Now()
_, _, err := h.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(h.ctx, sql)
autoAnalyzeHistgram.Observe(time.Since(startTime).Seconds())
if err != nil {
autoAnalyzeCounter.WithLabelValues("failed").Inc()
} else {
autoAnalyzeCounter.WithLabelValues("succ").Inc()
}
return errors.Trace(err)
}

0 comments on commit 6e63f38

Please sign in to comment.