incubator-seata icon indicating copy to clipboard operation
incubator-seata copied to clipboard

feature: add SqlMonitor and SlowSqlEntry for SQL execution monitoring

Open Oscarcheng0312 opened this issue 6 months ago • 3 comments

  • [ ] I have registered the PR changes.

Ⅰ. Describe what this PR did

This PR introduces a new SQL monitoring utility to the seata-common module:

  • SqlMonitor: a singleton class for collecting SQL execution statistics.
    • Tracks slow SQL (execution time > threshold), up to maxSlowEntries
    • Maintains histogram of transaction execution time
    • Maintains histogram of connection hold time
    • Thread-safe using concurrent data structures and explicit locking
    • Includes setters setSlowThreshold() and setMaxSlowEntries() for future dynamic configuration
  • SlowSqlEntry: a simple immutable model class holding slow SQL info (text, exec time, timestamp)

Ⅱ. Does this pull request fix one issue?

No. This is a new feature and the first step in the SQL monitoring implementation plan.

Ⅲ. Why don't you add test cases (unit test/integration test)?

Test cases are included:

  • SqlMonitorTest.java covers:
    • Slow SQL recording and eviction behavior
    • Histogram bucket logic for transaction/hold times
    • Fast SQL skip logic
    • Queue size trimming

Ⅳ. Describe how to verify it

  • Run SqlMonitorTest with JUnit
  • Verify correctness of slow SQL detection, histogram aggregation, and bounded queue behavior

Ⅴ. Special notes for reviews

  • This is PR 1/3 in the SQL monitoring feature set
  • Follow-up PRs will integrate this utility with seata-rm-datasource (recording) and seata-server (metrics exposure)
  • Configuration injection will be addressed later via application.yml or dynamic configuration center

Oscarcheng0312 avatar Jul 02 '25 11:07 Oscarcheng0312

Hi @slievrly, this PR adds SqlMonitor and SlowSqlEntry to support SQL execution monitoring (slow SQL tracking + histograms).
Would you mind reviewing it when convenient? This is the first part of the GSoC connection pool monitoring plan. Thanks!

Oscarcheng0312 avatar Jul 02 '25 11:07 Oscarcheng0312

Codecov Report

:x: Patch coverage is 90.32258% with 6 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 60.40%. Comparing base (db73418) to head (74437dc). :warning: Report is 27 commits behind head on 2.x.

Files with missing lines Patch % Lines
...va/org/apache/seata/common/monitor/SqlMonitor.java 92.45% 4 Missing :warning:
.../org/apache/seata/common/monitor/SlowSqlEntry.java 77.77% 2 Missing :warning:
Additional details and impacted files
@@             Coverage Diff              @@
##                2.x    #7491      +/-   ##
============================================
+ Coverage     60.34%   60.40%   +0.05%     
  Complexity      658      658              
============================================
  Files          1284     1286       +2     
  Lines         48465    48527      +62     
  Branches       5694     5702       +8     
============================================
+ Hits          29247    29311      +64     
+ Misses        16600    16599       -1     
+ Partials       2618     2617       -1     
Files with missing lines Coverage Δ
.../org/apache/seata/common/monitor/SlowSqlEntry.java 77.77% <77.77%> (ø)
...va/org/apache/seata/common/monitor/SqlMonitor.java 92.45% <92.45%> (ø)

... and 9 files with indirect coverage changes

Impacted file tree graph

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jul 02 '25 11:07 codecov[bot]

@slievrly Hi, pls take a look when you are convenient.

Oscarcheng0312 avatar Jul 05 '25 16:07 Oscarcheng0312