-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
roachprod: redirect CRDB logs utility #132586
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "logger", | ||
srcs = ["log.go"], | ||
srcs = [ | ||
"log.go", | ||
"log_redirect.go", | ||
], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/roachprod/logger", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/cli/exit", | ||
"//pkg/util/log", | ||
"//pkg/util/log/logconfig", | ||
"//pkg/util/log/logpb", | ||
"//pkg/util/syncutil", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "logger_test", | ||
srcs = ["log_redirect_test.go"], | ||
embed = [":logger"], | ||
deps = [ | ||
"//pkg/util/log", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the CockroachDB Software License | ||
// included in the /LICENSE file. | ||
|
||
package logger | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/cockroach/pkg/util/log/logconfig" | ||
"github.com/cockroachdb/cockroach/pkg/util/log/logpb" | ||
"github.com/cockroachdb/cockroach/pkg/util/syncutil" | ||
) | ||
|
||
type logRedirect struct { | ||
syncutil.Mutex | ||
logger *Logger | ||
cancelIntercept func() | ||
configured bool | ||
} | ||
|
||
var logRedirectInst = &logRedirect{} | ||
|
||
// InitCRDBLogConfig sets up an interceptor for the CockroachDB log in order to | ||
// redirect logs to a roachprod logger. This is necessary as the CockroachDB log | ||
// is used in some test utilities shared between roachtest and CockroachDB. | ||
// Generally, CockroachDB logs should not be used explicitly in roachtest. | ||
func InitCRDBLogConfig(logger *Logger) { | ||
logRedirectInst.Lock() | ||
defer logRedirectInst.Unlock() | ||
if logRedirectInst.configured { | ||
panic("internal error: CRDB log interceptor already configured") | ||
} | ||
if logger == nil { | ||
panic("internal error: specified logger is nil") | ||
} | ||
logConf := logconfig.DefaultStderrConfig() | ||
logConf.Sinks.Stderr.Filter = logpb.Severity_FATAL | ||
// Disable logging to a file. File sinks write the application arguments to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this just to be extra sure or will it still log to file even with the intercept below? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah actually it seems like you need it at the very least because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, if I remember correctly you have to validate and apply the config, otherwise the default logger kick in and starts printing to stderr/stdout which we want to avoid as well. |
||
// the log by default (see: log_entry.go), and it is best to avoid logging | ||
// the roachtest arguments as it may contain sensitive information. | ||
if err := logConf.Validate(nil); err != nil { | ||
panic(fmt.Errorf("internal error: could not validate CRDB log config: %w", err)) | ||
} | ||
if _, err := log.ApplyConfig(logConf, nil /* fileSinkMetricsForDir */, nil /* fatalOnLogStall */); err != nil { | ||
panic(fmt.Errorf("internal error: could not apply CRDB log config: %w", err)) | ||
} | ||
logRedirectInst.logger = logger | ||
logRedirectInst.cancelIntercept = log.InterceptWith(context.Background(), logRedirectInst) | ||
logRedirectInst.configured = true | ||
} | ||
|
||
// TestingCRDBLogConfig is meant to be used in unit tests to reset the CRDB log, | ||
// it's interceptor, and the redirect log config. This is necessary to avoid | ||
// leaking state between tests, that test the logging. | ||
func TestingCRDBLogConfig(logger *Logger) { | ||
logRedirectInst.Lock() | ||
defer logRedirectInst.Unlock() | ||
if logRedirectInst.cancelIntercept != nil { | ||
logRedirectInst.cancelIntercept() | ||
} | ||
log.TestingResetActive() | ||
logRedirectInst = &logRedirect{} | ||
InitCRDBLogConfig(logger) | ||
} | ||
|
||
// Intercept intercepts CockroachDB log entries and redirects it to the | ||
// appropriate roachtest test logger or stderr. | ||
func (i *logRedirect) Intercept(logData []byte) { | ||
var entry logpb.Entry | ||
if err := json.Unmarshal(logData, &entry); err != nil { | ||
i.logger.Errorf("failed to unmarshal intercepted log entry: %v", err) | ||
} | ||
l := i.logger | ||
if l != nil && !l.Closed() { | ||
if entry.Severity == logpb.Severity_ERROR || entry.Severity == logpb.Severity_FATAL { | ||
i.writeLog(l.Stderr, entry) | ||
return | ||
} | ||
i.writeLog(l.Stdout, entry) | ||
} | ||
} | ||
|
||
func (i *logRedirect) writeLog(dst io.Writer, entry logpb.Entry) { | ||
if err := log.FormatLegacyEntry(entry, dst); err != nil { | ||
i.logger.Errorf("could not format and write CRDB log entry: %v", err) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the CockroachDB Software License | ||
// included in the /LICENSE file. | ||
|
||
package logger | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockLogger struct { | ||
logger *Logger | ||
writer *mockWriter | ||
} | ||
|
||
type mockWriter struct { | ||
lines []string | ||
} | ||
|
||
func (w *mockWriter) Write(p []byte) (n int, err error) { | ||
w.lines = append(w.lines, string(p)) | ||
return len(p), nil | ||
} | ||
|
||
func newMockLogger(t *testing.T) *mockLogger { | ||
writer := &mockWriter{} | ||
logConf := Config{Stdout: writer, Stderr: writer} | ||
l, err := logConf.NewLogger("" /* path */) | ||
require.NoError(t, err) | ||
return &mockLogger{logger: l, writer: writer} | ||
} | ||
|
||
func requireLine(t *testing.T, l *mockLogger, line string) { | ||
t.Helper() | ||
found := false | ||
for _, logLine := range l.writer.lines { | ||
if strings.Contains(logLine, line) { | ||
found = true | ||
break | ||
} | ||
} | ||
require.True(t, found, "expected line not found: %s", line) | ||
} | ||
|
||
func TestLogRedirect(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the goal of the PR is to make sure we stop leaking roachtest args, then should we have a test for that? Not entirely sure how to easily do that though, simplest thing I can think of is maybe assert that we only see one line in the writer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's the main goal. Initially I had a goroutine router (scoping log calls for CRDB log to goroutines so it ends up with the test log) as well to maybe make the CRDB log more functional for roachtests. But it ended up feeling like an overly complex solution, so now it's just a barebones interceptor to avoid logging the args. We could technically make args optional in the logger itself, but I'd rather not mess with prod code for this. |
||
l := newMockLogger(t) | ||
TestingCRDBLogConfig(l.logger) | ||
ctx := context.Background() | ||
|
||
log.Infof(ctx, "[simple test]") | ||
requireLine(t, l, "[simple test]") | ||
require.Equal(t, 1, len(l.writer.lines)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't expect this to get called twice but is it something we want to panic for vs. returning an error/logging a warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with panics mostly because that's how it was previously, but I'm not strongly opinionated against errors here. I think since it's during the init phase a panic is okay and will probably crash the whole process, which I'd expect if for some reason we can't configure the logging it would be good to investigate.