Repository navigation
Update global metric logger to be contextual - #1366
Conversation
|
I agree that tests seem like a good candidate for an exception and I like a global logger in tests more than passing around the logger in each call ;) |
|
+1 for the exception and using a global logger, it's way cleaner. |
|
/lgtm |
| // Both gLog and "go test" use -v flag. The code below is a work around so that we can still set v value for gLog | ||
| flag.StringVar(&logLevel, "logLevel", fmt.Sprint(VerboseLogLevel), "verbose log level") | ||
| flag.Lookup("v").Value.Set(logLevel) | ||
| glog.Infof("Logging set to verbose mode with logLevel %d", VerboseLogLevel) |
There was a problem hiding this comment.
Can we just drop glog now?
There was a problem hiding this comment.
no, glog is for getting k8s logs
There was a problem hiding this comment.
unfortunately client-go (which is used by the knative client libs) uses glog and it can be super handy to have requests and responses logged when things go wrong.
i'll add some detail on this to the comment!
|
I have mixed feeling about global logger. When we run tests in sequential, it's fine. but this prevents us from running tests concurrently later on. Also each test case may have different context fields in future, for example different object type/ids, it would be hard to manage these fields centralized. |
|
@jessiezcc ANy reason why a global logger cannot serve parallel tests? zap logger is thread safe - initialization of the logger can be made thread safe as well (if it is not already - too lazy to check :)) |
|
@mdemirhan All TCs are sharing the same logger, aren't they going to step on each other's toes? |
In this PR the global logger is specific to each test case, i.e. the name / sub category whatever will be the name of the last test that initialized it, so if tests were running in parallel you'd end up with tests logging with the wrong context. |
|
I think @jessiezcc is right - I'm gonna think about this a bit more and come back to it. |
|
@jessiezcc @bobcatfish I see what you mean by sharing the logger (should have looked at the code before talking :)) One recommendation is to not expose the global logger like it is done in the PR but instead have a func like |
06b79ee to
8432368
Compare
|
I've updated this to make the metric logger contextual and NOT to use a global logger, PTAL |
| trace.UnregisterExporter(exporter) | ||
| } | ||
|
|
||
| logger := baseLogger.Named(context) |
There was a problem hiding this comment.
Named clone the logger, so we are good for concurrency again. Thanks for making the change!
There was a problem hiding this comment.
no problem! note that when running the tests concurrently, the context of the metrics will still be incorrect, but this is unavoidable with the metrics lib we are using :(
|
/lgtm |
| test.GetContextLogger("TestHelloWorld") | ||
| ``` | ||
|
|
||
| _A side effect of `GetContextLogger` is to change the context of the logger |
There was a problem hiding this comment.
Does it really matter? I mean, isn't expected now that everything, including the metric emission, is contextualized? If you agree, I think this note can be removed to avoid confusion.
There was a problem hiding this comment.
Yeah, you're right. I'll make sure it's mentioned in the docstring and the commit message, but it probably doesn't need to be mentioned here.
The only weird thing is that if the tests are run in parallel (in the same process), the metrics logger is going to end up using the wrong context (i.e. whichever test has most recently created its own logger ) since the metrics logger is global. The only way around this that I can think of is to use a metrics lib that doesn't record metrics globally :S
There was a problem hiding this comment.
Ok, I think this is indeed worth noting as a comment. Right now the E2E tests don't run parallel, and even if they do, the metrics aggregation job doesn't care about the context, so we're good (if they do in the future, we'll fix it somehow).
|
/approve Feel free to unhold when you think this is ready for merging. |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: adrcunha, bobcatfish The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Initially this was an experiment to use a global logging object, for 2 reasons: 1. The opencensus metrics exporter has no choice but to be global 2. It clutters up the function signatures a bit After that experiment I have decided that it is not a good idea to use a global logger because: 1. It promotes bad practices 2. What if we ever want to write tests FOR our test libraries? (if they become sophisticated enough) And this _may_ lead to some of these libs one day being actual libraries, in which case they should follow best practices for libraries. So instead I have updated the metrics emitter to also be contextual, and sadly there is no way to avoid it being global :( which means that if tests are run in parallel within the same process, each test will be emitting metrics using the context of whichever test had most recently created a logger :( :( :( The only way around this I can think of is to use a metrics emitter that is not global (i.e. a different lib). This changes the output of metrics b/c the context will be included as well. A possible future modification would be to create structs for some of these libs and initialize those structs with a logger object so the function signatures do not include a logger.
8432368 to
64e7bb2
Compare
|
/hold cancel Need another |
|
/lgtm |
Passing around a logger object is a good pattern, however it makes the interface to the test library methods more cluttered. It is also a good practice to avoid logging in libraries, however in our tests we actively want the libraries to log for us to provide context for debugging, which leads me (and maybe @jonjohnsonjr?) to the conclusion that test libraries really are a special case, and it might not be totally evil to have a global logging object.Initially this was an experiment to use a global logging object, for
2 reasons:
After that experiment I have decided that it is not a good idea to
use a global logger because:
become sophisticated enough) And this may lead to some of these
libs one day being actual libraries, in which case they should follow
best practices for libraries.
So instead I have updated the metrics emitter to also be contextual, and
sadly there is no way to avoid it being global :(
This changes the output of metrics b/c the context will be included as
well.
Previous output:
New output:
A possible future modification would be to create structs for some of
these libs and initialize those structs with a logger object so the
function signatures do not include a logger.
Proposed Changes