Skip to content
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

Preserve context in logger when calling FromContext. #11

Merged
merged 1 commit into from
Feb 6, 2024

Conversation

wlynch
Copy link
Member

@wlynch wlynch commented Feb 6, 2024

Modification of #5

Allows for passing through the logger while still using default Info methods. InfoContext remains to override the context with another value.

This changes the stored context value to store the underlying slog.Logger instead of the wrapped logger so we're not storing a context within a context.

Allows for passing through the logger while still using default Info
methods. InfoContext remains to override the context with another value.

This changes the stored context value to store the underlying
slog.Logger instead of the wrapped logger so we're not storing a context
within a context.
@wlynch wlynch requested a review from imjasonh February 6, 2024 17:32
@imjasonh imjasonh merged commit f4145d2 into chainguard-dev:main Feb 6, 2024
2 checks passed
imjasonh added a commit that referenced this pull request Feb 13, 2024
If `WithCloudTraceContext` is called from tests where GCE metadata isn't
available, it hangs indefinitely, which is not a great experience.

Some improvements in this PR:

- project ID lookup will only be performed once, instead of each time
`WithCloudTraceContext` is called
- if we detect we're being called in tests, the project ID won't be
looked up and we won't log with trace context
- add a standard short timeout to metadata requests, consistent with the
official client

Also add a test that shows that the `trace` context value is set when
the project ID env var is set; because this logging must go to stderr,
we can't easily test what actually gets logged, but we can show it
working in the verbose output:

```
go test ./gcp -v -count=1
=== RUN   TestTrace
=== RUN   TestTrace/no_env_set
{"time":"2024-02-12T20:22:14.059143-05:00","severity":"DEBUG","logging.googleapis.com/sourceLocation":{"function":"github.com/chainguard-dev/clog/gcp.insideTest","file":"/Users/jason/git/clog/gcp/trace.go","line":33},"message":"WithCloudTraceContext: inside test","function":"testing.tRunner","file":"/opt/homebrew/Cellar/go/1.21.5/libexec/src/testing/testing.go","line":1595}
{"time":"2024-02-12T20:22:14.059343-05:00","severity":"DEBUG","logging.googleapis.com/sourceLocation":{"function":"github.com/chainguard-dev/clog/gcp.WithCloudTraceContext.func1","file":"/Users/jason/git/clog/gcp/trace.go","line":56},"message":"WithCloudTraceContext: inside test, not looking up project ID"}
{"time":"2024-02-12T20:22:14.060156-05:00","severity":"INFO","logging.googleapis.com/sourceLocation":{"function":"github.com/chainguard-dev/clog/gcp.TestTrace.func2.1","file":"/Users/jason/git/clog/gcp/trace_test.go","line":37},"message":"hello world"}
=== RUN   TestTrace/env_set
{"time":"2024-02-12T20:22:14.060678-05:00","severity":"INFO","logging.googleapis.com/sourceLocation":{"function":"github.com/chainguard-dev/clog/gcp.TestTrace.func2.1","file":"/Users/jason/git/clog/gcp/trace_test.go","line":37},"message":"hello world","logging.googleapis.com/trace":"projects/my-project/traces/trace"}
--- PASS: TestTrace (0.00s)
    --- PASS: TestTrace/no_env_set (0.00s)
    --- PASS: TestTrace/env_set (0.00s)
PASS
ok      github.com/chainguard-dev/clog/gcp      0.166s
```

The test also attempts to check that GCE metadata isn't requested.

Through this work I've found that `clog.FromContext(ctx).Info("hello")`
doesn't carry the `trace` context value, possibly related to
#11

Signed-off-by: Jason Hall <[email protected]>
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.

2 participants