[MM-70225] Migrate Store.GetDiagnostics to request.CTX (#37635)

* Migrate store diagnostics to request context

Co-authored-by: Ben Schumacher <hanzei@users.noreply.github.com>

* Keep store context rule only in server AGENTS

Co-authored-by: Ben Schumacher <hanzei@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Ben Schumacher <hanzei@users.noreply.github.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Ben Schumacher
2026-08-12 09:03:09 +02:00
committed by GitHub
parent 265f1509fa
commit 270a503054
9 changed files with 26 additions and 23 deletions
+1
View File
@@ -16,3 +16,4 @@ When creating a pull request, follow `.github/PULL_REQUEST_TEMPLATE.md` exactly:
This repository has a checked-in Cloud Agent environment under `.cursor/`. Docker is started by `.cursor/scripts/cloud-agent-start.sh`; if Docker is unavailable in Cloud, treat that as an environment failure rather than falling back to snapshot assumptions.
The environment declares `mattermost/enterprise` as a Cursor multi-repo dependency. Cursor clones the repositories as siblings, so `server/Makefile` can use its default `../../enterprise` path; the install hook does not clone or symlink enterprise.
+2
View File
@@ -3,3 +3,5 @@
Never run `go mod tidy` directly. Always run `make modules-tidy` instead — it excludes private enterprise imports that would otherwise break the tidy.
After editing `i18n/en.json`, always run `make i18n-extract` — it regenerates the file with strings in the required order.
In the store layer, do not use `context.Context` in store method signatures. Use `request.CTX` and only call `rctx.Context()` inside internals that require a standard `context.Context`.
@@ -221,7 +221,7 @@ func (ps *PlatformService) getSupportPacketDiagnostics(rctx request.CTX) (*model
d.Database.ReplicaConnections = ps.Store.TotalReadDbConnections()
d.Database.SearchConnections = ps.Store.TotalSearchDbConnections()
err = ps.applyStoreDiagnostics(rctx.Context(), &d)
err = ps.applyStoreDiagnostics(rctx, &d)
if err != nil {
rErr = multierror.Append(rErr, err)
}
@@ -389,8 +389,8 @@ func (ps *PlatformService) getSupportPacketDiagnostics(rctx request.CTX) (*model
return fileData, rErr.ErrorOrNil()
}
func (ps *PlatformService) applyStoreDiagnostics(ctx context.Context, diagnostics *model.SupportPacketDiagnostics) error {
storeDiagnostics, err := ps.Store.GetDiagnostics(ctx)
func (ps *PlatformService) applyStoreDiagnostics(rctx request.CTX, diagnostics *model.SupportPacketDiagnostics) error {
storeDiagnostics, err := ps.Store.GetDiagnostics(rctx)
if storeDiagnostics == nil {
if err != nil {
return errors.Wrap(err, "error while collecting support packet database diagnostics")
@@ -6,7 +6,6 @@ package platform
import (
"bufio"
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
@@ -28,6 +27,7 @@ import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store"
"github.com/mattermost/mattermost/server/v8/channels/testlib"
"github.com/mattermost/mattermost/server/v8/config"
@@ -42,7 +42,7 @@ type fixedDBStatsStore struct {
replicaStats sql.DBStats
}
func (s *fixedDBStatsStore) GetDiagnostics(_ context.Context) (*store.DatabaseDiagnostics, error) {
func (s *fixedDBStatsStore) GetDiagnostics(_ request.CTX) (*store.DatabaseDiagnostics, error) {
diagnostics := &store.DatabaseDiagnostics{
MasterConnectionsInUse: s.masterStats.InUse,
MasterConnectionsIdle: s.masterStats.Idle,
@@ -13,12 +13,13 @@ import (
"github.com/pkg/errors"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store"
)
const pgDiagnosticsQueryTimeout = 10 * time.Second
func (ss *SqlStore) GetDiagnostics(ctx context.Context) (*store.DatabaseDiagnostics, error) {
func (ss *SqlStore) GetDiagnostics(rctx request.CTX) (*store.DatabaseDiagnostics, error) {
diagnostics := &store.DatabaseDiagnostics{}
applyDBPoolStats(diagnostics, ss.MasterDBStats(), ss.ReplicaDBStats())
@@ -26,7 +27,7 @@ func (ss *SqlStore) GetDiagnostics(ctx context.Context) (*store.DatabaseDiagnost
return diagnostics, nil
}
if err := collectPostgresDatabaseDiagnostics(ctx, ss.GetMaster().DB(), diagnostics); err != nil {
if err := collectPostgresDatabaseDiagnostics(rctx.Context(), ss.GetMaster().DB(), diagnostics); err != nil {
return diagnostics, err
}
@@ -4,7 +4,6 @@
package sqlstore
import (
"context"
"database/sql"
"testing"
"time"
@@ -18,10 +17,10 @@ import (
)
func TestGetDiagnostics(t *testing.T) {
StoreTest(t, func(t *testing.T, _ request.CTX, ss store.Store) {
StoreTest(t, func(t *testing.T, rctx request.CTX, ss store.Store) {
sqlStore := ss.(*SqlStore)
diagnostics, err := sqlStore.GetDiagnostics(context.Background())
diagnostics, err := sqlStore.GetDiagnostics(rctx)
require.NoError(t, err)
require.NotNil(t, diagnostics)
+1 -1
View File
@@ -80,7 +80,7 @@ type Store interface {
TotalMasterDbConnections() int
TotalReadDbConnections() int
TotalSearchDbConnections() int
GetDiagnostics(ctx context.Context) (*DatabaseDiagnostics, error)
GetDiagnostics(rctx request.CTX) (*DatabaseDiagnostics, error)
ReplicaLagTime() error
ReplicaLagAbs() error
CheckIntegrity() <-chan model.IntegrityCheckResult
+10 -10
View File
@@ -5,12 +5,12 @@
package mocks
import (
context "context"
sql "database/sql"
time "time"
model "github.com/mattermost/mattermost/server/public/model"
mlog "github.com/mattermost/mattermost/server/public/shared/mlog"
request "github.com/mattermost/mattermost/server/public/shared/request"
store "github.com/mattermost/mattermost/server/v8/channels/store"
mock "github.com/stretchr/testify/mock"
)
@@ -516,9 +516,9 @@ func (_m *Store) GetDbVersion(numerical bool) (string, error) {
return r0, r1
}
// GetDiagnostics provides a mock function with given fields: ctx
func (_m *Store) GetDiagnostics(ctx context.Context) (*store.DatabaseDiagnostics, error) {
ret := _m.Called(ctx)
// GetDiagnostics provides a mock function with given fields: rctx
func (_m *Store) GetDiagnostics(rctx request.CTX) (*store.DatabaseDiagnostics, error) {
ret := _m.Called(rctx)
if len(ret) == 0 {
panic("no return value specified for GetDiagnostics")
@@ -526,19 +526,19 @@ func (_m *Store) GetDiagnostics(ctx context.Context) (*store.DatabaseDiagnostics
var r0 *store.DatabaseDiagnostics
var r1 error
if rf, ok := ret.Get(0).(func(context.Context) (*store.DatabaseDiagnostics, error)); ok {
return rf(ctx)
if rf, ok := ret.Get(0).(func(request.CTX) (*store.DatabaseDiagnostics, error)); ok {
return rf(rctx)
}
if rf, ok := ret.Get(0).(func(context.Context) *store.DatabaseDiagnostics); ok {
r0 = rf(ctx)
if rf, ok := ret.Get(0).(func(request.CTX) *store.DatabaseDiagnostics); ok {
r0 = rf(rctx)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*store.DatabaseDiagnostics)
}
}
if rf, ok := ret.Get(1).(func(context.Context) error); ok {
r1 = rf(ctx)
if rf, ok := ret.Get(1).(func(request.CTX) error); ok {
r1 = rf(rctx)
} else {
r1 = ret.Error(1)
}
+2 -2
View File
@@ -4,7 +4,6 @@
package storetest
import (
"context"
"database/sql"
"time"
@@ -12,6 +11,7 @@ import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store"
"github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
)
@@ -204,7 +204,7 @@ func (s *Store) GetSchemaDefinition() (*model.SupportPacketDatabaseSchema, error
}, nil
}
func (s *Store) GetDiagnostics(_ context.Context) (*store.DatabaseDiagnostics, error) {
func (s *Store) GetDiagnostics(_ request.CTX) (*store.DatabaseDiagnostics, error) {
return &store.DatabaseDiagnostics{}, nil
}