-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(crons): Pass correct event types, fix checkin_margin field, add example #674
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
00ba024
fix: Pass correct event type
tonyo 6912a79
fix tests
tonyo 88de68c
Merge remote-tracking branch 'origin/master' into tonyo/fix-crons-api…
tonyo 6310d5e
Add transport test
tonyo 7c1d53c
fix linter, do not record check-in IDs as last events
tonyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,7 @@ type MonitorConfig struct { //nolint: maligned // prefer readability over optima | |
Schedule MonitorSchedule `json:"schedule,omitempty"` | ||
// The allowed margin of minutes after the expected check-in time that | ||
// the monitor will not be considered missed for. | ||
CheckInMargin int64 `json:"check_in_margin,omitempty"` | ||
CheckInMargin int64 `json:"checkin_margin,omitempty"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this almost looks like a typo to me. Will raise with the Crons team. |
||
// The allowed duration in minutes that the monitor may be `in_progress` | ||
// for before being considered failed due to timeout. | ||
MaxRuntime int64 `json:"max_runtime,omitempty"` | ||
|
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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if this causes issues. In Laravel, we sent the monitor config along for all check-ins.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It sorta feels natural to me: when the monitor already exists (guaranteed by the first check-in), we don't have to pass in the monitor config anymore.