Skip to content

Commit

Permalink
feature: support Heartbeat checks [sc-16785] (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine-C committed Aug 3, 2023
1 parent 2836395 commit 68b8dd8
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bugs.md
examples/demo/demo
.vscode/
.DS_Store
.DS_Store
.idea
87 changes: 86 additions & 1 deletion checkly.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ func (c *client) CreateCheck(
var checkType string
if check.Type == "BROWSER" {
checkType = "checks/browser"
} else {
} else if check.Type == "API" {
checkType = "checks/api"
} else if check.Type == "HEARTBEAT" {
checkType = "checks/heartbeat"
}
status, res, err := c.apiCall(
ctx,
Expand All @@ -208,6 +210,33 @@ func (c *client) CreateCheck(
return &result, nil
}

func (c *client) CreateHeartbeat(
ctx context.Context,
check HeartbeatCheck,
) (*HeartbeatCheck, error) {
data, err := json.Marshal(check)
if err != nil {
return nil, err
}
status, res, err := c.apiCall(
ctx,
http.MethodPost,
withAutoAssignAlertsFlag("checks/heartbeat"),
data,
)
if err != nil {
return nil, err
}
if status != http.StatusCreated {
return nil, fmt.Errorf("unexpected response status %d: %q", status, res)
}
var result HeartbeatCheck
if err = json.NewDecoder(strings.NewReader(res)).Decode(&result); err != nil {
return nil, fmt.Errorf("decoding error for data %s: %v", res, err)
}
return &result, nil
}

// Update updates an existing check with the specified details. It returns the
// updated check, or an error.
func (c *client) UpdateCheck(
Expand Down Expand Up @@ -238,6 +267,36 @@ func (c *client) UpdateCheck(
return &result, nil
}

// Update updates an existing check with the specified details. It returns the
// updated check, or an error.
func (c *client) UpdateHeartbeat(
ctx context.Context,
ID string, check HeartbeatCheck,
) (*HeartbeatCheck, error) {
data, err := json.Marshal(check)
if err != nil {
return nil, err
}
status, res, err := c.apiCall(
ctx,
http.MethodPut,
withAutoAssignAlertsFlag(fmt.Sprintf("checks/heartbeat/%s", ID)),
data,
)
if err != nil {
return nil, err
}
if status != http.StatusOK {
return nil, fmt.Errorf("unexpected response status %d: %q", status, res)
}
var result HeartbeatCheck
err = json.NewDecoder(strings.NewReader(res)).Decode(&result)
if err != nil {
return nil, fmt.Errorf("decoding error for data %s: %v", res, err)
}
return &result, nil
}

// Delete deletes the check with the specified ID.
func (c *client) DeleteCheck(
ctx context.Context,
Expand Down Expand Up @@ -284,6 +343,32 @@ func (c *client) GetCheck(
return &result, nil
}

// Get takes the ID of an existing check, and returns the check parameters, or
// an error.
func (c *client) GetHeartbeatCheck(
ctx context.Context,
ID string,
) (*HeartbeatCheck, error) {
status, res, err := c.apiCall(
ctx,
http.MethodGet,
fmt.Sprintf("checks/%s", ID),
nil,
)
if err != nil {
return nil, err
}
if status != http.StatusOK {
return nil, fmt.Errorf("unexpected response status %d: %q", status, res)
}
result := HeartbeatCheck{}
err = json.NewDecoder(strings.NewReader(res)).Decode(&result)
if err != nil {
return nil, fmt.Errorf("decoding error for data %s: %v", res, err)
}
return &result, nil
}

// CreateGroup creates a new check group with the specified details. It returns
// the newly-created group, or an error.
func (c *client) CreateGroup(
Expand Down
20 changes: 20 additions & 0 deletions demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ var group = checkly.Group{
LocalTearDownScript: "teardown-test",
}

var heartbeatCheck = checkly.HeartbeatCheck{
Name: "My Heartbeat Check",
Activated: true,
Muted: false,
AlertSettings: alertSettings,
Heartbeat: checkly.Heartbeat{
Period: 30,
PeriodUnit: "seconds",
Grace: 0,
GraceUnit: "seconds",
},
}

func main() {
apiKey := os.Getenv("CHECKLY_API_KEY")
if apiKey == "" {
Expand Down Expand Up @@ -238,4 +251,11 @@ func main() {
}
fmt.Printf("New check created with ID %s\n", gotCheck.ID)
}

// HB
gotHbCheck, err := client.CreateHeartbeat(ctx, heartbeatCheck)
if err != nil {
log.Fatal(err)
}
fmt.Printf("New check created with ID %s\n", gotHbCheck.ID)
}
47 changes: 47 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,25 @@ type Client interface {
ID string,
) (*Check, error)

GetHeartbeatCheck(
ctx context.Context,
ID string,
) (*HeartbeatCheck, error)

// Create creates a new check with the specified details.
// It returns the newly-created check, or an error.
CreateCheck(
ctx context.Context,
check Check,
) (*Check, error)

// CreateHeartbeat creates a new heartbeat check with the specified details.
// It returns the newly-created check, or an error.
CreateHeartbeat(
ctx context.Context,
check HeartbeatCheck,
) (*HeartbeatCheck, error)

// Update updates an existing check with the specified details.
// It returns the updated check, or an error.
UpdateCheck(
Expand All @@ -67,6 +79,14 @@ type Client interface {
check Check,
) (*Check, error)

// UpdateHeartbeat updates an existing heartbeat check with the specified details.
// It returns the updated check, or an error.
UpdateHeartbeat(
ctx context.Context,
ID string,
check HeartbeatCheck,
) (*HeartbeatCheck, error)

// Delete deletes the check with the specified ID.
DeleteCheck(
ctx context.Context,
Expand Down Expand Up @@ -355,6 +375,9 @@ const TypeBrowser = "BROWSER"
// TypeAPI is used to identify an API check.
const TypeAPI = "API"

// TypeHeartbeat is used to identify a browser check.
const TypeHeartbeat = "HEARTBEAT"

// Escalation type constants

// RunBased identifies a run-based escalation type, for use with an AlertSettings.
Expand Down Expand Up @@ -430,6 +453,7 @@ type Check struct {
AlertSettings AlertSettings `json:"alertSettings,omitempty"`
UseGlobalAlertSettings bool `json:"useGlobalAlertSettings"`
Request Request `json:"request"`
Heartbeat Heartbeat `json:"heartbeat"`
GroupID int64 `json:"groupId,omitempty"`
GroupOrder int `json:"groupOrder,omitempty"`
AlertChannelSubscriptions []AlertChannelSubscription `json:"alertChannelSubscriptions,omitempty"`
Expand All @@ -444,6 +468,29 @@ type Check struct {
SSLCheck bool `json:"sslCheck"`
}

type HeartbeatCheck struct {
ID string `json:"id"`
Name string `json:"name"`
Activated bool `json:"activated"`
Muted bool `json:"muted"`
Tags []string `json:"tags,omitempty"`
AlertSettings AlertSettings `json:"alertSettings,omitempty"`
UseGlobalAlertSettings bool `json:"useGlobalAlertSettings"`
AlertChannelSubscriptions []AlertChannelSubscription `json:"alertChannelSubscriptions,omitempty"`
Heartbeat Heartbeat `json:"heartbeat"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

// Heartbeat represents the parameter for the heartbeat check.
type Heartbeat struct {
Period int `json:"period"`
PeriodUnit string `json:"periodUnit"`
Grace int `json:"grace"`
GraceUnit string `json:"graceUnit"`
PingToken string `json:"pingToken"`
}

// Request represents the parameters for the request made by the check.
type Request struct {
Method string `json:"method"`
Expand Down

0 comments on commit 68b8dd8

Please sign in to comment.