From 7676a0968ccc4a24e316852ad9f278743d89092a Mon Sep 17 00:00:00 2001 From: wangleixin1 Date: Wed, 7 Aug 2024 20:01:43 +0800 Subject: [PATCH 1/3] 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. From f8c43823369061b380df6fdc8791d3b5b7440387 Mon Sep 17 00:00:00 2001 From: wangleixin1 Date: Thu, 8 Aug 2024 10:45:18 +0800 Subject: [PATCH 2/3] modify api place --- .../prometheusbackend/prometheusbackend.go | 13 +------ go/vt/vtgate/scatter_conn.go | 34 +++++++++++-------- go/vt/vtgate/vtgate.go | 9 +++++ 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/go/stats/prometheusbackend/prometheusbackend.go b/go/stats/prometheusbackend/prometheusbackend.go index cadb86c3f2b..a874b786bda 100644 --- a/go/stats/prometheusbackend/prometheusbackend.go +++ b/go/stats/prometheusbackend/prometheusbackend.go @@ -18,12 +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" + "strings" "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/log" @@ -43,16 +40,8 @@ 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 697348c3dd5..3be0f608ed2 100644 --- a/go/vt/vtgate/scatter_conn.go +++ b/go/vt/vtgate/scatter_conn.go @@ -86,21 +86,7 @@ var ( // NewScatterConn creates a new ScatterConn. func NewScatterConn(statsName string, txConn *TxConn, gw *TabletGateway) *ScatterConn { // this only works with TabletGateway - 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 { - + if statsName == "VttabletCall" { return &ScatterConn{ timings: vttabletTimings, tabletCallErrorCount: tabletCallErrorCount, @@ -108,6 +94,24 @@ func NewScatterConn(statsName string, txConn *TxConn, gw *TabletGateway) *Scatte gateway: gw, } } + + 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, + } + } func (stc *ScatterConn) startAction(name string, target *querypb.Target) (time.Time, []string) { diff --git a/go/vt/vtgate/vtgate.go b/go/vt/vtgate/vtgate.go index a38393ef8fb..b6ba241ec3c 100644 --- a/go/vt/vtgate/vtgate.go +++ b/go/vt/vtgate/vtgate.go @@ -394,6 +394,7 @@ func Init( }) vtgateInst.registerDebugHealthHandler() vtgateInst.registerDebugEnvHandler() + vtgateInst.registerClearMetrics() initAPI(gw.hc) return vtgateInst @@ -458,6 +459,14 @@ func (vtg *VTGate) registerDebugHealthHandler() { }) } +func (vtg *VTGate) registerClearMetrics() { + servenv.HTTPHandleFunc("/clear/metrics", func(w http.ResponseWriter, r *http.Request) { + ClearMetrics() + log.Infof("vtgate metrics have been cleared.") + w.Write([]byte("vtgate metrics have been cleared.")) + }) +} + // IsHealthy returns nil if server is healthy. // Otherwise, it returns an error indicating the reason. func (vtg *VTGate) IsHealthy() error { From d5459a70f252dc5072957ad4d4331e9fd1f7410e Mon Sep 17 00:00:00 2001 From: wangleixin1 Date: Thu, 8 Aug 2024 12:21:31 +0800 Subject: [PATCH 3/3] format --- go/stats/prometheusbackend/prometheusbackend.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/go/stats/prometheusbackend/prometheusbackend.go b/go/stats/prometheusbackend/prometheusbackend.go index a874b786bda..62165e117c1 100644 --- a/go/stats/prometheusbackend/prometheusbackend.go +++ b/go/stats/prometheusbackend/prometheusbackend.go @@ -18,9 +18,10 @@ package prometheusbackend import ( "expvar" + "strings" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" - "strings" "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/log"