Skip to content

Commit

Permalink
Expand errors in ctxz (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Mar 8, 2024
1 parent b419518 commit 34ab06c
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gorelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions _examples/zzap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions _examples/zzap/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
15 changes: 14 additions & 1 deletion ctxz/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"

"github.com/bool64/ctxd"
"github.com/bool64/logz"
Expand Down Expand Up @@ -41,14 +42,26 @@ 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:]

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 = ""
}
Expand Down
8 changes: 6 additions & 2 deletions ctxz/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ 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()

logzpage.Handler(o.LevelObservers()...).ServeHTTP(rw, req)

assert.Contains(t, rw.Body.String(), `{
"errDetail": 321,
"error": "oops",
"foo": "bar",
"shared": 123
}`)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down
8 changes: 4 additions & 4 deletions observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 34ab06c

Please sign in to comment.