According to
https://cloud.google.com/trace/docs/trace-context#http-requests,
`X-Cloud-Trace-Context` is a legacy header that precedes the W3C
standard `traceparent` header. Google Cloud services that support trace
context propagation typically support both the `traceparent` and the
legacy `X-Cloud-Trace-Context` header.
Switching to use this so that we don't have to use the legacy header
`X-Cloud-Trace-Context` when restoring trace context header for pubsub
eventing, but only using `traceparent` every where.
Currently `With()` will reset context back to `context.Background()`,
causing the `wrap/wrapf` calls to lose the original context.
This affects the `clog/gcp` handler who relies on a value keyed with
`"trace"` being set on the context
([see](https://github.com/chainguard-dev/clog/blob/main/gcp/trace.go#L110-L116))
(we should use a typed value though, but that's in a different PR).
---------
Signed-off-by: Nghia Tran <tcnghia@gmail.com>
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
https://github.com/chainguard-dev/clog/pull/11
Signed-off-by: Jason Hall <jason@chainguard.dev>
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.
This adds `clog.Fatal{Context}{f}`, and
`clog.FromContext(ctx).Fatal{Context}{f}`
`Fatal` logs at `LevelError` then calls `os.Exit(1)`, like stdlib
`log.Fatal`:
https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/log/log.go;l=413
---------
Signed-off-by: Jason Hall <jason@chainguard.dev>
Co-authored-by: Billy Lynch <1844673+wlynch@users.noreply.github.com>
I don't like any of the names here, and we might want it to be in a
separate subpackage. This lets you say `--log-level=DEBUG` to set the
level. The default is still INFO.
---------
Signed-off-by: Jason Hall <jason@chainguard.dev>
* rename to clog
Signed-off-by: Jason Hall <jason@chainguard.dev>
* fix example in README
Signed-off-by: Jason Hall <jason@chainguard.dev>
---------
Signed-off-by: Jason Hall <jason@chainguard.dev>