1
0
Fork 0
mirror of https://github.com/chainguard-dev/clog synced 2026-07-17 06:23:22 +00:00
Commit graph

24 commits

Author SHA1 Message Date
Kenny Leung
1a2666f783
setup dependabot for clog (#18)
Signed-off-by: Kenny Leung <kleung@chainguard.dev>
2024-06-21 09:00:44 +02:00
Billy Lynch
40901e8d69
slogtest: Allow passing in logging config options. (#12)
- Enables debug logging by default.
- Adds `TestLoggerWithOptions` to allow overriding default config.
- Adds `Context` as a shorthand for TestContextWithLogger
   (e.g. `slogtest.TestContextWithLogger(t)` -> `slogtest.Context(t)`)
2024-06-20 20:49:22 -07:00
Nghia Tran
6962f14d44
Use traceparent instead of X-Cloud-Trace-Context (#16)
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.
2024-06-18 19:32:50 -07:00
Nghia Tran
f300b26592
Preserve context in With() calls (#17)
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>
2024-06-18 20:33:25 -04:00
Jason Hall
cb3093988f
gcp/trace: limit project ID lookups, add tests (#14)
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>
2024-02-12 21:04:06 -05:00
Billy Lynch
f4145d29d4
Preserve context in logger when calling FromContext. (#11)
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.
2024-02-06 12:35:04 -05:00
Jason Hall
4d1a9405b3
add Fatal{Context}{f} (#8)
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>
2024-02-06 11:03:48 -05:00
Jason Hall
83244dc767
add a flag option (#10)
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>
2024-02-06 11:01:13 -05:00
Jason Hall
d34ebd4160
add GCP optimized slog package (#9)
Opening this to gather feedback.

This moves https://github.com/imjasonh/gcpslog into this repo, including
https://github.com/imjasonh/gcpslog/pull/2

---------

Signed-off-by: Jason Hall <jason@chainguard.dev>
2024-02-05 17:09:57 -05:00
Jason Hall
fec25b637d
Update README.md (#7)
Signed-off-by: Jason Hall <jason@chainguard.dev>
2024-01-18 11:37:43 -05:00
Jason Hall
04bee692f7
rename to clog (#6)
* 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>
2024-01-16 13:28:27 -05:00
Billy Lynch
c071a43a42
Fix handler example to actually use the handler. (#4) 2024-01-16 11:15:36 -05:00
Billy Lynch
988f6253cd
Add source.yaml (#3) 2023-12-21 17:59:27 +01:00
Carlos Tadeu Panato Junior
7a6e5ac694
update to chainguard-dev (#2) 2023-12-21 11:54:22 -05:00
Billy Lynch
9bf46567a6
Create go.yml 2023-12-12 20:40:15 -05:00
Billy Lynch
0d562667b2
Update godoc 2023-12-12 19:59:57 -05:00
Billy Lynch
3f156fd3a4
Add missing slog.Logger functions.
- WithGroup
- Handler
2023-12-12 19:22:50 -05:00
Billy Lynch
765cf08124
Add slogtest helpers. 2023-12-12 18:45:04 -05:00
Billy Lynch
8aac927af2
Add godoc badge 2023-12-12 18:30:25 -05:00
Billy Lynch
5565dbc976
Add context logger.
This is heavily inspired by https://pkg.go.dev/knative.dev/pkg/logging

Also adds top level logging methods for convinience.
2023-12-12 18:29:22 -05:00
Billy Lynch
81392732b8
Update README 2023-12-12 14:32:28 -05:00
Billy Lynch
85b981e4b6
Add glog/zap style logging handlers. 2023-12-12 14:31:30 -05:00
Billy Lynch
4da9c60893
Initial commit 2023-12-06 16:31:14 -07:00
Billy Lynch
2bf1735ac5
Initial commit 2023-12-06 16:21:07 -07:00