Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/psrpc-service-logger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"github.com/livekit/protocol": patch
"@livekit/protocol": patch
---

Hand psrpc the service logger in SetLogger so its bus errors are logged
4 changes: 4 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
"github.com/livekit/psrpc"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

Expand Down Expand Up @@ -59,6 +60,9 @@ func SetLogger(l Logger, name string) {
defaultLogger = l.WithName(name)
// pkg wrapper needs to drop two levels of depth
pkgLogger = l.WithCallDepth(1).WithName(name)
// psrpc reports its own bus failures (a dropped Redis subscription, a failed publish) only
// through its own package logger, which discards until one is set.
psrpc.SetLogger(logr.FromSlogHandler(ToSlogHandler(defaultLogger.WithComponent("psrpc"))))
}

func Debugw(msg string, keysAndValues ...any) {
Expand Down
55 changes: 55 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package logger

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net"
"os"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"

"github.com/livekit/psrpc/pkg/bus/redisbus"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
Expand Down Expand Up @@ -410,3 +418,50 @@ func TestLoggerCallDepth(t *testing.T) {
})
}
}

// chanWriteSyncer hands each write to the test, so a line logged from another goroutine is read
// without racing on a shared buffer.
type chanWriteSyncer chan []byte

func (c chanWriteSyncer) Write(p []byte) (int, error) {
c <- bytes.Clone(p)
return len(p), nil
}

func (chanWriteSyncer) Sync() error { return nil }

func TestSetLoggerRoutesPsrpcBusErrors(t *testing.T) {
t.Cleanup(func() {
defaultLogger = LogRLogger(discardLogger)
pkgLogger = LogRLogger(discardLogger)
})

lines := make(chanWriteSyncer, 1)
SetLogger(must.Get(NewZapLogger(&Config{}, jsonTee(lines))), "TEST")

dialErr := errors.New("redis is unreachable")
var dials atomic.Int32
rc := redis.NewClient(&redis.Options{
Dialer: func(context.Context, string, string) (net.Conn, error) {
if dials.Add(1) == 1 {
return nil, dialErr
}
// Park every reconnect after the first failure, so the bus logs once and then stays
// quiet instead of retrying into later tests.
select {}
},
})
redisbus.New(rc)

var line map[string]any
select {
case b := <-lines:
require.NoError(t, json.Unmarshal(b, &line))
case <-time.After(5 * time.Second):
t.Fatal("the psrpc bus error never reached the configured logger")
}
require.Equal(t, "error", line["level"])
require.Equal(t, "redis receive message failed", line["msg"])
require.Equal(t, "TEST.psrpc", line["logger"])
require.Contains(t, fmt.Sprint(line), dialErr.Error())
}
2 changes: 1 addition & 1 deletion logger/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ToSlogHandler(log Logger) slog.Handler {
switch log := log.(type) {
case ZapLogger:
zlog := log.ToZap().Desugar()
return zapslog.NewHandler(zlog.Core())
return zapslog.NewHandler(zlog.Core(), zapslog.WithName(zlog.Name()))
case LogRLogger:
return logr.ToSlogHandler(log.toLogr())
}
Expand Down
Loading