Skip to content

Commit

Permalink
fix(sentry): use log message when capturing the event
Browse files Browse the repository at this point in the history
to avoid grouping unrelated errors in Sentry, we must never create a new error

BC: the sentry writer has been simplified and now needs less dependencies to fully operate
  • Loading branch information
damianopetrungaro committed Sep 10, 2022
1 parent 1e12f06 commit d3d7a50
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 192 deletions.
2 changes: 1 addition & 1 deletion .github/release/release.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "2.1.0"
"version": "1.11.0"
}
31 changes: 13 additions & 18 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,46 @@
# [2.1.0](https://github.com/damianopetrungaro/golog/compare/v2.0.0...v2.1.0) (2022-09-10)
# [1.11.0](https://github.com/damianopetrungaro/golog/compare/v1.10.0...v1.11.0) (2022-08-22)


### Features

* **test:** add a fake logger with matcher APIs ([cbd0c72](https://github.com/damianopetrungaro/golog/commit/cbd0c72d364eb0af43b07a6967f67505dde21e8d))
* **encoder:** Add coloured level option to text encoder ([9ae33c2](https://github.com/damianopetrungaro/golog/commit/9ae33c2c8ebc38d9a269adb86c267c7ded63af3f))
* **encoder:** Add LevelFormatter to TextEncoder config ([f663020](https://github.com/damianopetrungaro/golog/commit/f6630206224bcd205d281185bb217be67f1eba3b))



# [2.0.0](https://github.com/damianopetrungaro/golog/compare/v1.11.0...v2.0.0) (2022-09-10)
# [1.10.0](https://github.com/damianopetrungaro/golog/compare/v1.9.3...v1.10.0) (2022-08-04)


### Features

* add presets ([5f5dc45](https://github.com/damianopetrungaro/golog/commit/5f5dc45d07d698591a47e5e50c28bf83228853ee))


### BREAKING CHANGES

* The API for the text encoder now adds a default text encoder which adds colors
* **datadog:** add dd tracing decorator ([3101db1](https://github.com/damianopetrungaro/golog/commit/3101db1689596c59b5649141b5d7e44b353fe998))



# [1.11.0](https://github.com/damianopetrungaro/golog/compare/v1.10.0...v1.11.0) (2022-08-22)
## [1.9.3](https://github.com/damianopetrungaro/golog/compare/v1.9.2...v1.9.3) (2022-07-28)


### Features
### Bug Fixes

* **encoder:** Add coloured level option to text encoder ([9ae33c2](https://github.com/damianopetrungaro/golog/commit/9ae33c2c8ebc38d9a269adb86c267c7ded63af3f))
* **encoder:** Add LevelFormatter to TextEncoder config ([f663020](https://github.com/damianopetrungaro/golog/commit/f6630206224bcd205d281185bb217be67f1eba3b))
* **http:** use underscore instead of white space in the default fields ([be87e7f](https://github.com/damianopetrungaro/golog/commit/be87e7fde3315a25f33bdddea9c55c1bac641b44))



# [1.10.0](https://github.com/damianopetrungaro/golog/compare/v1.9.3...v1.10.0) (2022-08-04)
## [1.9.2](https://github.com/damianopetrungaro/golog/compare/v1.9.1...v1.9.2) (2022-06-30)


### Features
### Bug Fixes

* **datadog:** add dd tracing decorator ([3101db1](https://github.com/damianopetrungaro/golog/commit/3101db1689596c59b5649141b5d7e44b353fe998))
* **sentry:** writer concurrent safe ([1519590](https://github.com/damianopetrungaro/golog/commit/15195909b0374476f1fa9075389492cb861b7c1f))



## [1.9.3](https://github.com/damianopetrungaro/golog/compare/v1.9.2...v1.9.3) (2022-07-28)
## [1.9.1](https://github.com/damianopetrungaro/golog/compare/v1.9.0...v1.9.1) (2022-06-10)


### Bug Fixes

* **http:** use underscore instead of white space in the default fields ([be87e7f](https://github.com/damianopetrungaro/golog/commit/be87e7fde3315a25f33bdddea9c55c1bac641b44))
* **opentelemtery:** change package name ([c8adc6f](https://github.com/damianopetrungaro/golog/commit/c8adc6f78fe65682ae5b1a5d81e879895564b607))



74 changes: 0 additions & 74 deletions cover.out.tmp

This file was deleted.

57 changes: 27 additions & 30 deletions sentry/sentry.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
package sentry

import (
"bytes"
"errors"
"fmt"
"context"

"github.com/getsentry/sentry-go"

"github.com/damianopetrungaro/golog"
)

type Writer struct {
Encoder golog.Encoder
Hub *sentry.Hub
ErrHandler golog.ErrorHandler
DefaultLevel golog.Level
CaptureExceptionFromLevel golog.Level
Hub *sentry.Hub
DefaultLevel golog.Level
}

func (w *Writer) WriteEntry(e golog.Entry) {
wTo, err := w.Encoder.Encode(e)
if err != nil {
w.ErrHandler(fmt.Errorf("%w: sentry writer on encoding: %s", golog.ErrEntryNotWritten, err))
return
}

buf := &bytes.Buffer{}
if _, err := wTo.WriteTo(buf); err != nil {
w.ErrHandler(fmt.Errorf("%w: sentry writer on write to: %s", golog.ErrEntryNotWritten, err))
return
ev := sentry.NewEvent()
ev.Level = toSentryLevel(e.Level())
ev.Message = e.Message()
for _, f := range e.Fields() {
ev.Extra[f.Key()] = f.Value()
}

hub := w.Hub.Clone()
if e.Level() >= w.CaptureExceptionFromLevel {
hub.CaptureException(errors.New(buf.String()))
return
}
hub.CaptureEvent(ev)
}

hub.CaptureMessage(buf.String())
func (w *Writer) Write(msg []byte) (int, error) {
e := golog.NewStdEntry(context.Background(), w.DefaultLevel, string(msg), golog.Fields{})
w.WriteEntry(e)

return len(msg), nil
}

func (w *Writer) Write(msg []byte) (int, error) {
hub := w.Hub.Clone()
if w.DefaultLevel >= w.CaptureExceptionFromLevel {
hub.CaptureException(errors.New(string(msg)))
return len(msg), nil
func toSentryLevel(lvl golog.Level) sentry.Level {
switch lvl {
case golog.DEBUG:
return sentry.LevelDebug
case golog.INFO:
return sentry.LevelInfo
case golog.WARN:
return sentry.LevelWarning
case golog.ERROR:
return sentry.LevelError
case golog.FATAL:
return sentry.LevelFatal
}

hub.CaptureMessage(string(msg))
return len(msg), nil
return sentry.LevelError
}
Loading

0 comments on commit d3d7a50

Please sign in to comment.