-
Notifications
You must be signed in to change notification settings - Fork 241
feat(crons): initial cron support #661
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
Changes from all commits
81e5401
e0e2ccf
b75b2bb
666c5e7
c68637b
9172741
3c9fc2d
cb77d96
20974d2
5ec9e0b
73d3bdd
f674960
3c0381e
5c43228
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package sentry | ||
|
||
import "time" | ||
|
||
type CheckInStatus string | ||
|
||
const ( | ||
CheckInStatusInProgress CheckInStatus = "in_progress" | ||
CheckInStatusOK CheckInStatus = "ok" | ||
CheckInStatusError CheckInStatus = "error" | ||
) | ||
|
||
type checkInScheduleType string | ||
|
||
const ( | ||
checkInScheduleTypeCrontab checkInScheduleType = "crontab" | ||
checkInScheduleTypeInterval checkInScheduleType = "interval" | ||
) | ||
|
||
type MonitorSchedule interface { | ||
// scheduleType is a private method that must be implemented for monitor schedule | ||
// implementation. It should never be called. This method is made for having | ||
// specific private implementation of MonitorSchedule interface. | ||
scheduleType() checkInScheduleType | ||
} | ||
|
||
type crontabSchedule struct { | ||
Type string `json:"type"` | ||
Value string `json:"value"` | ||
} | ||
|
||
func (c crontabSchedule) scheduleType() checkInScheduleType { | ||
return checkInScheduleTypeCrontab | ||
} | ||
|
||
// CrontabSchedule defines the MonitorSchedule with a cron format. | ||
// Example: "8 * * * *". | ||
func CrontabSchedule(scheduleString string) MonitorSchedule { | ||
return crontabSchedule{ | ||
Type: string(checkInScheduleTypeCrontab), | ||
Value: scheduleString, | ||
} | ||
} | ||
|
||
type intervalSchedule struct { | ||
Type string `json:"type"` | ||
Value int64 `json:"value"` | ||
Unit string `json:"unit"` | ||
} | ||
|
||
func (i intervalSchedule) scheduleType() checkInScheduleType { | ||
tonyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return checkInScheduleTypeInterval | ||
} | ||
|
||
type MonitorScheduleUnit string | ||
|
||
const ( | ||
MonitorScheduleUnitMinute MonitorScheduleUnit = "minute" | ||
MonitorScheduleUnitHour MonitorScheduleUnit = "hour" | ||
MonitorScheduleUnitDay MonitorScheduleUnit = "day" | ||
MonitorScheduleUnitWeek MonitorScheduleUnit = "week" | ||
MonitorScheduleUnitMonth MonitorScheduleUnit = "month" | ||
MonitorScheduleUnitYear MonitorScheduleUnit = "year" | ||
) | ||
|
||
// IntervalSchedule defines the MonitorSchedule with an interval format. | ||
// | ||
// Example: | ||
// | ||
// IntervalSchedule(1, sentry.MonitorScheduleUnitDay) | ||
func IntervalSchedule(value int64, unit MonitorScheduleUnit) MonitorSchedule { | ||
return intervalSchedule{ | ||
Type: string(checkInScheduleTypeInterval), | ||
Value: value, | ||
Unit: string(unit), | ||
} | ||
} | ||
|
||
type MonitorConfig struct { //nolint: maligned // prefer readability over optimal memory layout | ||
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"` | ||
// 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"` | ||
// A tz database string representing the timezone which the monitor's execution schedule is in. | ||
// See: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones | ||
Timezone string `json:"timezone,omitempty"` | ||
} | ||
|
||
type CheckIn struct { //nolint: maligned // prefer readability over optimal memory layout | ||
// Check-In ID (unique and client generated) | ||
ID EventID `json:"check_in_id"` | ||
// The distinct slug of the monitor. | ||
MonitorSlug string `json:"monitor_slug"` | ||
// The status of the check-in. | ||
Status CheckInStatus `json:"status"` | ||
// The duration of the check-in. Will only take effect if the status is ok or error. | ||
Duration time.Duration `json:"duration,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. What I realized is that we don't have an option to pass an existing 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. Done. I might want to add a test case for this one, or an example. |
||
} | ||
|
||
// serializedCheckIn is used by checkInMarshalJSON method on Event struct. | ||
// See https://develop.sentry.dev/sdk/check-ins/ | ||
type serializedCheckIn struct { //nolint: maligned | ||
// Check-In ID (unique and client generated). | ||
CheckInID string `json:"check_in_id"` | ||
// The distinct slug of the monitor. | ||
MonitorSlug string `json:"monitor_slug"` | ||
// The status of the check-in. | ||
Status CheckInStatus `json:"status"` | ||
// The duration of the check-in in seconds. Will only take effect if the status is ok or error. | ||
Duration float64 `json:"duration,omitempty"` | ||
Release string `json:"release,omitempty"` | ||
Environment string `json:"environment,omitempty"` | ||
MonitorConfig *MonitorConfig `json:"monitor_config,omitempty"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -413,6 +413,12 @@ func (client *Client) CaptureException(exception error, hint *EventHint, scope E | |
return client.CaptureEvent(event, hint, scope) | ||
} | ||
|
||
// CaptureCheckIn captures a check in. | ||
func (client *Client) CaptureCheckIn(checkIn *CheckIn, monitorConfig *MonitorConfig, scope EventModifier) *EventID { | ||
Comment on lines
+416
to
+417
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. There was a discussion in Discord (#off-topic) between me and @cleptric about what function name to implement (as the PHP and Node implementation is quite different), where Michi suggested to follow the PHP implementation as the event is the check in already. As for Go SDK, if this implementation folows PHP's, this would be something like: event := &sentry.Event{
// fill payloads
}
event.SetCheckIn(checkInPayload)
eventId := sentry.CaptureEvent(event) It's kind of awkward and confusing for me, so I went ahead with the |
||
event := client.EventFromCheckIn(checkIn, monitorConfig) | ||
return client.CaptureEvent(event, nil, scope) | ||
} | ||
|
||
// CaptureEvent captures an event on the currently active client if any. | ||
// | ||
// The event must already be assembled. Typically code would instead use | ||
|
@@ -524,6 +530,30 @@ func (client *Client) EventFromException(exception error, level Level) *Event { | |
return event | ||
} | ||
|
||
// EventFromCheckIn creates a new Sentry event from the given `check_in` instance. | ||
func (client *Client) EventFromCheckIn(checkIn *CheckIn, monitorConfig *MonitorConfig) *Event { | ||
event := NewEvent() | ||
checkInID := EventID(uuid()) | ||
if checkIn != nil { | ||
if checkIn.ID != "" { | ||
checkInID = checkIn.ID | ||
} | ||
|
||
event.CheckIn = &CheckIn{ | ||
ID: checkInID, | ||
MonitorSlug: checkIn.MonitorSlug, | ||
Status: checkIn.Status, | ||
Duration: checkIn.Duration, | ||
} | ||
} | ||
event.MonitorConfig = monitorConfig | ||
|
||
// EventID should be equal to CheckInID | ||
event.EventID = checkInID | ||
|
||
return event | ||
} | ||
|
||
// reverse reverses the slice a in place. | ||
func reverse(a []Exception) { | ||
for i := len(a)/2 - 1; i >= 0; i-- { | ||
|
Uh oh!
There was an error while loading. Please reload this page.