Skip to content

Commit

Permalink
metrics: add EMA cpu usage (#39995)
Browse files Browse the repository at this point in the history
close #40029
  • Loading branch information
hawkingrei authored Dec 19, 2022
1 parent 48585a7 commit 08f6ea4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions metrics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"meta.go",
"metrics.go",
"owner.go",
"resourcemanager.go",
"server.go",
"session.go",
"sli.go",
Expand Down
2 changes: 2 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ func RegisterMetrics() {
prometheus.MustRegister(TTLJobStatus)
prometheus.MustRegister(TTLPhaseTime)

prometheus.MustRegister(EMACPUUsageGauge)

tikvmetrics.InitMetrics(TiDB, TiKVClient)
tikvmetrics.RegisterMetrics()
tikvmetrics.TiKVPanicCounter = PanicCounter // reset tidb metrics for tikv metrics
Expand Down
27 changes: 27 additions & 0 deletions metrics/resourcemanager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022 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/prometheus/client_golang/prometheus"

var (
// EMACPUUsageGauge means exponential moving average of CPU usage
EMACPUUsageGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "tidb",
Subsystem: "rm",
Name: "ema_cpu_usage",
Help: "exponential moving average of CPU usage",
})
)
2 changes: 1 addition & 1 deletion resourcemanager/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func (r *ResourceManager) Start() {
// Stop is to stop resource manager
func (r *ResourceManager) Stop() {
r.cpuObserver.Stop()
r.wg.Done()
r.wg.Wait()
}
1 change: 1 addition & 0 deletions util/cpu/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
importpath = "github.com/pingcap/tidb/util/cpu",
visibility = ["//visibility:public"],
deps = [
"//metrics",
"//util/cgroup",
"//util/mathutil",
"@com_github_cloudfoundry_gosigar//:gosigar",
Expand Down
2 changes: 2 additions & 0 deletions util/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/cloudfoundry/gosigar"
"github.com/pingcap/log"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/util/cgroup"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/atomic"
Expand Down Expand Up @@ -66,6 +67,7 @@ func (c *Observer) Start() {
curr := c.observe()
c.cpu.Add(curr)
cpuUsage.Store(c.cpu.Get())
metrics.EMACPUUsageGauge.Set(c.cpu.Get())
case <-c.exit:
return
}
Expand Down

0 comments on commit 08f6ea4

Please sign in to comment.