Skip to content

Commit

Permalink
metrics: add more metrics and fix some metrics (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Apr 6, 2023
1 parent e85dc2a commit 28ce2c6
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 52 deletions.
6 changes: 4 additions & 2 deletions pkg/manager/cert/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/TiProxy/lib/util/errors"
"github.com/pingcap/TiProxy/lib/util/security"
"github.com/pingcap/TiProxy/lib/util/waitgroup"
"github.com/pingcap/TiProxy/pkg/metrics"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -134,8 +135,9 @@ func (cm *CertManager) reload() {
if err := cm.sqlTLS.Reload(cm.logger); err != nil {
errs = append(errs, err)
}
err := errors.Collect(errors.New("loading certs"), errs...)
if err != nil {
if len(errs) > 0 {
metrics.ServerErrCounter.WithLabelValues("load_cert").Add(float64(len(errs)))
err := errors.Collect(errors.New("loading certs"), errs...)
cm.logger.Error("failed to reload some certs", zap.Error(err))
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/manager/router/backend_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ func (bo *BackendObserver) checkHealth(ctx context.Context, backends map[string]

// Also dial the SQL port just in case that the SQL port hangs.
err := connectWithRetry(func() error {
startTime := time.Now()
conn, err := net.DialTimeout("tcp", addr, bo.healthCheckConfig.DialTimeout)
setPingBackendMetrics(addr, err == nil, startTime)
if err == nil {
if err := conn.Close(); err != nil && !pnet.IsDisconnectError(err) {
bo.logger.Error("close connection in health check failed", zap.Error(err))
Expand Down
8 changes: 7 additions & 1 deletion pkg/manager/router/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ func addMigrateMetrics(from, to string, succeed bool, startTime time.Time) {
metrics.MigrateCounter.WithLabelValues(from, to, resLabel).Inc()

cost := time.Since(startTime)
metrics.MigrateDurationHistogram.WithLabelValues(from, to, resLabel).Observe(float64(cost.Milliseconds()))
metrics.MigrateDurationHistogram.WithLabelValues(from, to, resLabel).Observe(cost.Seconds())
}

func readMigrateCounter(from, to string, succeed bool) (int, error) {
return metrics.ReadCounter(metrics.MigrateCounter.WithLabelValues(from, to, succeedToLabel(succeed)))
}

func setPingBackendMetrics(addr string, succeed bool, startTime time.Time) {
cost := time.Since(startTime)
resLabel := succeedToLabel(succeed)
metrics.PingBackendGauge.WithLabelValues(addr, resLabel).Set(cost.Seconds())
}
57 changes: 57 additions & 0 deletions pkg/metrics/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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/prometheus/client_golang/prometheus"

const (
LblRes = "res"
LblStatus = "status"
)

var (
BackendStatusGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: ModuleProxy,
Subsystem: LabelBackend,
Name: "b_status",
Help: "Gauge of backend status.",
}, []string{LblBackend, LblStatus})

GetBackendHistogram = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: ModuleProxy,
Subsystem: LabelBackend,
Name: "get_backend_duration_seconds",
Help: "Bucketed histogram of time (s) for getting an available backend.",
Buckets: prometheus.ExponentialBuckets(0.000001, 2, 26), // 1us ~ 30s
})

GetBackendCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: ModuleProxy,
Subsystem: LabelBackend,
Name: "get_backend",
Help: "Counter of getting backend.",
}, []string{LblRes})

PingBackendGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: ModuleProxy,
Subsystem: LabelBackend,
Name: "ping_duration_seconds",
Help: "Time (s) of pinging the SQL port of each backend.",
}, []string{LblBackend, LblRes})
)
15 changes: 3 additions & 12 deletions pkg/metrics/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,10 @@ const (
LblBackend = "backend"
LblFrom = "from"
LblTo = "to"
LblStatus = "status"
LblMigrateResult = "migrate_res"
)

var (
BackendStatusGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: ModuleProxy,
Subsystem: LabelBalance,
Name: "b_status",
Help: "Gauge of backend status.",
}, []string{LblBackend, LblStatus})

BackendConnGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: ModuleProxy,
Expand All @@ -57,8 +48,8 @@ var (
prometheus.HistogramOpts{
Namespace: ModuleProxy,
Subsystem: LabelBalance,
Name: "migrate_duration_millis",
Help: "Bucketed histogram of migrating time (ms) of sessions.",
Buckets: prometheus.ExponentialBuckets(0.1, 2, 26), // 0.1ms ~ 1h
Name: "migrate_duration_seconds",
Help: "Bucketed histogram of migrating time (s) of sessions.",
Buckets: prometheus.ExponentialBuckets(0.0001, 2, 26), // 0.1ms ~ 1h
}, []string{LblFrom, LblTo, LblMigrateResult})
)
Loading

0 comments on commit 28ce2c6

Please sign in to comment.