Skip to content

Commit

Permalink
fix: sentry dont receive errors event (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys authored Jul 5, 2022
1 parent f1a54c1 commit c29f456
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var apiCmd = &cobra.Command{
}

sentryHandler := sentryhttp.New(sentryhttp.Options{
Repanic: true,
Repanic: false,
WaitForDelivery: true,
})

Expand Down
11 changes: 0 additions & 11 deletions internal/api/api.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package api

import (
"context"
"fmt"
"os"

"entgo.io/ent/dialect/sql"
Expand Down Expand Up @@ -303,13 +302,3 @@ func (r *Resolver) User() apigen.UserResolver { return &userResolver{r} }
type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
type userResolver struct{ *Resolver }

// !!! WARNING !!!
// The code below was going to be deleted when updating resolvers. It has been copied here so you have
// one last chance to move it out of harms way if you want. There are two reasons this happens:
// - When renaming or deleting a resolver the old code will be put in here. You can safely delete
// it when you're done.
// - You have helper methods in this file. Move them out to keep these resolver files clean.
func (r *queryResolver) MyFollowings(ctx context.Context) ([]*generated.User, error) {
panic(fmt.Errorf("not implemented"))
}
2 changes: 1 addition & 1 deletion internal/jwtks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ServeHTTP(port *string) error {
}).Handler)

sentryHandler := sentryhttp.New(sentryhttp.Options{
Repanic: true,
Repanic: false,
WaitForDelivery: true,
})

Expand Down
1 change: 0 additions & 1 deletion internal/models/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func WithTx(ctx context.Context, client *modelgen.Client, fn func(tx *modelgen.T
if v := recover(); v != nil {
tx.Rollback()
sentry.CaptureException(v.(error))
panic(v)
}
}()
if err := fn(tx); err != nil {
Expand Down
25 changes: 25 additions & 0 deletions internal/webhooks/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package webhooks
import (
"context"
"encoding/json"
"errors"
"os"

"github.com/getsentry/sentry-go"
Expand All @@ -19,6 +20,11 @@ type processor struct {
ctx context.Context
}

// ErrInvalidWebhook is returned when the webhook is invalid and cannot be processed
// by the processor due to missing metadata key
var ErrInvalidWebhook = errors.New("invalid webhook, metadata is empty")

// New creates a new webhooks processor instance
func New() *processor {
if err := modelsutils.Connect(); err != nil {
log.Fatal().Err(err).Msg("failed to connect to database")
Expand All @@ -30,6 +36,10 @@ func New() *processor {
}
}

// Serve starts the webhooks processor and listens for incoming message from
// the RabbitMQ queue "webhooks-processor" filled by the webhooked project.
//
// See https://github.com/42Atomys/webhooked
func (p *processor) Serve(amqpUrl, channel string) error {
conn, err := amqp.Dial(amqpUrl)
if err != nil {
Expand Down Expand Up @@ -70,12 +80,19 @@ func (p *processor) Serve(amqpUrl, channel string) error {
err := p.handler(d.Body)
if err != nil {
sentry.CaptureException(err)

if errors.Is(err, ErrInvalidWebhook) {
goto ACK
}

if err = d.Nack(false, true); err != nil {
sentry.CaptureException(err)
log.Error().Err(err).Msg("Cannot nack the message")
}
continue
}

ACK:
if err := d.Ack(false); err != nil {
log.Error().Err(err).Msg("Cannot ack the message")
sentry.CaptureException(err)
Expand All @@ -85,14 +102,22 @@ func (p *processor) Serve(amqpUrl, channel string) error {
return nil
}

// handler is the main function that processes the webhooks. It parses the
// webhook metadata and calls the appropriate processor function.
// This function is called by the Serve function on each incoming message.
func (p *processor) handler(data []byte) error {
md := &duoapi.Webhook{}

if err := json.Unmarshal(data, &md); err != nil {
log.Error().Err(err).Msg("Failed to unmarshal webhook metadata")
}

if md == nil || md.Metadata == nil {
log.Error().Str("payload", string(data)).Msg("Webhook metadata is nil")
return ErrInvalidWebhook
}
log.Debug().Msgf("Received a message(%s.%s): %+v", md.Metadata.Model, md.Metadata.Event, md.Payload)

var err error
switch md.Metadata.Model {
case "location":
Expand Down
34 changes: 23 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ THE SOFTWARE.
package main

import (
"errors"
"os"
"time"

"github.com/getsentry/sentry-go"
"github.com/rs/zerolog"
Expand All @@ -36,32 +38,38 @@ func init() {
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

if os.Getenv("GO_ENV") != "" &&
os.Getenv("GO_ENV") != "development" &&
os.Getenv("SENTRY_DSN") != "" {
initSentry()
}
}

func main() {
func() {
defer sentry.Recover()
cmd.Execute()
defer func() {
err := recover()

if err != nil {
sentry.CurrentHub().Recover(err)
sentry.Flush(time.Second * 5)
}
}()

// Sentry needs to be in main for obscur reason...
if os.Getenv("GO_ENV") != "development" || os.Getenv("SENTRY_DSN") != "" {
initSentry()
}
cmd.Execute()
}

/**
* Initialize sentry client
* https://docs.sentry.io/platforms/go/
*/
func initSentry() {
var tracesSampleRate = 0.0
var tracesSampleRate, sampleSampleRate = 0.0, 1.0
var env = "development"
var release = "stud42@dev"

if os.Getenv("GO_ENV") == "production" {
tracesSampleRate = 0.2
sampleSampleRate = 1.0

env = "production"
release = "stud42@" + os.Getenv("APP_VERSION")

Expand All @@ -76,10 +84,14 @@ func initSentry() {
Environment: env,
Release: release,
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
for _, frame := range event.Exception {
log.Warn().Err(errors.New(frame.Value)).Str("eventID", string(event.EventID)).Msg("send exception to sentry")
event.Tags["eventID"] = string(event.EventID)
}
return event
},
TracesSampleRate: tracesSampleRate,
SampleRate: tracesSampleRate,
SampleRate: sampleSampleRate,
AttachStacktrace: true,
})
if err != nil {
Expand Down

0 comments on commit c29f456

Please sign in to comment.