Skip to content

Commit

Permalink
Merge pull request #2217 from authzed/mysql_logging
Browse files Browse the repository at this point in the history
[mysql] pipe logger into mysql for debug printing (#1055)
  • Loading branch information
barakmich authored Jan 29, 2025
2 parents 00c7884 + fa5e56e commit 542053f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
17 changes: 13 additions & 4 deletions internal/datastore/mysql/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ func newMySQLDatastore(ctx context.Context, uri string, replicaIndex int, option
return nil, fmt.Errorf(errUnableToInstantiate, err)
}

parsedURI, err := mysql.ParseDSN(uri)
mysqlConfig, err := mysql.ParseDSN(uri)
if err != nil {
return nil, common.RedactAndLogSensitiveConnString(ctx, "NewMySQLDatastore: could not parse connection URI", err, uri)
}

if !parsedURI.ParseTime {
if !mysqlConfig.ParseTime {
return nil, errors.New("error in NewMySQLDatastore: connection URI for MySQL datastore must include `parseTime=true` as a query parameter; see https://spicedb.dev/d/parse-time-mysql for more details")
}

Expand All @@ -150,13 +150,16 @@ func newMySQLDatastore(ctx context.Context, uri string, replicaIndex int, option
}
}

err = mysqlCommon.MaybeAddCredentialsProviderHook(parsedURI, credentialsProvider)
err = mysqlCommon.MaybeAddCredentialsProviderHook(mysqlConfig, credentialsProvider)
if err != nil {
return nil, err
}

// Feed our logger through to the Connector
mysqlConfig.Logger = debugLogger{}

// Call NewConnector with the existing parsed configuration to preserve the BeforeConnect added by the CredentialsProvider
connector, err := mysql.NewConnector(parsedURI)
connector, err := mysql.NewConnector(mysqlConfig)
if err != nil {
return nil, common.RedactAndLogSensitiveConnString(ctx, "NewMySQLDatastore: failed to create connector", err, uri)
}
Expand Down Expand Up @@ -669,3 +672,9 @@ func buildLivingObjectFilterForRevision(revision datastore.Revision) queryFilter
func currentlyLivingObjects(original sq.SelectBuilder) sq.SelectBuilder {
return original.Where(sq.Eq{colDeletedTxn: liveDeletedTxnID})
}

type debugLogger struct{}

func (debugLogger) Print(v ...any) {
log.Logger.Debug().CallerSkipFrame(1).Str("datastore", "mysql").Msg(fmt.Sprint(v...))
}
9 changes: 0 additions & 9 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package logging

import (
"context"
"fmt"

"github.com/rs/zerolog"
)
Expand Down Expand Up @@ -39,11 +38,3 @@ func WithLevel(level zerolog.Level) *zerolog.Event { return Logger.WithLevel(lev
func Log() *zerolog.Event { return Logger.Log() }

func Ctx(ctx context.Context) *zerolog.Logger { return zerolog.Ctx(ctx) }

func Print(v ...interface{}) {
Logger.Debug().CallerSkipFrame(1).Msg(fmt.Sprint(v...))
}

func Printf(format string, v ...interface{}) {
Logger.Debug().CallerSkipFrame(1).Msgf(format, v...)
}

0 comments on commit 542053f

Please sign in to comment.