Skip to content

Commit

Permalink
feat: clear_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
wlx5575 committed Aug 7, 2024
1 parent 27f2d73 commit 7676a09
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
10 changes: 10 additions & 0 deletions go/stats/prometheusbackend/prometheusbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down
47 changes: 32 additions & 15 deletions go/vt/vtgate/scatter_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions go/vt/vtgate/vtgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7676a09

Please sign in to comment.