providers/proxy: add gorm logging (#17758)

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens L.
2025-10-28 14:39:47 +01:00
committed by GitHub
parent e8013bf76a
commit e2904d13a9
3 changed files with 51 additions and 38 deletions
@@ -31,7 +31,7 @@ func (a *Application) getStore(p api.ProxyOutpostConfig, externalHost *url.URL)
}
if a.isEmbedded {
// New PostgreSQL store
ps, err := postgresstore.NewPostgresStore()
ps, err := postgresstore.NewPostgresStore(a.log)
if err != nil {
return nil, err
}
@@ -0,0 +1,48 @@
package postgresstore
import (
"context"
"time"
log "github.com/sirupsen/logrus"
gormlogger "gorm.io/gorm/logger"
)
type logrusLogger struct {
logger *log.Entry
}
func NewLogger(parent *log.Entry) *logrusLogger {
return &logrusLogger{
logger: parent,
}
}
func (l *logrusLogger) LogMode(gormlogger.LogLevel) gormlogger.Interface {
return l
}
func (l *logrusLogger) Info(ctx context.Context, s string, args ...interface{}) {
l.logger.WithContext(ctx).Infof(s, args...)
}
func (l *logrusLogger) Warn(ctx context.Context, s string, args ...interface{}) {
l.logger.WithContext(ctx).Warnf(s, args...)
}
func (l *logrusLogger) Error(ctx context.Context, s string, args ...interface{}) {
l.logger.WithContext(ctx).Errorf(s, args...)
}
func (l *logrusLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
elapsed := time.Since(begin)
sql, _ := fc()
fields := log.Fields{
"elapsed": elapsed,
}
if err != nil {
l.logger.WithContext(ctx).WithFields(fields).WithError(err).Error(sql)
return
}
l.logger.WithContext(ctx).WithFields(fields).Trace(sql)
}
@@ -7,7 +7,6 @@ import (
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/google/uuid"
@@ -18,7 +17,6 @@ import (
_ "gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"goauthentik.io/internal/config"
"goauthentik.io/internal/outpost/proxyv2/constants"
@@ -159,12 +157,12 @@ func SetupGORMWithRefreshablePool(cfg config.PostgreSQLConfig, gormConfig *gorm.
}
// NewPostgresStore returns a new PostgresStore
func NewPostgresStore() (*PostgresStore, error) {
func NewPostgresStore(log *log.Entry) (*PostgresStore, error) {
cfg := config.Get().PostgreSQL
// Configure GORM
gormConfig := &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
Logger: NewLogger(log),
NowFunc: func() time.Time {
return time.Now().UTC()
},
@@ -437,39 +435,6 @@ func generateSessionID() string {
return uuid.New().String()
}
var (
globalStore *PostgresStore
mu sync.Mutex
)
// GetPersistentStore creates a new postgres store if it is the first time the function has been called.
// If the function is called multiple times, the store from the variable is returned to ensure that only one instance is running.
func GetPersistentStore() (*PostgresStore, error) {
mu.Lock()
defer mu.Unlock()
if globalStore == nil {
store, err := NewPostgresStore()
if err != nil {
return nil, fmt.Errorf("failed to create persistent store: %w", err)
}
globalStore = store
}
return globalStore, nil
}
// StopPersistentStore stops the cleanup background job and clears the globalStore variable.
func StopPersistentStore() {
mu.Lock()
defer mu.Unlock()
if globalStore != nil {
_ = globalStore.Close()
globalStore = nil
}
}
// NewTestStore creates a PostgresStore for testing with the given database and pool.
// The pool reference is required to properly close connections in test cleanup.
func NewTestStore(db *gorm.DB, pool *RefreshableConnPool) *PostgresStore {