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

Fix data race with logging #1215

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 6 additions & 9 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ type FrameSession struct {
vu k6modules.VU

logger *log.Logger
// logger that will properly serialize RemoteObject instances
serializer *log.Logger

// Keep a reference to the main frame span so we can end it
// when FrameSession.ctx is Done
Expand Down Expand Up @@ -105,7 +103,6 @@ func NewFrameSession(
vu: k6ext.GetVU(ctx),
k6Metrics: k6Metrics,
logger: l,
serializer: l.ConsoleLogFormatterSerializer(),
}

var parentNM *NetworkManager
Expand Down Expand Up @@ -612,7 +609,7 @@ func (fs *FrameSession) navigateFrame(frame *Frame, url, referrer string) (strin
}

func (fs *FrameSession) onConsoleAPICalled(event *cdpruntime.EventConsoleAPICalled) {
l := fs.serializer.
l := fs.logger.
WithTime(event.Timestamp.Time()).
WithField("source", "browser").
WithField("browser_source", "console-api")
Expand All @@ -632,19 +629,19 @@ func (fs *FrameSession) onConsoleAPICalled(event *cdpruntime.EventConsoleAPICall
parsedObjects = append(parsedObjects, s)
}

l = l.WithField("stringObjects", parsedObjects)
msg := strings.Join(parsedObjects, " ")

switch event.Type {
case "log", "info":
l.Info()
l.Info(msg)
case "warning":
l.Warn()
l.Warn(msg)
case "error":
l.Error()
l.Error(msg)
default:
// this is where debug & other console.* apis will default to (such as
// console.table).
l.Debug()
l.Debug(msg)
}
}

Expand Down
28 changes: 0 additions & 28 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"regexp"
"runtime"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -152,19 +151,6 @@ func (l *Logger) ReportCaller() {
l.SetReportCaller(true)
}

// ConsoleLogFormatterSerializer creates a new logger that will
// correctly serialize RemoteObject instances.
func (l *Logger) ConsoleLogFormatterSerializer() *Logger {
return &Logger{
Logger: &logrus.Logger{
Out: l.Out,
Level: l.Level,
Formatter: &consoleLogFormatter{l.Formatter},
Hooks: l.Hooks,
},
}
}

// SetCategoryFilter enables filtering logs by the filter regex.
func (l *Logger) SetCategoryFilter(filter string) (err error) {
if filter == "" {
Expand All @@ -175,17 +161,3 @@ func (l *Logger) SetCategoryFilter(filter string) (err error) {
}
return nil
}

type consoleLogFormatter struct {
logrus.Formatter
}

// Format assembles a message from marshalling elements in the "objects" field
// to JSON separated by space, and deletes the field when done.
func (f *consoleLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
if stringObjects, ok := entry.Data["stringObjects"].([]string); ok {
entry.Message = strings.Join(stringObjects, " ")
delete(entry.Data, "stringObjects")
}
return f.Formatter.Format(entry)
}
45 changes: 0 additions & 45 deletions log/logger_test.go

This file was deleted.