Repository navigation
ROX-34356: add pg_stat_activity to diagnostic bundle - #20130
Conversation
📝 WalkthroughWalkthroughIntroduces PostgreSQL active session statistics collection to the debug service. A new Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant DebugService as Debug Service
participant StatsModule as Stats Module
participant DB as PostgreSQL
Client->>DebugService: getCentralDBData()
DebugService->>StatsModule: GetPGStatActivities(ctx, db, limit)
StatsModule->>DB: Query pg_stat_activity
DB-->>StatsModule: Active session records
StatsModule-->>DebugService: PGStatActivities{records, error}
Note over DebugService: Marshal to JSON
DebugService->>DebugService: Write to zip
DebugService-->>Client: Debug data bundle
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~28 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
central/debug/service/db_diagnostics_test.go (1)
73-86: Add an assertion for query truncation contractThis test covers presence/activity well; consider also asserting
len(*a.Query) <= 1000whena.Query != nilso thesubstr(query, 1, 1000)behavior is regression-tested.Proposed test addition
func (s *DBDiagnosticTestSuite) TestGetPGStatActivities() { activities := stats.GetPGStatActivities(s.ctx, s.dbPool, 100) s.Empty(activities.Error) s.True(len(activities.Activities) > 0) foundActive := false for _, a := range activities.Activities { + if a.Query != nil { + s.LessOrEqual(len(*a.Query), 1000) + } if a.State != nil && *a.State == "active" { foundActive = true break } } s.True(foundActive, "expected to find at least one active connection") }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@central/debug/service/db_diagnostics_test.go` around lines 73 - 86, The test TestGetPGStatActivities currently checks for active connections but omits validating the query truncation; inside the loop over activities.Activities in TestGetPGStatActivities, add an assertion that when a.Query != nil then len(*a.Query) <= 1000 to verify the substr(query, 1, 1000) contract is honored (use the test suite's assertion helpers, e.g., s.True or s.LessOrEqual, referencing activities.Activities and a.Query).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@central/debug/service/db_diagnostics_test.go`:
- Around line 73-86: The test TestGetPGStatActivities currently checks for
active connections but omits validating the query truncation; inside the loop
over activities.Activities in TestGetPGStatActivities, add an assertion that
when a.Query != nil then len(*a.Query) <= 1000 to verify the substr(query, 1,
1000) contract is honored (use the test suite's assertion helpers, e.g., s.True
or s.LessOrEqual, referencing activities.Activities and a.Query).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Central YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Pro Plus
Run ID: 061ac0c7-30f1-48f8-aefd-deb0e089a642
📒 Files selected for processing (3)
central/debug/service/db_diagnostics_test.gocentral/debug/service/service.gopkg/postgres/stats/stats.go
🚀 Build Images ReadyImages are ready for commit 67284ff. To use with deploy scripts: export MAIN_IMAGE_TAG=4.11.x-738-g67284ff2a7 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #20130 +/- ##
==========================================
- Coverage 49.77% 49.74% -0.03%
==========================================
Files 2767 2768 +1
Lines 209596 209705 +109
==========================================
Hits 104318 104318
- Misses 97579 97688 +109
Partials 7699 7699
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Support frequently needs pg_stat_activity data when triaging database performance issues but has to ask customers to run manual queries. This adds it to the diagnostic bundle automatically, following the existing pg_stat_statements/tuple stats pattern. Captures connection state, active queries, wait events, and transaction timestamps in central-db-pg-activity.json. On external databases with limited access, gracefully returns only the application own sessions. This change was partially generated by AI. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Match the style of PGStatStatement, PGTupleStat, and PGAnalyzeStat which rely on default Go JSON marshaling without explicit tags. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Keep type/function groupings together — each stat group (types + function) is now contiguous in the file. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Strip string literals from captured query text to prevent sensitive values from leaking into diagnostic bundles. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
3bcfc79 to
b6fae9c
Compare
|
/retest |
clickboo
left a comment
There was a problem hiding this comment.
LGTM. Recommendation to verify, if not already done, that the redaction is complete enough to handle the several sql types if required.
Description
Support frequently needs pg_stat_activity data when triaging database performance issues (slow queries, connection exhaustion, lock contention) but has to ask customers to run manual queries. This adds it to the diagnostic bundle automatically as central-db-pg-activity.json, following the existing pg_stat_statements/tuple stats pattern in pkg/postgres/stats.
Columns captured:
datname, pid, usename, application_name, backend_start, xact_start, query_start, state_change, wait_event_type, wait_event, state, backend_type, query (truncated to 1000 chars)
External database handling:
No special-casing needed. On external databases with limited privileges, PostgreSQL returns only the application's own sessions — which is the right data for diagnosing StackRox issues. If the view is completely inaccessible, the error is captured in the JSON output and the file is still written to the bundle.
User-facing documentation
Testing and quality
Automated testing
How I validated my change
go test -v -tags sql_integration ./central/debug/service/ \
-run TestDBDiagnostic/TestGetPGStatActivities -count=1
permission errors are expected in the common case