-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add structure logging to Vitess #11960
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9eb1e47
Create a function to replace glog with PlanetScale log
EmadMokhtar 3af20ab
Merge remote-tracking branch 'upstream/main'
EmadMokhtar 9efb9a3
Merge branch 'vitessio:main' into main
EmadMokhtar 91a600d
Add test case for replacing glog
EmadMokhtar e317113
Remove one test
EmadMokhtar cf25de9
Add flag for PS Logger usage
EmadMokhtar 9453797
Update the usage test files and replace _ with - for the flag
EmadMokhtar eb47a09
Apply code review comments
EmadMokhtar 823d7b0
Add copyrights and release notes
EmadMokhtar 4dcaf16
Update the year in the copyrights
EmadMokhtar 6d451d4
Fix typo
EmadMokhtar e388bf8
Merge remote-tracking branch 'upstream/main'
EmadMokhtar f52977d
Empty Commit
EmadMokhtar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| Copyright 2023 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package logutil | ||
|
|
||
| import ( | ||
| pslog "github.com/planetscale/log" | ||
| noglog "github.com/slok/noglog" | ||
|
|
||
| "vitess.io/vitess/go/vt/log" | ||
| ) | ||
|
|
||
| type PSLogger pslog.SugaredLogger | ||
|
|
||
| // SetPlanetScaleLogger in-place noglog replacement with PlanetScale's logger. | ||
| func SetPlanetScaleLogger(conf *pslog.Config) (psLogger *pslog.SugaredLogger, err error) { | ||
| // Use the passed configuration instead of the default configuration | ||
| if conf != nil { | ||
| configLogger, err := conf.Build() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| psLogger = configLogger.Sugar() | ||
| } else { | ||
| psLogger = pslog.NewPlanetScaleSugarLogger() | ||
| } | ||
|
|
||
| noglog.SetLogger(&noglog.LoggerFunc{ | ||
| DebugfFunc: func(f string, a ...interface{}) { psLogger.Debugf(f, a...) }, | ||
| InfofFunc: func(f string, a ...interface{}) { psLogger.Infof(f, a...) }, | ||
| WarnfFunc: func(f string, a ...interface{}) { psLogger.Warnf(f, a...) }, | ||
| ErrorfFunc: func(f string, a ...interface{}) { psLogger.Errorf(f, a...) }, | ||
| }) | ||
|
|
||
| log.Flush = noglog.Flush | ||
| log.Info = noglog.Info | ||
| log.Infof = noglog.Infof | ||
| log.InfoDepth = noglog.InfoDepth | ||
| log.Warning = noglog.Warning | ||
| log.Warningf = noglog.Warningf | ||
| log.WarningDepth = noglog.WarningDepth | ||
| log.Error = noglog.Error | ||
| log.Errorf = noglog.Errorf | ||
| log.ErrorDepth = noglog.ErrorDepth | ||
| log.Exit = noglog.Exit | ||
| log.Exitf = noglog.Exitf | ||
| log.ExitDepth = noglog.ExitDepth | ||
| log.Fatal = noglog.Fatal | ||
| log.Fatalf = noglog.Fatalf | ||
| log.FatalDepth = noglog.FatalDepth | ||
|
|
||
| return | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| Copyright 2023 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package logutil | ||
systay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "net/url" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| pslog "github.com/planetscale/log" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
|
|
||
| vtlog "vitess.io/vitess/go/vt/log" | ||
| ) | ||
|
|
||
| // MemorySink implements zap.Sink by writing all messages to a buffer. | ||
| // It's used to capture the logs. | ||
| type MemorySink struct { | ||
| *bytes.Buffer | ||
| } | ||
|
|
||
| // Implement Close and Sync as no-ops to satisfy the interface. The Write | ||
| // method is provided by the embedded buffer. | ||
| func (s *MemorySink) Close() error { return nil } | ||
| func (s *MemorySink) Sync() error { return nil } | ||
|
|
||
| func SetupLoggerWithMemSink() (sink *MemorySink, err error) { | ||
| // Create a sink instance, and register it with zap for the "memory" | ||
| // protocol. | ||
| sink = &MemorySink{new(bytes.Buffer)} | ||
| err = zap.RegisterSink("memory", func(*url.URL) (zap.Sink, error) { | ||
| return sink, nil | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| testLoggerConf := pslog.NewPlanetScaleConfig(pslog.DetectEncoding(), pslog.InfoLevel) | ||
| testLoggerConf.OutputPaths = []string{"memory://"} | ||
| testLoggerConf.ErrorOutputPaths = []string{"memory://"} | ||
| _, err = SetPlanetScaleLogger(&testLoggerConf) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| func TestPSLogger_Replacing_glog(t *testing.T) { | ||
| type logMsg struct { | ||
| Level string `json:"level"` | ||
| Msg string `json:"msg"` | ||
| } | ||
|
|
||
| type testCase struct { | ||
| name string | ||
| logLevel zapcore.Level | ||
| } | ||
|
|
||
| dummyLogMessage := "testing log" | ||
| testCases := []testCase{ | ||
| {"log info", pslog.InfoLevel}, | ||
| {"log warn", pslog.WarnLevel}, | ||
| {"log error", pslog.ErrorLevel}, | ||
| } | ||
|
|
||
| sink, err := SetupLoggerWithMemSink() | ||
| assert.NoError(t, err) | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var loggingFunc func(format string, args ...interface{}) | ||
| var expectedLevel string | ||
|
|
||
| switch tc.logLevel { | ||
| case zapcore.InfoLevel: | ||
| loggingFunc = vtlog.Infof | ||
| expectedLevel = "info" | ||
| case zapcore.ErrorLevel: | ||
| loggingFunc = vtlog.Errorf | ||
| expectedLevel = "error" | ||
| case zapcore.WarnLevel: | ||
| loggingFunc = vtlog.Warningf | ||
| expectedLevel = "warn" | ||
| } | ||
|
|
||
| loggingFunc(dummyLogMessage) | ||
|
|
||
| // Unmarshal the captured log. This means we're getting a struct log. | ||
| actualLog := logMsg{} | ||
| err = json.Unmarshal(sink.Bytes(), &actualLog) | ||
| assert.NoError(t, err) | ||
| // Reset the sink so that it'll contain one log per test case. | ||
| sink.Reset() | ||
|
|
||
| assert.Equal(t, expectedLevel, actualLog.Level) | ||
| assert.Equal(t, dummyLogMessage, actualLog.Msg) | ||
|
|
||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.