Skip to content

resourcecontrol: propagate request source for RU metrics#1929

Open
YuhaoZhang00 wants to merge 1 commit into
tikv:masterfrom
YuhaoZhang00:rc/resource-control-metrics
Open

resourcecontrol: propagate request source for RU metrics#1929
YuhaoZhang00 wants to merge 1 commit into
tikv:masterfrom
YuhaoZhang00:rc/resource-control-metrics

Conversation

@YuhaoZhang00

@YuhaoZhang00 YuhaoZhang00 commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Ref tikv/pd#10634

Related PR: tikv/pd#10588

What problem does this PR solve?

pd/client records RU consumption inside its resource-group controller, but client-go's resourcecontrol.RequestInfo does not currently propagate the TiKV request source. Without that value, the per-request-source RU metric introduced by tikv/pd#10588 cannot attribute RRU/WRU to user queries, DDL, stats, and other internal activities.

What is changed and how does it work?

  • Store tikvrpc.Request.GetRequestSource() in client-go's resourcecontrol.RequestInfo for both read and write requests.
  • Add RequestSource() so RequestInfo implements the optional request-source provider recognized by pd/client.
  • Cover request-source propagation for read requests, write requests, and requests without a source.

This PR only propagates request metadata. It does not change resource-control bypass behavior, define client-go RU counters, or duplicate RU accounting in the client-go interceptor. The metric itself is owned and recorded by pd/client in tikv/pd#10588.

The metric becomes effective after client-go consumes a pd/client version containing tikv/pd#10588.

Tests

go test ./internal/resourcecontrol -count=1

Release note

None

@ti-chi-bot ti-chi-bot Bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. dco-signoff: yes Indicates the PR's author has signed the dco. labels Mar 30, 2026
@coderabbitai

coderabbitai Bot commented Mar 30, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

RequestInfo now preserves TiKV request source values through MakeRequestInfo and exposes them with RequestSource(). Tests cover read, prewrite, and commit request behavior.

Changes

Request source propagation

Layer / File(s) Summary
Store, expose, and validate request sources
internal/resourcecontrol/resource_control.go, internal/resourcecontrol/resource_control_test.go
RequestInfo stores request source values, MakeRequestInfo populates them for read and write requests, and tests verify populated and empty sources.с

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested labels: contribution

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: propagating request source to support RU metrics.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ti-chi-bot ti-chi-bot Bot added contribution This PR is from a community contributor. first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. labels Mar 30, 2026
@ti-chi-bot

ti-chi-bot Bot commented Mar 30, 2026

Copy link
Copy Markdown

Welcome @YuhaoZhang00!

It looks like this is your first PR to tikv/client-go 🎉.

I'm the bot to help you request reviewers, add labels and more, See available commands.

We want to make sure your contribution gets all the attention it needs!



Thank you, and welcome to tikv/client-go. 😃

@ti-chi-bot ti-chi-bot Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 30, 2026
@YuhaoZhang00 YuhaoZhang00 changed the title resource_control: add Prometheus metric for RU consumption by source resource_control: add Prometheus metrics for RU consumption and bypassed requests Mar 30, 2026
@YuhaoZhang00
YuhaoZhang00 marked this pull request as ready for review March 31, 2026 03:00
@ti-chi-bot ti-chi-bot Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 31, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@metrics/metrics.go`:
- Around line 985-1001: The counters TiKVResourceControlRUCounter and
TiKVResourceControlBypassedCounter currently hardcode Subsystem:
"resource_control" which yields wrong metric names; update their CounterOpts to
use Subsystem: subsystem and move "resource_control" into the Name field so the
metrics become tikv_client_go_resource_control_ru_total and
tikv_client_go_resource_control_bypassed_request_total (i.e., set Name for
TiKVResourceControlRUCounter to "resource_control_ru_total" and for
TiKVResourceControlBypassedCounter to
"resource_control_bypassed_request_total"), leaving the rest (Help, ConstLabels,
label names LblResourceGroup/LblSource/LblType) unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bd655b94-3baa-438a-a3ab-39011ae52513

📥 Commits

Reviewing files that changed from the base of the PR and between 282ada6 and 7c31694.

📒 Files selected for processing (2)
  • internal/client/client_interceptor.go
  • metrics/metrics.go

Comment thread metrics/metrics.go Outdated
Comment on lines +985 to +1001
TiKVResourceControlRUCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "resource_control",
Name: "ru_total",
Help: "Counter of resource units consumed by requests going through resource control.",
ConstLabels: constLabels,
}, []string{LblResourceGroup, LblSource, LblType})

TiKVResourceControlBypassedCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "resource_control",
Name: "bypassed_request_total",
Help: "Counter of requests that bypassed resource control.",
ConstLabels: constLabels,
}, []string{LblResourceGroup, LblSource})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Expect: default metrics use namespace=tikv and subsystem=client_go.
rg -n 'initMetrics\("tikv",\s*"client_go",\s*nil\)' metrics/metrics.go

# Expect: the new counters override that subsystem with "resource_control".
rg -n 'TiKVResourceControl(RU|Bypassed)Counter|Subsystem:\s+"resource_control"|Name:\s+"(ru_total|bypassed_request_total)"' metrics/metrics.go

Repository: tikv/client-go

Length of output: 665


Change Subsystem and Name in these CounterOpts to match the tikv_client_go_* naming pattern.

With the default initMetrics("tikv", "client_go", nil), hardcoding Subsystem: "resource_control" exports tikv_resource_control_{ru,bypassed_request}_total instead of the expected tikv_client_go_resource_control_* naming. This breaks consistency with other metrics. Use Subsystem: subsystem and move resource_control into Name.

🔧 Proposed fix
  TiKVResourceControlRUCounter = prometheus.NewCounterVec(
      prometheus.CounterOpts{
          Namespace:   namespace,
-         Subsystem:   "resource_control",
-         Name:        "ru_total",
+         Subsystem:   subsystem,
+         Name:        "resource_control_ru_total",
          Help:        "Counter of resource units consumed by requests going through resource control.",
          ConstLabels: constLabels,
      }, []string{LblResourceGroup, LblSource, LblType})

  TiKVResourceControlBypassedCounter = prometheus.NewCounterVec(
      prometheus.CounterOpts{
          Namespace:   namespace,
-         Subsystem:   "resource_control",
-         Name:        "bypassed_request_total",
+         Subsystem:   subsystem,
+         Name:        "resource_control_bypassed_request_total",
          Help:        "Counter of requests that bypassed resource control.",
          ConstLabels: constLabels,
      }, []string{LblResourceGroup, LblSource})
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
TiKVResourceControlRUCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "resource_control",
Name: "ru_total",
Help: "Counter of resource units consumed by requests going through resource control.",
ConstLabels: constLabels,
}, []string{LblResourceGroup, LblSource, LblType})
TiKVResourceControlBypassedCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "resource_control",
Name: "bypassed_request_total",
Help: "Counter of requests that bypassed resource control.",
ConstLabels: constLabels,
}, []string{LblResourceGroup, LblSource})
TiKVResourceControlRUCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "resource_control_ru_total",
Help: "Counter of resource units consumed by requests going through resource control.",
ConstLabels: constLabels,
}, []string{LblResourceGroup, LblSource, LblType})
TiKVResourceControlBypassedCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "resource_control_bypassed_request_total",
Help: "Counter of requests that bypassed resource control.",
ConstLabels: constLabels,
}, []string{LblResourceGroup, LblSource})
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@metrics/metrics.go` around lines 985 - 1001, The counters
TiKVResourceControlRUCounter and TiKVResourceControlBypassedCounter currently
hardcode Subsystem: "resource_control" which yields wrong metric names; update
their CounterOpts to use Subsystem: subsystem and move "resource_control" into
the Name field so the metrics become tikv_client_go_resource_control_ru_total
and tikv_client_go_resource_control_bypassed_request_total (i.e., set Name for
TiKVResourceControlRUCounter to "resource_control_ru_total" and for
TiKVResourceControlBypassedCounter to
"resource_control_bypassed_request_total"), leaving the rest (Help, ConstLabels,
label names LblResourceGroup/LblSource/LblType) unchanged.

Signed-off-by: Yuhao Zhang <yhzhang00@outlook.com>
@YuhaoZhang00
YuhaoZhang00 force-pushed the rc/resource-control-metrics branch from 3ea6073 to aff7447 Compare July 21, 2026 07:09
@ti-chi-bot

ti-chi-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign zhongzc for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 21, 2026
@YuhaoZhang00 YuhaoZhang00 changed the title resource_control: add Prometheus metrics for RU consumption and bypassed requests resourcecontrol: propagate request source for RU metrics Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution This PR is from a community contributor. dco-signoff: yes Indicates the PR's author has signed the dco. first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant