-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,2 +1,26 @@ | ||
# go-cron-sentry | ||
# Sentry Cron reporter | ||
|
||
Package to send reports to https://sentry.io/crons/ service. | ||
|
||
Handles stop, start and detects and sends crash too. | ||
|
||
For more information see https://docs.sentry.io/product/crons/ and https://docs.sentry.io/product/crons/getting-started/http/ | ||
|
||
|
||
## Example | ||
|
||
```go | ||
package main | ||
|
||
import "github.com/teamniteo/go-sentry/cron" | ||
|
||
var cronReport = cron.NewMonitor("teamniteo", "monitor-slug-or-uuid") | ||
|
||
func main() { | ||
cronReport.Start() | ||
defer cronReport.Stop() // will handle crash too | ||
|
||
// ... the rest of the stuff | ||
} | ||
|
||
``` |
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,52 @@ | ||
package cron | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/imroc/req" | ||
) | ||
|
||
type Cron struct { | ||
Team string | ||
DSN string | ||
Monitor string | ||
} | ||
|
||
func (c *Cron) header() req.Header { | ||
return req.Header{ | ||
"Content-Type": "application/json", | ||
"Authorization": fmt.Sprintf("DSN %s", c.DSN), | ||
} | ||
} | ||
|
||
func (c *Cron) url(verb string) string { | ||
return fmt.Sprintf("https://sentry.io/api/0/organizations/%s/monitors/%s/%s/", c.Team, c.Monitor, verb) | ||
} | ||
|
||
func NewMonitor(team, monitor string) Cron { | ||
sentryDSN := os.Getenv("SENTRY_DSN") | ||
|
||
return Cron{ | ||
Team: team, | ||
DSN: sentryDSN, | ||
Monitor: monitor, | ||
} | ||
} | ||
|
||
func (m *Cron) Start() error { | ||
_, err := req.Post(m.url("checkins"), m.header(), started.json()) | ||
return err | ||
} | ||
|
||
func (m *Cron) Stop() error { | ||
|
||
// handle crash | ||
if err := recover(); err != nil { | ||
req.Put(m.url("latest"), m.header(), errored.json()) | ||
return nil | ||
} | ||
|
||
_, err := req.Put(m.url("latest"), m.header(), finished.json()) | ||
return err | ||
} |
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,30 @@ | ||
package cron | ||
|
||
import "testing" | ||
|
||
func TestCron_url(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
fields Cron | ||
verb string | ||
want string | ||
}{ | ||
{"started", Cron{"team", "dsn", "monitor"}, "checkins", "https://sentry.io/api/0/organizations/team/monitors/monitor/checkins/"}, | ||
{"ok", Cron{"team", "dsn", "monitor"}, "latest", "https://sentry.io/api/0/organizations/team/monitors/monitor/latest/"}, | ||
{"errored", Cron{"team", "dsn", "monitor"}, "latest", "https://sentry.io/api/0/organizations/team/monitors/monitor/latest/"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := &Cron{ | ||
Team: tt.fields.Team, | ||
DSN: tt.fields.DSN, | ||
Monitor: tt.fields.Monitor, | ||
} | ||
if got := c.url(tt.verb); got != tt.want { | ||
t.Errorf("Cron.url() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,15 @@ | ||
package cron | ||
|
||
import "strings" | ||
|
||
type Report string | ||
|
||
const ( | ||
started Report = `{"status": "in_progress"}` | ||
finished Report = `{"status": "ok"}` | ||
errored Report = `{"status": "error"}` | ||
) | ||
|
||
func (r Report) json() *strings.Reader { | ||
return strings.NewReader(string(r)) | ||
} |
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,5 @@ | ||
module github.com/teamniteo/go-sentry | ||
|
||
go 1.20 | ||
|
||
require github.com/imroc/req v0.3.2 |
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,2 @@ | ||
github.com/imroc/req v0.3.2 h1:M/JkeU6RPmX+WYvT2vaaOL0K+q8ufL5LxwvJc4xeB4o= | ||
github.com/imroc/req v0.3.2/go.mod h1:F+NZ+2EFSo6EFXdeIbpfE9hcC233id70kf0byW97Caw= |