Skip to content

feature (logging): Logging sdk key with every log message. #242

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 29 commits into from
Mar 25, 2020

Conversation

thomaszurkan-optimizely
Copy link
Contributor

@thomaszurkan-optimizely thomaszurkan-optimizely commented Mar 19, 2020

The sdk key is passed to composite classes and classes that create other objects that require logging. Small grained classes just accept a logger.

The logger logs sdk key mapped value by default along with name.
There are setters and getters for the sdk key log mapping.
Added unit test for sdk key in log.

Also updated the travis unit tests from 1.8 to 1.10. and from 1.13 to 1.14.

Copy link
Contributor

@mikecdavis mikecdavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the recent activity with auth datafiles and the aforementioned request on the Java-SDK, perhaps using the SDK key is going to cause more waves than we'd like. As an alternative can we switch to supporting a configurable "instance-name" field that is added to the logger fields as opposed to the strict SDK key. Somewhat inspired by the NamedThreadFactory in the java-sdk, but also to add a bit more color to the existing logs since the current names are bit opaque since it's not even clear they originate from "optimizely" at all. Using a sequenced name like optimizey-1,optimizely-2...optimizely-n should give us the visibility we want and support a multi-instance environment without proliferating the SDK keys throughout the logs.

I think this should be a relatively minor change to the existing changes you've already made since moving to a struct-based logger was still crucial for this to implemented effectively.

eg := utils.NewExecGroup(ctx)
appClient := &OptimizelyClient{execGroup: eg, notificationCenter: registry.GetNotificationCenter(f.SDKKey)}
eg := utils.NewExecGroup(ctx, logging.GetLogger(f.SDKKey, "ExecGroup"))
appClient := &OptimizelyClient{execGroup: eg,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, are you running go fmt? You can add it to your IDE or run it manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ran it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, by default you will have a sdk key mapping of a log mapping which goes to optimizely-1,2,3, etc. To override, you would simply call UseSdkKeyForLogging(sdkKey) before creating your client factory.

@@ -41,8 +42,10 @@ func (m *MockRequester) Get(uri string, headers ...utils.Header) (response []byt
return args.Get(0).([]byte), args.Get(1).(http.Header), args.Int(2), args.Error(3)
}

var logger = logging.GetLogger("polling_manager_test", "PollingManager")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably do the same thing in client_test.go

@@ -129,6 +127,8 @@ func NewBatchEventProcessor(options ...BPOptionConfig) *BatchEventProcessor {
opt(p)
}

p.logger = logging.GetLogger(p.sdkKey, "BatchEventProcessor")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, can you directly to the &BatchEventProcessor{...} call above? This is more inline with how all the other loggers are set on the respective struct.

Copy link
Contributor Author

@thomaszurkan-optimizely thomaszurkan-optimizely Mar 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, because the sdk key is part of the config options. So, first I run my config options, they, I grab the sdk key, then I get the logger with the sdk key. Look at the whole method for context.

Comment on lines 36 to 50
messBuilder := strings.Builder{}
_, _ = messBuilder.WriteString("[")

_, _ = messBuilder.WriteString(level.String())
_, _ = messBuilder.WriteString("]")
for _, v := range fields {
if s, ok := v.(string); ok {
_, _ = messBuilder.WriteString("[")
_, _ = messBuilder.WriteString(s)
_, _ = messBuilder.WriteString("]")
}
}
_, _ = messBuilder.WriteString(" ")
_, _ = messBuilder.WriteString(message)
l.logger.Println(messBuilder.String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting that some customers might not want the SDK key included by default.
optimizely/java-sdk#362

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now using optimizely-1,2,3,4,5,etc. as a mapping.

@@ -19,14 +19,11 @@ package notification

import (
"fmt"
"github.com/optimizely/go-sdk/pkg/logging"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, should be after the builtins.

Copy link
Contributor

@mikecdavis mikecdavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

message = fmt.Sprintf("[%s][%s] %s", level, fields["name"], message)
l.logger.Println(message)
messBuilder := strings.Builder{}
_, _ = messBuilder.WriteString("[")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"_, _" is not needed here, and errors are not checked, I think fmt.Sprintf will be cleaner here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at the consumer level, there is no telling how many interfaces have been added. so, a sprintf is not appropriate here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, the , was a result of go lint. our linter required it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that linters complain about not checking an error. Anyway, WriteString is returning nil error all the time, so there is really no point of checking that error.

I wanted to simplify this:

                 _, _ = messBuilder.WriteString("[")
		_, _ = messBuilder.WriteString(level.String())
		_, _ = messBuilder.WriteString("]")

with

message = fmt.Sprintf("[%d]", level )
_, _ = messBuilder.WriteString(message)

maybe it's more readable

}

// NewMurmurhashBucketer returns a new instance of the murmurhash bucketer
func NewMurmurhashBucketer(hashSeed uint32) *MurmurhashBucketer {
func NewMurmurhashBucketer(sdkKey string, hashSeed uint32) *MurmurhashBucketer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

little weird that NewMurmurhashBucketer takes sdkKey .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. at that level, it should just take a logger. fixed.

@thomaszurkan-optimizely thomaszurkan-optimizely merged commit e149ce6 into master Mar 25, 2020
@thomaszurkan-optimizely thomaszurkan-optimizely deleted the loggingPerSDK branch March 25, 2020 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants