Skip to content

Commit

Permalink
Merge pull request #508 from dunglas/fix/logging-error-handling
Browse files Browse the repository at this point in the history
fix: improve logging errors
  • Loading branch information
dunglas authored Apr 29, 2021
2 parents cb62f99 + 2fd25af commit 764e87c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
12 changes: 7 additions & 5 deletions docs/hub/debug.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 764e87c

Please sign in to comment.