Skip to content

Commit

Permalink
Add package
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Apr 17, 2023
1 parent db12bbe commit 1f0f4ae
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 1 deletion.
26 changes: 25 additions & 1 deletion README.md
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
}

```
52 changes: 52 additions & 0 deletions cron/cron.go
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
}
30 changes: 30 additions & 0 deletions cron/cron_test.go
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)
}
})
}
}
15 changes: 15 additions & 0 deletions cron/report.go
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))
}
5 changes: 5 additions & 0 deletions go.mod
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
2 changes: 2 additions & 0 deletions go.sum
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=

0 comments on commit 1f0f4ae

Please sign in to comment.