Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use slog in debug package #4473

Merged
merged 13 commits into from
Jul 22, 2024
Next Next commit
use slog in handler
  • Loading branch information
chilagrow committed Jul 17, 2024
commit 16c05af75cd077f2d211952cd9d4f41f3d21e95a
1 change: 1 addition & 0 deletions cmd/ferretdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ func run() {

h, closeBackend, err := registry.NewHandler(cli.Handler, &registry.NewHandlerOpts{
Logger: logger,
SLogger: slogger,
ConnMetrics: metrics.ConnMetrics,
StateProvider: stateProvider,
TCPHost: cli.Listen.Addr,
Expand Down
2 changes: 2 additions & 0 deletions ferretdb/ferretdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/url"
"sync"

Expand Down Expand Up @@ -126,6 +127,7 @@ func New(config *Config) (*FerretDB, error) {

h, closeBackend, err := registry.NewHandler(config.Handler, &registry.NewHandlerOpts{
Logger: log,
SLogger: slog.Default(), // TODO https://github.com/FerretDB/FerretDB/issues/4013
ConnMetrics: metrics.ConnMetrics,
StateProvider: sp,
TCPHost: config.Listener.TCP,
Expand Down
2 changes: 2 additions & 0 deletions integration/setup/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package setup

import (
"context"
"log/slog"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -179,6 +180,7 @@ func setupListener(tb testtb.TB, ctx context.Context, logger *zap.Logger, opts *

handlerOpts := &registry.NewHandlerOpts{
Logger: logger,
SLogger: slog.Default(), // TODO https://github.com/FerretDB/FerretDB/issues/4013
ConnMetrics: listenerMetrics.ConnMetrics,
StateProvider: sp,

Expand Down
9 changes: 4 additions & 5 deletions internal/handler/cmd_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
"fmt"
"strings"

"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/handler/common"
"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/logging"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/wire"
)
Expand Down Expand Up @@ -75,7 +74,7 @@

dbName, err := common.GetRequiredParam[string](authDoc, "db")
if err != nil {
h.L.Debug("No `db` in `speculativeAuthenticate`", zap.Error(err))
h.L.DebugContext(connCtx, "No `db` in `speculativeAuthenticate`", logging.Error(err))

Check warning on line 77 in internal/handler/cmd_query.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/cmd_query.go#L77

Added line #L77 was not covered by tests

opReply.SetDocument(reply)

Expand All @@ -84,7 +83,7 @@

speculativeAuthenticate, err := h.saslStart(connCtx, dbName, authDoc)
if err != nil {
h.L.Debug("Speculative authentication failed", zap.Error(err))
h.L.DebugContext(connCtx, "Speculative authentication failed", logging.Error(err))

// unsuccessful speculative authentication leave `speculativeAuthenticate` field unset
// and let `saslStart` return an error
Expand All @@ -93,7 +92,7 @@
return &opReply, nil
}

h.L.Debug("Speculative authentication passed")
h.L.DebugContext(connCtx, "Speculative authentication passed")

reply.Set("speculativeAuthenticate", speculativeAuthenticate)

Expand Down
17 changes: 9 additions & 8 deletions internal/handler/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import (
"context"

"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/clientconn/conninfo"
"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
Expand Down Expand Up @@ -295,23 +294,25 @@
}

// checkSCRAMConversation returns error if SCRAM conversation is not valid.
func checkSCRAMConversation(ctx context.Context, l *zap.Logger) error {
func checkSCRAMConversation(ctx context.Context, l *slog.Logger) error {
_, _, conv, _ := conninfo.Get(ctx).Auth()

switch {
case conv == nil:
l.Warn("checkSCRAMConversation: no conversation")
l.WarnContext(ctx, "checkSCRAMConversation: no conversation")

Check warning on line 302 in internal/handler/commands.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/commands.go#L302

Added line #L302 was not covered by tests

case !conv.Valid():
l.Warn(
l.WarnContext(
ctx,

Check warning on line 306 in internal/handler/commands.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/commands.go#L305-L306

Added lines #L305 - L306 were not covered by tests
"checkSCRAMConversation: invalid conversation",
zap.String("username", conv.Username()), zap.Bool("valid", conv.Valid()), zap.Bool("done", conv.Done()),
slog.String("username", conv.Username()), slog.Bool("valid", conv.Valid()), slog.Bool("done", conv.Done()),

Check warning on line 308 in internal/handler/commands.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/commands.go#L308

Added line #L308 was not covered by tests
)

default:
l.Debug(
l.DebugContext(
ctx,
"checkSCRAMConversation: passed",
zap.String("username", conv.Username()), zap.Bool("valid", conv.Valid()), zap.Bool("done", conv.Done()),
slog.String("username", conv.Username()), slog.Bool("valid", conv.Valid()), slog.Bool("done", conv.Done()),
)

return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/common/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package common

import (
"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/handler/handlerparams"
"github.com/FerretDB/FerretDB/internal/types"
Expand Down Expand Up @@ -44,7 +44,7 @@ type CountParams struct {
}

// GetCountParams returns the parameters for the count command.
func GetCountParams(document *types.Document, l *zap.Logger) (*CountParams, error) {
func GetCountParams(document *types.Document, l *slog.Logger) (*CountParams, error) {
var count CountParams

err := handlerparams.ExtractParams(document, "count", &count, l)
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/common/delete_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package common

import (
"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/handler/handlerparams"
"github.com/FerretDB/FerretDB/internal/types"
Expand Down Expand Up @@ -55,7 +55,7 @@ type Delete struct {
}

// GetDeleteParams returns parameters for delete operation.
func GetDeleteParams(document *types.Document, l *zap.Logger) (*DeleteParams, error) {
func GetDeleteParams(document *types.Document, l *slog.Logger) (*DeleteParams, error) {
params := DeleteParams{
Ordered: true,
}
Expand Down
5 changes: 2 additions & 3 deletions internal/handler/common/distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ package common
import (
"errors"
"fmt"

"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/handler/commonpath"
"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
Expand Down Expand Up @@ -49,7 +48,7 @@ type DistinctParams struct {
}

// GetDistinctParams returns `distinct` command parameters.
func GetDistinctParams(document *types.Document, l *zap.Logger) (*DistinctParams, error) {
func GetDistinctParams(document *types.Document, l *slog.Logger) (*DistinctParams, error) {
var dp DistinctParams

err := handlerparams.ExtractParams(document, "distinct", &dp, l)
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/common/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package common

import (
"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
"github.com/FerretDB/FerretDB/internal/handler/handlerparams"
Expand Down Expand Up @@ -45,7 +45,7 @@ type ExplainParams struct {
}

// GetExplainParams returns the parameters for the explain command.
func GetExplainParams(document *types.Document, l *zap.Logger) (*ExplainParams, error) {
func GetExplainParams(document *types.Document, l *slog.Logger) (*ExplainParams, error) {
var err error

var db, collection string
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/common/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package common

import (
"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
"github.com/FerretDB/FerretDB/internal/handler/handlerparams"
Expand Down Expand Up @@ -65,7 +65,7 @@ type FindParams struct {
}

// GetFindParams returns `find` command parameters.
func GetFindParams(doc *types.Document, l *zap.Logger) (*FindParams, error) {
func GetFindParams(doc *types.Document, l *slog.Logger) (*FindParams, error) {
params := FindParams{
BatchSize: 101,
}
Expand Down
5 changes: 2 additions & 3 deletions internal/handler/common/findandmodify.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ package common

import (
"fmt"

"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
"github.com/FerretDB/FerretDB/internal/handler/handlerparams"
Expand Down Expand Up @@ -59,7 +58,7 @@ type FindAndModifyParams struct {
}

// GetFindAndModifyParams returns `findAndModifyParams` command parameters.
func GetFindAndModifyParams(doc *types.Document, l *zap.Logger) (*FindAndModifyParams, error) {
func GetFindAndModifyParams(doc *types.Document, l *slog.Logger) (*FindAndModifyParams, error) {
var params FindAndModifyParams

err := handlerparams.ExtractParams(doc, "findAndModify", &params, l)
Expand Down
5 changes: 2 additions & 3 deletions internal/handler/common/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ package common

import (
"fmt"

"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
"github.com/FerretDB/FerretDB/internal/handler/handlerparams"
Expand Down Expand Up @@ -45,7 +44,7 @@ type InsertParams struct {
}

// GetInsertParams returns the parameters for an insert command.
func GetInsertParams(document *types.Document, l *zap.Logger) (*InsertParams, error) {
func GetInsertParams(document *types.Document, l *slog.Logger) (*InsertParams, error) {
params := InsertParams{
Ordered: true,
}
Expand Down
7 changes: 3 additions & 4 deletions internal/handler/common/unimplemented.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ package common

import (
"fmt"

"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
"github.com/FerretDB/FerretDB/internal/types"
Expand Down Expand Up @@ -60,12 +59,12 @@ func UnimplementedNonDefault(doc *types.Document, field string, isDefault func(v
}

// Ignored logs a message if doc has any of the given fields.
func Ignored(doc *types.Document, l *zap.Logger, fields ...string) {
func Ignored(doc *types.Document, l *slog.Logger, fields ...string) {
for _, field := range fields {
if v, err := doc.Get(field); err == nil {
l.Debug(
"ignoring field",
zap.String("command", doc.Command()), zap.String("field", field), zap.Any("value", v),
slog.String("command", doc.Command()), slog.String("field", field), slog.Any("value", v),
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/common/update_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package common

import (
"go.uber.org/zap"
"log/slog"

"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
"github.com/FerretDB/FerretDB/internal/handler/handlerparams"
Expand Down Expand Up @@ -84,7 +84,7 @@ type UpdateResult struct {
}

// GetUpdateParams returns parameters for update command.
func GetUpdateParams(document *types.Document, l *zap.Logger) (*UpdateParams, error) {
func GetUpdateParams(document *types.Document, l *slog.Logger) (*UpdateParams, error) {
var params UpdateParams

err := handlerparams.ExtractParams(document, "update", &params, l)
Expand Down
Loading
Loading