From 7676a0968ccc4a24e316852ad9f278743d89092a Mon Sep 17 00:00:00 2001 From: wangleixin1 Date: Wed, 7 Aug 2024 20:01:43 +0800 Subject: [PATCH] feat: clear_metrics --- .../prometheusbackend/prometheusbackend.go | 10 ++++ go/vt/vtgate/scatter_conn.go | 47 +++++++++++++------ go/vt/vtgate/vtgate.go | 17 +++++++ 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/go/stats/prometheusbackend/prometheusbackend.go b/go/stats/prometheusbackend/prometheusbackend.go index 62165e117c1..cadb86c3f2b 100644 --- a/go/stats/prometheusbackend/prometheusbackend.go +++ b/go/stats/prometheusbackend/prometheusbackend.go @@ -18,7 +18,9 @@ package prometheusbackend import ( "expvar" + "net/http" "strings" + "vitess.io/vitess/go/vt/vtgate" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -41,8 +43,16 @@ var ( func Init(namespace string) { servenv.HTTPHandle("/metrics", promhttp.Handler()) be.namespace = namespace + servenv.HTTPHandleFunc("/clear/metrics", ClearMetricsHandler) + stats.Register(be.publishPrometheusMetric) } +func ClearMetricsHandler(w http.ResponseWriter, r *http.Request) { + + vtgate.ClearMetrics() + log.Infof("All vtgate Prometheus metrics have been cleared.") + w.Write([]byte("All vtgate Prometheus metrics have been cleared.")) +} // publishPrometheusMetric is used to publish the metric to Prometheus. func (be PromBackend) publishPrometheusMetric(name string, v expvar.Var) { diff --git a/go/vt/vtgate/scatter_conn.go b/go/vt/vtgate/scatter_conn.go index 8b571f7b67d..697348c3dd5 100644 --- a/go/vt/vtgate/scatter_conn.go +++ b/go/vt/vtgate/scatter_conn.go @@ -72,24 +72,41 @@ type shardActionFunc func(rs *srvtopo.ResolvedShard, i int) error // the results and errors for the caller. type shardActionTransactionFunc func(rs *srvtopo.ResolvedShard, i int, shardActionInfo *shardActionInfo) (*shardActionInfo, error) +var ( + vttabletTimings = stats.NewMultiTimings( + "VttabletCall", + "Scatter connection timings", + []string{"Operation", "Keyspace", "ShardName", "DbType"}) + tabletCallErrorCount = stats.NewCountersWithMultiLabels( + "VttabletCallErrorCount", + "Error count from tablet calls in scatter conns", + []string{"Operation", "Keyspace", "ShardName", "DbType"}) +) + // NewScatterConn creates a new ScatterConn. func NewScatterConn(statsName string, txConn *TxConn, gw *TabletGateway) *ScatterConn { // this only works with TabletGateway - tabletCallErrorCountStatsName := "" - if statsName != "" { - tabletCallErrorCountStatsName = statsName + "ErrorCount" - } - return &ScatterConn{ - timings: stats.NewMultiTimings( - statsName, - "Scatter connection timings", - []string{"Operation", "Keyspace", "ShardName", "DbType"}), - tabletCallErrorCount: stats.NewCountersWithMultiLabels( - tabletCallErrorCountStatsName, - "Error count from tablet calls in scatter conns", - []string{"Operation", "Keyspace", "ShardName", "DbType"}), - txConn: txConn, - gateway: gw, + if statsName == "" { + return &ScatterConn{ + timings: stats.NewMultiTimings( + statsName, + "Scatter connection timings", + []string{"Operation", "Keyspace", "ShardName", "DbType"}), + tabletCallErrorCount: stats.NewCountersWithMultiLabels( + "", + "Error count from tablet calls in scatter conns", + []string{"Operation", "Keyspace", "ShardName", "DbType"}), + txConn: txConn, + gateway: gw, + } + } else { + + return &ScatterConn{ + timings: vttabletTimings, + tabletCallErrorCount: tabletCallErrorCount, + txConn: txConn, + gateway: gw, + } } } diff --git a/go/vt/vtgate/vtgate.go b/go/vt/vtgate/vtgate.go index 7d28c0e9697..a38393ef8fb 100644 --- a/go/vt/vtgate/vtgate.go +++ b/go/vt/vtgate/vtgate.go @@ -216,6 +216,23 @@ var ( []string{"Operation", "Keyspace", "DbType"}) ) +func ClearMetrics() { + vschemaCounters.ResetAll() + errorCounts.ResetAll() + warnings.ResetAll() + vstreamSkewDelayCount.Reset() + vindexUnknownParams.Reset() + timings.Reset() + rowsReturned.ResetAll() + rowsAffected.ResetAll() + queriesProcessed.ResetAll() + queriesRouted.ResetAll() + queriesProcessedByTable.ResetAll() + queriesRoutedByTable.ResetAll() + vttabletTimings.Reset() + tabletCallErrorCount.ResetAll() +} + // VTGate is the rpc interface to vtgate. Only one instance // can be created. It implements vtgateservice.VTGateService // VTGate exposes multiple generations of interfaces.