From 34ab06c5006991d6a2d34c8ddc081bfb44937dd4 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Fri, 8 Mar 2024 22:34:19 +0100 Subject: [PATCH] Expand errors in ctxz (#12) --- .github/workflows/bench.yml | 2 +- .github/workflows/golangci-lint.yml | 6 +++--- .github/workflows/gorelease.yml | 2 +- .github/workflows/test-unit.yml | 4 ++-- Makefile | 2 +- _examples/zzap/go.mod | 8 ++++---- _examples/zzap/go.sum | 4 ++++ ctxz/observer.go | 15 ++++++++++++++- ctxz/observer_test.go | 8 ++++++-- go.mod | 4 ++-- go.sum | 10 +++++----- observer.go | 8 ++++---- 12 files changed, 47 insertions(+), 26 deletions(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 51fbc24..f118ccd 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -21,7 +21,7 @@ env: GO111MODULE: "on" CACHE_BENCHMARK: "off" # Enables benchmark result reuse between runs, may skew latency results. RUN_BASE_BENCHMARK: "on" # Runs benchmark for PR base in case benchmark result is missing. - GO_VERSION: 1.21.x + GO_VERSION: 1.22.x jobs: bench: runs-on: ubuntu-latest diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 5e985cc..6239219 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -21,13 +21,13 @@ jobs: steps: - uses: actions/setup-go@v3 with: - go-version: 1.21.x + go-version: 1.22.x - uses: actions/checkout@v2 - name: golangci-lint - uses: golangci/golangci-lint-action@v3.7.0 + uses: golangci/golangci-lint-action@v4.0.0 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.55.2 + version: v1.56.2 # Optional: working directory, useful for monorepos # working-directory: somedir diff --git a/.github/workflows/gorelease.yml b/.github/workflows/gorelease.yml index 97ebe87..6356a9d 100644 --- a/.github/workflows/gorelease.yml +++ b/.github/workflows/gorelease.yml @@ -9,7 +9,7 @@ concurrency: cancel-in-progress: true env: - GO_VERSION: 1.21.x + GO_VERSION: 1.22.x jobs: gorelease: runs-on: ubuntu-latest diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index 4214222..9b6e93d 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -15,13 +15,13 @@ concurrency: env: GO111MODULE: "on" RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing. - COV_GO_VERSION: 1.21.x # Version of Go to collect coverage + COV_GO_VERSION: 1.22.x # Version of Go to collect coverage TARGET_DELTA_COV: 90 # Target coverage of changed lines, in percents jobs: test: strategy: matrix: - go-version: [ 1.19.x, 1.20.x, 1.21.x ] + go-version: [ 1.20.x, 1.21.x, 1.22.x ] runs-on: ubuntu-latest steps: - name: Install Go stable diff --git a/Makefile b/Makefile index 4027c74..79a4626 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -#GOLANGCI_LINT_VERSION := "v1.55.2" # Optional configuration to pinpoint golangci-lint version. +#GOLANGCI_LINT_VERSION := "v1.56.2" # Optional configuration to pinpoint golangci-lint version. # The head of Makefile determines location of dev-go to include standard targets. GO ?= go diff --git a/_examples/zzap/go.mod b/_examples/zzap/go.mod index 84f6378..55f7a5f 100644 --- a/_examples/zzap/go.mod +++ b/_examples/zzap/go.mod @@ -7,12 +7,12 @@ replace github.com/bool64/logz => ./../.. require ( github.com/bool64/logz v0.0.0-00010101000000-000000000000 github.com/drhodes/golorem v0.0.0-20160418191928-ecccc744c2d9 - go.uber.org/zap v1.24.0 + go.uber.org/zap v1.26.0 ) require ( - github.com/vearutop/dynhist-go v1.0.0 // indirect - github.com/vearutop/lograte v1.0.5 // indirect + github.com/vearutop/dynhist-go v1.2.3 // indirect + github.com/vearutop/lograte v1.1.3 // indirect go.uber.org/atomic v1.10.0 // indirect - go.uber.org/multierr v1.10.0 // indirect + go.uber.org/multierr v1.11.0 // indirect ) diff --git a/_examples/zzap/go.sum b/_examples/zzap/go.sum index e4b40d9..871de65 100644 --- a/_examples/zzap/go.sum +++ b/_examples/zzap/go.sum @@ -31,8 +31,10 @@ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/swaggest/usecase v1.2.0/go.mod h1:oc5+QoAxG3Et5Gl9lRXgEOm00l4VN9gdVQSMIa5EeLY= github.com/vearutop/dynhist-go v1.0.0 h1:SLYGcQMzmIoWnuqtF9lV8ULUBC735A56M5nL2CNth4c= github.com/vearutop/dynhist-go v1.0.0/go.mod h1:7Cgyu5Ww8FwdB+Y+zawRz9cQT5oXAxw294L9lQ+JI/k= +github.com/vearutop/dynhist-go v1.2.3/go.mod h1:liiiYiwAi8ixC3DbkxooEhASTF6ysJSXy+piCrBtxEg= github.com/vearutop/lograte v1.0.5 h1:H3XAhqjggYt0oyJ8AqzxHClKpJW0odmu3mYYMeLcvkQ= github.com/vearutop/lograte v1.0.5/go.mod h1:hN7Zo1dpCKPVEEkxPM3zKtZHpkxtOip0cSITUZKZAxg= +github.com/vearutop/lograte v1.1.3/go.mod h1:toX+le7NyZBmZ0Kw1ox78UOiDldXAkul2UEmgVKL5eQ= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= @@ -42,8 +44,10 @@ go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= +go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= diff --git a/ctxz/observer.go b/ctxz/observer.go index 78679a0..c73e338 100644 --- a/ctxz/observer.go +++ b/ctxz/observer.go @@ -5,6 +5,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "github.com/bool64/ctxd" "github.com/bool64/logz" @@ -41,7 +42,7 @@ func (t tuples) MarshalJSON() ([]byte, error) { ) for i, l := range kv { - if label == "" { + if label == "" { //nolint:nestif label, ok = l.(string) if !ok { m["malformedFields"] = kv[i:] @@ -49,6 +50,18 @@ func (t tuples) MarshalJSON() ([]byte, error) { break } } else { + if err, ok := l.(error); ok { + l = err.Error() + + var se ctxd.StructuredError + + if errors.As(err, &se) { + for k, v := range se.Fields() { + m[k] = v + } + } + } + m[label] = l label = "" } diff --git a/ctxz/observer_test.go b/ctxz/observer_test.go index f0976c5..ce5bc91 100644 --- a/ctxz/observer_test.go +++ b/ctxz/observer_test.go @@ -27,10 +27,12 @@ func TestNewObserver(t *testing.T) { o = o.WithLogger(ctxd.NoOpLogger{}) + err := ctxd.NewError(ctx, "oops", "errDetail", 321) + o.Warn(ctx, "warn", "foo", "bar") - o.Error(ctx, "error", "foo", "bar") + o.Error(ctx, "error", "foo", "bar", "error", err) - req, err := http.NewRequest(http.MethodGet, "/debug/logz?level=Info&msg=info", nil) + req, err := http.NewRequest(http.MethodGet, "/debug/logz?level=Error&msg=error", nil) require.NoError(t, err) rw := httptest.NewRecorder() @@ -38,6 +40,8 @@ func TestNewObserver(t *testing.T) { logzpage.Handler(o.LevelObservers()...).ServeHTTP(rw, req) assert.Contains(t, rw.Body.String(), `{ + "errDetail": 321, + "error": "oops", "foo": "bar", "shared": 123 }`) diff --git a/go.mod b/go.mod index 25aa070..d1f2fe5 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,11 @@ go 1.19 require ( github.com/bool64/ctxd v1.2.1 - github.com/bool64/dev v0.2.33 + github.com/bool64/dev v0.2.34 github.com/stretchr/testify v1.8.4 github.com/vearutop/dynhist-go v1.2.3 github.com/vearutop/lograte v1.1.3 - go.uber.org/zap v1.26.0 + go.uber.org/zap v1.27.0 ) require ( diff --git a/go.sum b/go.sum index 688dcd1..cc033b4 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/bool64/ctxd v1.2.1 h1:hARFteq0zdn4bwfmxLhak3fXFuvtJVKDH2X29VV/2ls= github.com/bool64/ctxd v1.2.1/go.mod h1:ZG6QkeGVLTiUl2mxPpyHmFhDzFZCyocr9hluBV3LYuc= -github.com/bool64/dev v0.2.33 h1:ETAcSa8H9w4talcCdSQCCnLX7PMHmuxdLcDl6TpSDj4= -github.com/bool64/dev v0.2.33/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= +github.com/bool64/dev v0.2.34 h1:P9n315P8LdpxusnYQ0X7MP1CZXwBK5ae5RZrd+GdSZE= +github.com/bool64/dev v0.2.34/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -15,11 +15,11 @@ github.com/vearutop/dynhist-go v1.2.3 h1:EIMWszSDm6b7zmqySgx8zW2qNctE3IXUJggGlDF github.com/vearutop/dynhist-go v1.2.3/go.mod h1:liiiYiwAi8ixC3DbkxooEhASTF6ysJSXy+piCrBtxEg= github.com/vearutop/lograte v1.1.3 h1:wgZege2tbCCEYHORI+vZbSJcmayd2HTfkfGnOBrOpYw= github.com/vearutop/lograte v1.1.3/go.mod h1:toX+le7NyZBmZ0Kw1ox78UOiDldXAkul2UEmgVKL5eQ= -go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= -go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/observer.go b/observer.go index af438c3..63a4d29 100644 --- a/observer.go +++ b/observer.go @@ -67,7 +67,7 @@ type PreparedObserver struct { } // Observer keeps track of messages. -// Deprecated: use NewObserver() to avoid lazy init. +// Use NewObserver() to create new instance. type Observer struct { Config PreparedObserver @@ -285,7 +285,7 @@ type Bucket struct { func (l *PreparedObserver) GetEntries() []Entry { result := make([]Entry, 0, atomic.LoadUint32(&l.count)) - l.entries.Range(func(key, value interface{}) bool { + l.entries.Range(func(_, value interface{}) bool { result = append(result, l.exportEntry(value.(*entry), false)) return true @@ -298,7 +298,7 @@ func (l *PreparedObserver) GetEntries() []Entry { func (l *PreparedObserver) GetEntriesWithSamples() []Entry { result := make([]Entry, 0, atomic.LoadUint32(&l.count)) - l.entries.Range(func(key, value interface{}) bool { + l.entries.Range(func(_, value interface{}) bool { result = append(result, l.exportEntry(value.(*entry), true)) return true @@ -311,7 +311,7 @@ func (l *PreparedObserver) GetEntriesWithSamples() []Entry { func (l *PreparedObserver) Find(msg string) Entry { var e Entry - l.entries.Range(func(key, value interface{}) bool { + l.entries.Range(func(_, value interface{}) bool { if value.(*entry).msg == msg { e = l.exportEntry(value.(*entry), true)