Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: add metrics for batch client #996

Merged
merged 3 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions internal/client/client_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type batchCommandsEntry struct {
// canceled indicated the request is canceled or not.
canceled int32
err error
start time.Time
}

func (b *batchCommandsEntry) isCanceled() bool {
Expand Down Expand Up @@ -378,11 +379,14 @@ func (a *batchConn) getClientAndSend() {
}
defer cli.unlockForSend()

now := time.Now()
tiKVBatchWaitToSendDuration := metrics.TiKVBatchWaitDuration.WithLabelValues("wait-to-send", target)
req, forwardingReqs := a.reqBuilder.build(func(id uint64, e *batchCommandsEntry) {
cli.batched.Store(id, e)
if trace.IsEnabled() {
trace.Log(e.ctx, "rpc", "send")
}
tiKVBatchWaitToSendDuration.Observe(float64(now.Sub(e.start)))
})
if req != nil {
cli.send("", req)
Expand Down Expand Up @@ -507,6 +511,14 @@ func (c *batchCommandsClient) isStopped() bool {
}

func (c *batchCommandsClient) send(forwardedHost string, req *tikvpb.BatchCommandsRequest) {
start := time.Now()
defer func() {
if forwardedHost == "" {
metrics.TiKVBatchConnSendDuration.WithLabelValues(c.target).Observe(time.Since(start).Seconds())
} else {
metrics.TiKVBatchConnSendDuration.WithLabelValues(forwardedHost).Observe(time.Since(start).Seconds())
}
}()
err := c.initBatchClient(forwardedHost)
if err != nil {
logutil.BgLogger().Warn(
Expand Down Expand Up @@ -773,18 +785,19 @@ func sendBatchRequest(
req *tikvpb.BatchCommandsRequest_Request,
timeout time.Duration,
) (*tikvrpc.Response, error) {
start := time.Now()
entry := &batchCommandsEntry{
ctx: ctx,
req: req,
res: make(chan *tikvpb.BatchCommandsResponse_Response, 1),
forwardedHost: forwardedHost,
canceled: 0,
err: nil,
start: start,
}
timer := time.NewTimer(timeout)
defer timer.Stop()

start := time.Now()
select {
case batchConn.batchCommandsCh <- entry:
case <-ctx.Done():
Expand All @@ -795,7 +808,7 @@ func sendBatchRequest(
return nil, errors.WithMessage(context.DeadlineExceeded, "wait sendLoop")
}
waitDuration := time.Since(start)
metrics.TiKVBatchWaitDuration.Observe(float64(waitDuration))
metrics.TiKVBatchWaitDuration.WithLabelValues("wait-to-chan", addr).Observe(float64(waitDuration))

select {
case res, ok := <-entry.res:
Expand Down
22 changes: 17 additions & 5 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ var (
TiKVLocalLatchWaitTimeHistogram prometheus.Histogram
TiKVStatusDuration *prometheus.HistogramVec
TiKVStatusCounter *prometheus.CounterVec
TiKVBatchWaitDuration prometheus.Histogram
TiKVBatchWaitDuration *prometheus.HistogramVec
TiKVBatchConnSendDuration *prometheus.HistogramVec
TiKVBatchSendLatency prometheus.Histogram
TiKVBatchWaitOverLoad prometheus.Counter
TiKVBatchPendingRequests *prometheus.HistogramVec
Expand Down Expand Up @@ -333,15 +334,25 @@ func initMetrics(namespace, subsystem string, constLabels prometheus.Labels) {
ConstLabels: constLabels,
}, []string{LblResult})

TiKVBatchWaitDuration = prometheus.NewHistogram(
TiKVBatchWaitDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "batch_wait_duration",
Buckets: prometheus.ExponentialBuckets(1, 2, 34), // 1ns ~ 8s
Help: "batch wait duration",
Buckets: prometheus.ExponentialBuckets(64, 2, 34), // 64ns ~ 549s
Help: "batch-cmd wait duration, unit is nanosecond",
ConstLabels: constLabels,
})
}, []string{LblType, LblStore})

TiKVBatchConnSendDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "batch_conn_send_seconds",
Buckets: prometheus.ExponentialBuckets(0.0005, 2, 22), // 0.5ms ~ 1048s
Help: "batch conn send duration",
ConstLabels: constLabels,
}, []string{LblStore})

TiKVBatchSendLatency = prometheus.NewHistogram(
prometheus.HistogramOpts{
Expand Down Expand Up @@ -768,6 +779,7 @@ func RegisterMetrics() {
prometheus.MustRegister(TiKVStatusDuration)
prometheus.MustRegister(TiKVStatusCounter)
prometheus.MustRegister(TiKVBatchWaitDuration)
prometheus.MustRegister(TiKVBatchConnSendDuration)
prometheus.MustRegister(TiKVBatchSendLatency)
prometheus.MustRegister(TiKVBatchRecvLatency)
prometheus.MustRegister(TiKVBatchWaitOverLoad)
Expand Down
Loading