Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: attach const label keyspace_id #41693

Merged
merged 6 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions br/cmd/br/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ go_library(
"//session",
"//util",
"//util/logutil",
"//util/metricsutil",
"@com_github_gogo_protobuf//proto",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_kvproto//pkg/brpb",
Expand Down
5 changes: 5 additions & 0 deletions br/cmd/br/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pingcap/tidb/br/pkg/version/build"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/util/metricsutil"
"github.com/spf13/cobra"
"go.uber.org/zap"
"sourcegraph.com/sourcegraph/appdash"
Expand All @@ -25,6 +26,10 @@ func runBackupCommand(command *cobra.Command, cmdName string) error {
return errors.Trace(err)
}

if err := metricsutil.RegisterMetricsForBR(cfg.PD, cfg.KeyspaceName); err != nil {
return errors.Trace(err)
}

ctx := GetDefaultContext()
if cfg.EnableOpenTracing {
var store *appdash.MemoryStore
Expand Down
5 changes: 5 additions & 0 deletions br/cmd/br/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/br/pkg/version/build"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/util/metricsutil"
"github.com/spf13/cobra"
"go.uber.org/zap"
"sourcegraph.com/sourcegraph/appdash"
Expand All @@ -27,6 +28,10 @@ func runRestoreCommand(command *cobra.Command, cmdName string) error {
return errors.Trace(err)
}

if err := metricsutil.RegisterMetricsForBR(cfg.PD, cfg.KeyspaceName); err != nil {
return errors.Trace(err)
}

if task.IsStreamRestore(cmdName) {
if err := cfg.ParseStreamRestoreFlags(command.Flags()); err != nil {
return errors.Trace(err)
Expand Down
1 change: 1 addition & 0 deletions domain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ go_library(
"//ddl/util",
"//domain/globalconfigsync",
"//domain/infosync",
"//domain/metrics",
"//errno",
"//infoschema",
"//infoschema/perfschema",
Expand Down
11 changes: 3 additions & 8 deletions domain/historical_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@ package domain
import (
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/metrics"
domain_metrics "github.com/pingcap/tidb/domain/metrics"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)

var (
generateHistoricalStatsSuccessCounter = metrics.HistoricalStatsCounter.WithLabelValues("generate", "success")
generateHistoricalStatsFailedCounter = metrics.HistoricalStatsCounter.WithLabelValues("generate", "fail")
)

// HistoricalStatsWorker indicates for dump historical stats
type HistoricalStatsWorker struct {
tblCH chan int64
Expand Down Expand Up @@ -85,10 +80,10 @@ func (w *HistoricalStatsWorker) DumpHistoricalStats(tableID int64, statsHandle *
return errors.Errorf("cannot get DBInfo by TableID %d", tableID)
}
if _, err := statsHandle.RecordHistoricalStatsToStorage(dbInfo.Name.O, tblInfo, tableID, isPartition); err != nil {
generateHistoricalStatsFailedCounter.Inc()
domain_metrics.GenerateHistoricalStatsFailedCounter.Inc()
return errors.Errorf("record table %s.%s's historical stats failed, err:%v", dbInfo.Name.O, tblInfo.Name.O, err)
}
generateHistoricalStatsSuccessCounter.Inc()
domain_metrics.GenerateHistoricalStatsSuccessCounter.Inc()
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions domain/metrics/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "metrics",
srcs = ["metrics.go"],
importpath = "github.com/pingcap/tidb/domain/metrics",
visibility = ["//visibility:public"],
deps = [
"//metrics",
"@com_github_prometheus_client_golang//prometheus",
],
)
52 changes: 52 additions & 0 deletions domain/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metrics

import (
"github.com/pingcap/tidb/metrics"
"github.com/prometheus/client_golang/prometheus"
)

// domain metrics vars
var (
GenerateHistoricalStatsSuccessCounter prometheus.Counter
GenerateHistoricalStatsFailedCounter prometheus.Counter

PlanReplayerDumpTaskSuccess prometheus.Counter
PlanReplayerDumpTaskFailed prometheus.Counter

PlanReplayerCaptureTaskSendCounter prometheus.Counter
PlanReplayerCaptureTaskDiscardCounter prometheus.Counter

PlanReplayerRegisterTaskGauge prometheus.Gauge
)

func init() {
InitMetricsVars()
}

// InitMetricsVars init domain metrics vars.
func InitMetricsVars() {
GenerateHistoricalStatsSuccessCounter = metrics.HistoricalStatsCounter.WithLabelValues("generate", "success")
GenerateHistoricalStatsFailedCounter = metrics.HistoricalStatsCounter.WithLabelValues("generate", "fail")

PlanReplayerDumpTaskSuccess = metrics.PlanReplayerTaskCounter.WithLabelValues("dump", "success")
PlanReplayerDumpTaskFailed = metrics.PlanReplayerTaskCounter.WithLabelValues("dump", "fail")

PlanReplayerCaptureTaskSendCounter = metrics.PlanReplayerTaskCounter.WithLabelValues("capture", "send")
PlanReplayerCaptureTaskDiscardCounter = metrics.PlanReplayerTaskCounter.WithLabelValues("capture", "discard")

PlanReplayerRegisterTaskGauge = metrics.PlanReplayerRegisterTaskGauge
}
14 changes: 4 additions & 10 deletions domain/plan_replayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/tidb/bindinfo"
"github.com/pingcap/tidb/domain/infosync"
domain_metrics "github.com/pingcap/tidb/domain/metrics"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser"
Expand Down Expand Up @@ -179,13 +180,6 @@ func insertPlanReplayerSuccessStatusRecord(ctx context.Context, sctx sessionctx.
}
}

var (
planReplayerCaptureTaskSendCounter = metrics.PlanReplayerTaskCounter.WithLabelValues("capture", "send")
planReplayerCaptureTaskDiscardCounter = metrics.PlanReplayerTaskCounter.WithLabelValues("capture", "discard")

planReplayerRegisterTaskGauge = metrics.PlanReplayerRegisterTaskGauge
)

type planReplayerHandle struct {
*planReplayerTaskCollectorHandle
*planReplayerTaskDumpHandle
Expand All @@ -200,10 +194,10 @@ func (h *planReplayerHandle) SendTask(task *PlanReplayerDumpTask) bool {
if !task.IsContinuesCapture {
h.planReplayerTaskCollectorHandle.removeTask(task.PlanReplayerTaskKey)
}
planReplayerCaptureTaskSendCounter.Inc()
domain_metrics.PlanReplayerCaptureTaskSendCounter.Inc()
return true
default:
planReplayerCaptureTaskDiscardCounter.Inc()
domain_metrics.PlanReplayerCaptureTaskDiscardCounter.Inc()
// directly discard the task if the task channel is full in order not to block the query process
logutil.BgLogger().Warn("discard one plan replayer dump task",
zap.String("sql-digest", task.SQLDigest), zap.String("plan-digest", task.PlanDigest))
Expand Down Expand Up @@ -241,7 +235,7 @@ func (h *planReplayerTaskCollectorHandle) CollectPlanReplayerTask() error {
}
}
h.setupTasks(tasks)
planReplayerRegisterTaskGauge.Set(float64(len(tasks)))
domain_metrics.PlanReplayerRegisterTaskGauge.Set(float64(len(tasks)))
return nil
}

Expand Down
11 changes: 3 additions & 8 deletions domain/plan_replayer_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/bindinfo"
"github.com/pingcap/tidb/config"
domain_metrics "github.com/pingcap/tidb/domain/metrics"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
Expand Down Expand Up @@ -151,11 +151,6 @@ func (tne *tableNameExtractor) handleIsView(t *ast.TableName) (bool, error) {
return true, nil
}

var (
planReplayerDumpTaskSuccess = metrics.PlanReplayerTaskCounter.WithLabelValues("dump", "success")
planReplayerDumpTaskFailed = metrics.PlanReplayerTaskCounter.WithLabelValues("dump", "fail")
)

// DumpPlanReplayerInfo will dump the information about sqls.
// The files will be organized into the following format:
/*
Expand Down Expand Up @@ -224,9 +219,9 @@ func DumpPlanReplayerInfo(ctx context.Context, sctx sessionctx.Context,
zap.Strings("sqls", sqls))
}
errMsg = err.Error()
planReplayerDumpTaskFailed.Inc()
domain_metrics.PlanReplayerDumpTaskFailed.Inc()
} else {
planReplayerDumpTaskSuccess.Inc()
domain_metrics.PlanReplayerDumpTaskSuccess.Inc()
}
err1 := zw.Close()
if err1 != nil {
Expand Down
1 change: 1 addition & 0 deletions executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ go_library(
"//domain/infosync",
"//errno",
"//executor/aggfuncs",
"//executor/metrics",
"//expression",
"//expression/aggregation",
"//infoschema",
Expand Down
Loading