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

fix: improve logging errors #508

Merged
merged 2 commits into from Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions docs/hub/debug.md
@@ -1,7 +1,7 @@
# Debug the Mercure.rocks Hub

The hub is shipped with [`pprof`](https://blog.golang.org/pprof),
a profiler allowing to find bottlenecks, memory leaks and blocked goroutines
a profiler allowing to find bottlenecks, memory leaks and blocked goroutines
among other things.

To enable the profiler, add the `debug` global directive to your `Caddyfile`:
Expand All @@ -19,14 +19,16 @@ You can use [the `pprof` tool](https://golang.org/pkg/net/http/pprof/) to visual

## Examples

Tip: type `web` when in interative mode to display a visual representation of the profile.

Look at the heap profile:

go tool pprof http://localhost:2019/debug/pprof/heap
```console
go tool pprof -http=:8080 http://localhost:2019/debug/pprof/heap
```

Look at the past memory allocations:

go tool pprof http://localhost:2019/debug/pprof/allocs
```console
go tool pprof -http=:8080 http://localhost:2019/debug/pprof/allocs
```

See `http://localhost:2019/debug/pprof/` for the list of available data.
2 changes: 2 additions & 0 deletions log.go
Expand Up @@ -2,6 +2,8 @@ package mercure

import "go.uber.org/zap/zapcore"

// stringArray has been copied from https://github.com/uber-go/zap/blob/master/array.go#L250-L257
// Copyright (c) 2016 Uber Technologies, Inc.
type stringArray []string

func (ss stringArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
Expand Down
9 changes: 7 additions & 2 deletions subscriber.go
@@ -1,6 +1,7 @@
package mercure

import (
"fmt"
"net/url"
"sync"

Expand Down Expand Up @@ -229,10 +230,14 @@ func (s *Subscriber) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("remote_addr", s.RemoteAddr)
}
if s.TopicSelectors != nil {
enc.AddArray("topic_selectors", stringArray(s.TopicSelectors))
if err := enc.AddArray("topic_selectors", stringArray(s.TopicSelectors)); err != nil {
return fmt.Errorf("log error: %w", err)
}
}
if s.Topics != nil {
enc.AddArray("topics", stringArray(s.Topics))
if err := enc.AddArray("topics", stringArray(s.Topics)); err != nil {
return fmt.Errorf("log error: %w", err)
}
}

return nil
Expand Down
6 changes: 5 additions & 1 deletion update.go
@@ -1,6 +1,8 @@
package mercure

import (
"fmt"

"github.com/gofrs/uuid"
"go.uber.org/zap/zapcore"
)
Expand All @@ -25,7 +27,9 @@ func (u *Update) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("id", u.ID)
enc.AddString("type", u.Type)
enc.AddUint64("retry", u.Retry)
enc.AddArray("topics", stringArray(u.Topics))
if err := enc.AddArray("topics", stringArray(u.Topics)); err != nil {
return fmt.Errorf("log error: %w", err)
}
enc.AddBool("private", u.Private)

if u.Debug {
Expand Down