diff --git a/statistics/metrics.go b/statistics/metrics.go new file mode 100644 index 000000000000..22a3d7c9641a --- /dev/null +++ b/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) +} diff --git a/statistics/update.go b/statistics/update.go index 71fde6533676..53606d8231d0 100644 --- a/statistics/update.go +++ b/statistics/update.go @@ -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 { @@ -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) +}