Skip to content

Update global metric logger to be contextual - #1366

Merged
google-prow-robot merged 1 commit into
knative:masterfrom
bobcatfish:global_logger
Jul 20, 2018
Merged

google-prow-robot merged 1 commit into
knative:masterfrom
bobcatfish:global_logger

Conversation

@bobcatfish

@bobcatfish bobcatfish commented Jun 27, 2018 •

Copy link
Copy Markdown
Contributor

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:

  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 :(

This changes the output of metrics b/c the context will be included as
well.

Previous output:

info	TestBlueGreenRoute	conformance/blue_green_test.go:188	Since the Configuration was updated a new Revision will be created and the Configuration will be updated
info	test/logging.go:64	metric WaitForConfigurationState/prodarwmlxlg/ConfigurationUpdatedWithRevision 1532038661515264014 1532038661611044911 95.780897ms
info	TestBlueGreenRoute	conformance/blue_green_test.go:195	Waiting for revision "prodarwmlxlg-00001" to be ready
info	test/logging.go:64	metric WaitForRevision/prodarwmlxlg-00001/RevisionIsReady 1532038661611154885 1532038665787220418 4.176065533s
info	TestBlueGreenRoute	conformance/blue_green_test.go:199	Waiting for revision "prodarwmlxlg-00002" to be ready

New output:

info	TestBlueGreenRoute	conformance/blue_green_test.go:195	Waiting for revision "prodbikgknnx-00001" to be ready
info	TestBlueGreenRoute	test/logging.go:64	metric WaitForRevision/prodbikgknnx-00001/RevisionIsReady 1532042663872700584 1532042669022228353 5.149527769s
info	TestBlueGreenRoute	conformance/blue_green_test.go:199	Waiting for revision "prodbikgknnx-00002" to be ready
info	TestBlueGreenRoute	test/logging.go:64	metric WaitForRevision/prodbikgknnx-00002/RevisionIsReady 1532042669022329733 1532042669098558396 76.228663ms

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

  • Use a context specific logger for emitting metrics
NONE

@google-prow-robot google-prow-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jun 27, 2018
@mdemirhan

Copy link
Copy Markdown
Contributor

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 ;)

@adrcunha

Copy link
Copy Markdown
Contributor

+1 for the exception and using a global logger, it's way cleaner.

@adrcunha

Copy link
Copy Markdown
Contributor

/lgtm
/hold

@google-prow-robot google-prow-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm Indicates that a PR is ready to be merged. labels Jun 27, 2018
Comment thread test/logging.go
// 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we just drop glog now?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

no, glog is for getting k8s logs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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!

@jessiezcc

Copy link
Copy Markdown
Contributor

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.

@mdemirhan

Copy link
Copy Markdown
Contributor

@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 :))

@jessiezcc

Copy link
Copy Markdown
Contributor

@mdemirhan All TCs are sharing the same logger, aren't they going to step on each other's toes?

@bobcatfish

Copy link
Copy Markdown
Contributor Author

@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.

@bobcatfish

Copy link
Copy Markdown
Contributor Author

I think @jessiezcc is right - I'm gonna think about this a bit more and come back to it.

@mdemirhan

Copy link
Copy Markdown
Contributor

@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 LoggerWithName that returns a named version of the global logger and doesn't return global logger directly. That will not work well with the way you register and unregister exporters for traces though. I think you should register the exporter only once with the global logger and when you create spans, add contextual information into the span itself (like the name of the test for example).

@google-prow-robot google-prow-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed lgtm Indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jul 19, 2018
@bobcatfish bobcatfish changed the title Use a global log object in the tests Update global metric logger to be contextual Jul 19, 2018
@bobcatfish

Copy link
Copy Markdown
Contributor Author

I've updated this to make the metric logger contextual and NOT to use a global logger, PTAL

Comment thread test/logging.go
trace.UnregisterExporter(exporter)
}

logger := baseLogger.Named(context)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Named clone the logger, so we are good for concurrency again. Thanks for making the change!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 :(

@jessiezcc

Copy link
Copy Markdown
Contributor

/lgtm

@google-prow-robot google-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Jul 20, 2018
Comment thread test/adding_tests.md Outdated
test.GetContextLogger("TestHelloWorld")
```

_A side effect of `GetContextLogger` is to change the context of the logger

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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).

@adrcunha

Copy link
Copy Markdown
Contributor

/approve
/hold

Feel free to unhold when you think this is ready for merging.

@google-prow-robot

Copy link
Copy Markdown

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-prow-robot google-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 20, 2018
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.
@google-prow-robot google-prow-robot removed the lgtm Indicates that a PR is ready to be merged. label Jul 20, 2018
@bobcatfish

Copy link
Copy Markdown
Contributor Author

/hold cancel

Need another lgtm when you get a chance @adrcunha or @jessiezcc

@google-prow-robot google-prow-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 20, 2018
@adrcunha

Copy link
Copy Markdown
Contributor

/lgtm

@google-prow-robot google-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Jul 20, 2018
@google-prow-robot
google-prow-robot merged commit 1e055d3 into knative:master Jul 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants