-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sentry integration to workers (#46)
* feat: add sentry integration to worker * docs: add sentry alerter docs for worker
- Loading branch information
1 parent
f60ccbb
commit 373b9f4
Showing
6 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/hatchet-dev/hatchet/pkg/client" | ||
"github.com/hatchet-dev/hatchet/pkg/cmdutils" | ||
"github.com/hatchet-dev/hatchet/pkg/errors/sentry" | ||
"github.com/hatchet-dev/hatchet/pkg/worker" | ||
"github.com/joho/godotenv" | ||
) | ||
|
||
type userCreateEvent struct { | ||
Username string `json:"username"` | ||
UserId string `json:"user_id"` | ||
Data map[string]string `json:"data"` | ||
} | ||
|
||
type stepOneOutput struct { | ||
Message string `json:"message"` | ||
} | ||
|
||
func StepOne(ctx context.Context, input *userCreateEvent) (result *stepOneOutput, err error) { | ||
return nil, fmt.Errorf("this is an error") | ||
} | ||
|
||
func main() { | ||
err := godotenv.Load() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
client, err := client.New() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
sentryAlerter, err := sentry.NewSentryAlerter(&sentry.SentryAlerterOpts{ | ||
DSN: os.Getenv("SENTRY_DSN"), | ||
Environment: os.Getenv("SENTRY_ENVIRONMENT"), | ||
}) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Create a worker. This automatically reads in a TemporalClient from .env and workflow files from the .hatchet | ||
// directory, but this can be customized with the `worker.WithTemporalClient` and `worker.WithWorkflowFiles` options. | ||
w, err := worker.NewWorker( | ||
worker.WithClient( | ||
client, | ||
), | ||
worker.WithErrorAlerter(sentryAlerter), | ||
) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = w.On(worker.Event("user:create"), &worker.WorkflowJob{ | ||
Name: "failing-workflow", | ||
Description: "This is a failing workflow.", | ||
Steps: []worker.WorkflowStep{ | ||
{ | ||
Function: StepOne, | ||
}, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// err = worker.RegisterAction("echo:echo", func(ctx context.Context, input *actionInput) (result any, err error) { | ||
// return map[string]interface{}{ | ||
// "message": input.Message, | ||
// }, nil | ||
// }) | ||
|
||
// if err != nil { | ||
// panic(err) | ||
// } | ||
|
||
// err = worker.RegisterAction("echo:object", func(ctx context.Context, input *actionInput) (result any, err error) { | ||
// return nil, nil | ||
// }) | ||
|
||
// if err != nil { | ||
// panic(err) | ||
// } | ||
|
||
interruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan()) | ||
defer cancel() | ||
|
||
go func() { | ||
err = w.Start(interruptCtx) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
cancel() | ||
}() | ||
|
||
testEvent := userCreateEvent{ | ||
Username: "echo-test", | ||
UserId: "1234", | ||
Data: map[string]string{ | ||
"test": "test", | ||
}, | ||
} | ||
|
||
// push an event | ||
err = client.Event().Push( | ||
context.Background(), | ||
"user:create", | ||
testEvent, | ||
) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for { | ||
select { | ||
case <-interruptCtx.Done(): | ||
return | ||
default: | ||
time.Sleep(time.Second) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package sentry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/getsentry/sentry-go" | ||
) | ||
|
||
type SentryAlerter struct { | ||
client *sentry.Client | ||
} | ||
|
||
func noIntegrations(ints []sentry.Integration) []sentry.Integration { | ||
return []sentry.Integration{} | ||
} | ||
|
||
type SentryAlerterOpts struct { | ||
DSN string | ||
Environment string | ||
} | ||
|
||
func NewSentryAlerter(opts *SentryAlerterOpts) (*SentryAlerter, error) { | ||
sentryClient, err := sentry.NewClient(sentry.ClientOptions{ | ||
Dsn: opts.DSN, | ||
AttachStacktrace: true, | ||
Integrations: noIntegrations, | ||
Environment: opts.Environment, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SentryAlerter{ | ||
client: sentryClient, | ||
}, nil | ||
} | ||
|
||
func (s *SentryAlerter) SendAlert(ctx context.Context, err error, data map[string]interface{}) { | ||
scope := sentry.NewScope() | ||
|
||
for key, val := range data { | ||
scope.SetTag(key, fmt.Sprintf("%v", val)) | ||
} | ||
|
||
s.client.CaptureException( | ||
err, | ||
&sentry.EventHint{ | ||
Data: data, | ||
}, | ||
scope, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters