pkg/test/logstream
Matt Moore c4535cf5bf
[master] Format markdown (#1629)
Produced via:
  `prettier --write --prose-wrap=always $(find -name '*.md' | grep -v vendor | grep -v .github | grep -v docs/cmd/)`
/assign n3wscott vagababov
/cc n3wscott vagababov
2020-08-17 08:53:07 -07:00
..
README.md [master] Format markdown (#1629) 2020-08-17 08:53:07 -07:00
doc.go Upstream the logstream package from knative/serving. (#472) 2019-06-18 20:29:46 -07:00
interface.go Replace init with sync.Once (#1587) 2020-08-06 10:48:29 -07:00
kubelogs.go Make the match about smarter about filtering its own tests. (#1624) 2020-08-16 12:06:06 -07:00
null.go Replace init with sync.Once (#1587) 2020-08-06 10:48:29 -07:00

README.md

How to use logstream

This is a guide to start using logstream in your e2e testing.

Requirements

  1. The SYSTEM_NAMESPACE environment variable must be configured. Many of the knative test scripts already define this, and in some places (e.g. serving) randomize it. However, to facilitate usage outside of CI, you should consider including a package like this and linking it like this
  1. Test resources must be named with test.ObjectNameForTest(t)
  1. At the start of your test add: t.Cleanup(logstream.Start(t))

With that, you will start getting logs from the processes in the system namespace interleaved into your test output via t.Log.

How it works

In Knative we use zap.Logger for all of our logging, and most of those loggers (e.g. in the context of a reconcile) have been decorated with the "key" of the resource being processed. logstream simply decodes these structured log messages and when the key matches the naming prefix that ObjectNameForTest uses, it includes it into the test's output.

Integrating in Libraries.

When a shared component is set up and called from reconciliation, it may have it's own logger. If that component is dealing with individual resources, it can scope individual log statements to that resource by decorating the logger with its key like so:

logger := logger.With(zap.String(logkey.Key, resourceKey))

Now, any log statements that the library prints through this logger will appear in the logstream!

For an example of this pattern, see the knative/networking prober library.