-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(crons): Pass correct event types, fix checkin_margin field, add e…
…xample (#674)
- Loading branch information
Showing
10 changed files
with
153 additions
and
51 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,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/getsentry/sentry-go" | ||
) | ||
|
||
func runTask(monitorSlug string, duration time.Duration, shouldFail bool) { | ||
checkinId := sentry.CaptureCheckIn( | ||
&sentry.CheckIn{ | ||
MonitorSlug: monitorSlug, | ||
Status: sentry.CheckInStatusInProgress, | ||
}, | ||
&sentry.MonitorConfig{ | ||
Schedule: sentry.CrontabSchedule("* * * * *"), | ||
MaxRuntime: 2, | ||
CheckInMargin: 1, | ||
}, | ||
) | ||
task := fmt.Sprintf("Task[monitor_slug=%s,id=%s]", monitorSlug, *checkinId) | ||
fmt.Printf("Task started: %s\n", task) | ||
|
||
time.Sleep(duration) | ||
|
||
var status sentry.CheckInStatus | ||
if shouldFail { | ||
status = sentry.CheckInStatusError | ||
} else { | ||
status = sentry.CheckInStatusOK | ||
} | ||
|
||
sentry.CaptureCheckIn( | ||
&sentry.CheckIn{ | ||
ID: *checkinId, | ||
MonitorSlug: monitorSlug, | ||
Status: status, | ||
}, | ||
nil, | ||
) | ||
fmt.Printf("Task finished: %s; Status: %s\n", task, status) | ||
} | ||
|
||
func main() { | ||
_ = sentry.Init(sentry.ClientOptions{ | ||
Dsn: "", | ||
Debug: true, | ||
}) | ||
|
||
// Start a task that runs every minute and always succeeds | ||
go func() { | ||
for { | ||
go runTask("sentry-go-periodic-task-success", time.Second, false) | ||
time.Sleep(time.Minute) | ||
} | ||
}() | ||
|
||
time.Sleep(3 * time.Second) | ||
|
||
// Start a task that runs every minute and fails every second time | ||
go func() { | ||
shouldFail := true | ||
for { | ||
go runTask("sentry-go-periodic-task-sometimes-fail", 2*time.Second, shouldFail) | ||
time.Sleep(time.Minute) | ||
shouldFail = !shouldFail | ||
} | ||
}() | ||
|
||
select {} | ||
} |
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
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
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 |
---|---|---|
|
@@ -280,6 +280,36 @@ func TestEnvelopeFromTransactionWithProfile(t *testing.T) { | |
} | ||
} | ||
|
||
func TestEnvelopeFromCheckInEvent(t *testing.T) { | ||
event := newTestEvent(checkInType) | ||
event.CheckIn = &CheckIn{ | ||
MonitorSlug: "test-slug", | ||
ID: "123c5be4d31e48959103a1f878a1efcb", | ||
Status: CheckInStatusOK, | ||
} | ||
event.MonitorConfig = &MonitorConfig{ | ||
Schedule: IntervalSchedule(1, MonitorScheduleUnitHour), | ||
CheckInMargin: 10, | ||
MaxRuntime: 5000, | ||
Timezone: "Asia/Singapore", | ||
} | ||
sentAt := time.Unix(0, 0).UTC() | ||
|
||
body := getRequestBodyFromEvent(event) | ||
b, err := envelopeFromBody(event, newTestDSN(t), sentAt, body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
got := b.String() | ||
want := `{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sent_at":"1970-01-01T00:00:00Z","dsn":"http://[email protected]/sentry/1","sdk":{"name":"sentry.go","version":"0.0.1"}} | ||
{"type":"check_in","length":232} | ||
{"check_in_id":"123c5be4d31e48959103a1f878a1efcb","monitor_slug":"test-slug","status":"ok","monitor_config":{"schedule":{"type":"interval","value":1,"unit":"hour"},"checkin_margin":10,"max_runtime":5000,"timezone":"Asia/Singapore"}} | ||
` | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("Envelope mismatch (-want +got):\n%s", diff) | ||
} | ||
} | ||
|
||
func TestGetRequestFromEvent(t *testing.T) { | ||
testCases := []struct { | ||
testName string | ||
|