From 292b2282aafc27983b59e300f1405de80b707c65 Mon Sep 17 00:00:00 2001 From: Keefer Taylor | Tessellated Date: Thu, 21 Dec 2023 16:11:48 -0800 Subject: [PATCH 1/5] fix logging --- health/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/health/client.go b/health/client.go index c8ac27a..1bd96f2 100644 --- a/health/client.go +++ b/health/client.go @@ -25,7 +25,7 @@ type healthClient struct { var _ HealthClient = (*healthClient)(nil) func NewHealthClient(logger *log.Logger, pingKey string, createNewChecks bool) HealthClient { - prefixedLogger := logger.ApplyPrefix("[HEALTHCHECKS] 🩺") + prefixedLogger := logger.ApplyPrefix(" [HEALTHCHECKS] 🩺") client := &http.Client{ Timeout: 10 * time.Second, From 5037039443b4f9da7dcf57b356edc6ecb462f70a Mon Sep 17 00:00:00 2001 From: Keefer Taylor | Tessellated Date: Fri, 16 Aug 2024 15:47:47 -0700 Subject: [PATCH 2/5] wip --- .DS_Store | Bin 0 -> 6148 bytes health/client.go | 85 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fe130575a8539d7fca50695caacf0d771591e741 GIT binary patch literal 6148 zcmeHKu}%U(5S^6+F4~Z2VR@~Zn9$mIBDOZR7IK~e639_Ne+8Dto|sr#_ycw(CYE-7 zf*)XQVSKZ@hFuOUiJF;Y_ub9T+`N0_c32`(jZs=Bsu59$!5FM!nq$1qqhKr6vjJ56 zj9$Bba-Q`Eg-CIT0;0fgRDi$TfOe@(T^f4duS&=6T0d(xlU|m<7f$Z?-YzH42mX5c z<#62q*C>}E_G-`iC3LU>%|C{!(-+3dTXpX%si)`O_?l?^7!cekIIyQN0)k( z`dpZ-yXx}Td8!_68y-%+%VB}|E#?MI zIw@Tl&#{%2J)tOF9sWp%lZp+>EDDGMz5*q4TjTzJwD|n*C&`^CAPW2|1ys;HX*Tdm zwzn2uj(cr{afZRhak)XAg2}dHeZj4G6GI!;BEA5|7ITB}z~qmBmO&;_V5tgx0TRrh ArT_o{ literal 0 HcmV?d00001 diff --git a/health/client.go b/health/client.go index 1bd96f2..975a914 100644 --- a/health/client.go +++ b/health/client.go @@ -1,6 +1,8 @@ package health import ( + "bytes" + "encoding/json" "fmt" "io" "net/http" @@ -12,11 +14,24 @@ import ( type HealthClient interface { SendSuccess(slug string) error SendFailure(slug string) error + SendFailureWithErr(slug string, err error) error +} + +type CreateCheckPayload struct { + Slug string `json:"slug"` + Timeout int `json:"timeout"` + Grace int `json:"grace"` + Unique []string `json:"unique"` + Channels string `json:"channels"` + ApiKey string `json:"api_key"` } type healthClient struct { - pingKey string - createNewChecks bool + pingKey string + apiKey string + createNewChecks bool + timeoutSeconds int + gracePeriodSeconds int client *http.Client logger *log.Logger @@ -24,7 +39,7 @@ type healthClient struct { var _ HealthClient = (*healthClient)(nil) -func NewHealthClient(logger *log.Logger, pingKey string, createNewChecks bool) HealthClient { +func NewHealthClient(logger *log.Logger, apiKey, pingKey string, createNewChecks bool, timeoutSeconds, gracePeriodSeconds int) HealthClient { prefixedLogger := logger.ApplyPrefix(" [HEALTHCHECKS] 🩺") client := &http.Client{ @@ -32,10 +47,13 @@ func NewHealthClient(logger *log.Logger, pingKey string, createNewChecks bool) H } return &healthClient{ - pingKey: pingKey, - createNewChecks: createNewChecks, - client: client, - logger: prefixedLogger, + pingKey: pingKey, + apiKey: apiKey, + createNewChecks: createNewChecks, + client: client, + logger: prefixedLogger, + timeoutSeconds: timeoutSeconds, + gracePeriodSeconds: gracePeriodSeconds, } } @@ -44,8 +62,14 @@ func NewHealthClient(logger *log.Logger, pingKey string, createNewChecks bool) H func (hc *healthClient) SendSuccess(slug string) error { hc.logger.Info().Str("slug", slug).Msg("sending success") - shouldCreateNewChecks := hc.createNewChecksValue() - url := fmt.Sprintf("https://hc-ping.com/%s/%s?create=%d", hc.pingKey, slug, shouldCreateNewChecks) + if hc.createNewChecks { + err := hc.UpsertCheck(slug) + if err != nil { + return err + } + } + + url := fmt.Sprintf("https://hc-ping.com/%s/%s", hc.pingKey, slug) resp, err := hc.client.Get(url) if err != nil { @@ -63,10 +87,19 @@ func (hc *healthClient) SendSuccess(slug string) error { } func (hc *healthClient) SendFailure(slug string) error { + return hc.SendFailureWithErr(slug, nil) +} + +func (hc *healthClient) SendFailureWithErr(slug string, err error) error { hc.logger.Info().Str("slug", slug).Msg("sending failure") - shouldCreateNewChecks := hc.createNewChecksValue() - url := fmt.Sprintf("https://hc-ping.com/%s/%s/fail?create=%d", hc.pingKey, slug, shouldCreateNewChecks) + if hc.createNewChecks { + err := hc.UpsertCheck(slug) + if err != nil { + return err + } + } + url := fmt.Sprintf("https://hc-ping.com/%s/%s/fail", hc.pingKey, slug) resp, err := hc.client.Get(url) if err != nil { @@ -83,12 +116,30 @@ func (hc *healthClient) SendFailure(slug string) error { return nil } -// Private methods +func (hc *healthClient) UpsertCheck(slug string) error { + url := "https://healthchecks.io/api/v3/checks/" + createCheckPayload := &CreateCheckPayload{ + Slug: slug, + Timeout: hc.timeoutSeconds, + Grace: hc.gracePeriodSeconds, + Unique: []string{"name", "slug"}, + Channels: "*", + ApiKey: hc.apiKey, + } -func (hc *healthClient) createNewChecksValue() int { - if hc.createNewChecks { - return 1 - } else { - return 0 + payload, err := json.Marshal(createCheckPayload) + if err != nil { + return err + } + + resp, err := hc.client.Post(url, "application/json", bytes.NewBuffer(payload)) + if err != nil { + return err } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusCreated { + return fmt.Errorf("unexpected code from HTTP to %s: HTTP %d", url, resp.StatusCode) + } + return nil } From 1d20678a1bb24d14ca72f3b89a6e69518d1dc902 Mon Sep 17 00:00:00 2001 From: Keefer Taylor | Tessellated Date: Fri, 16 Aug 2024 15:55:09 -0700 Subject: [PATCH 3/5] get upsert working --- health/client.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/health/client.go b/health/client.go index 975a914..8f49519 100644 --- a/health/client.go +++ b/health/client.go @@ -12,12 +12,15 @@ import ( ) type HealthClient interface { + UpsertCheck(slug string) error + SendSuccess(slug string) error SendFailure(slug string) error SendFailureWithErr(slug string, err error) error } type CreateCheckPayload struct { + Name string `json:"name"` Slug string `json:"slug"` Timeout int `json:"timeout"` Grace int `json:"grace"` @@ -119,6 +122,7 @@ func (hc *healthClient) SendFailureWithErr(slug string, err error) error { func (hc *healthClient) UpsertCheck(slug string) error { url := "https://healthchecks.io/api/v3/checks/" createCheckPayload := &CreateCheckPayload{ + Name: slug, Slug: slug, Timeout: hc.timeoutSeconds, Grace: hc.gracePeriodSeconds, From d08c5faf2a76de6e24169ea0e98221ff2f02b032 Mon Sep 17 00:00:00 2001 From: Keefer Taylor | Tessellated Date: Fri, 16 Aug 2024 15:55:51 -0700 Subject: [PATCH 4/5] work work --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index fe130575a8539d7fca50695caacf0d771591e741..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKu}%U(5S^6+F4~Z2VR@~Zn9$mIBDOZR7IK~e639_Ne+8Dto|sr#_ycw(CYE-7 zf*)XQVSKZ@hFuOUiJF;Y_ub9T+`N0_c32`(jZs=Bsu59$!5FM!nq$1qqhKr6vjJ56 zj9$Bba-Q`Eg-CIT0;0fgRDi$TfOe@(T^f4duS&=6T0d(xlU|m<7f$Z?-YzH42mX5c z<#62q*C>}E_G-`iC3LU>%|C{!(-+3dTXpX%si)`O_?l?^7!cekIIyQN0)k( z`dpZ-yXx}Td8!_68y-%+%VB}|E#?MI zIw@Tl&#{%2J)tOF9sWp%lZp+>EDDGMz5*q4TjTzJwD|n*C&`^CAPW2|1ys;HX*Tdm zwzn2uj(cr{afZRhak)XAg2}dHeZj4G6GI!;BEA5|7ITB}z~qmBmO&;_V5tgx0TRrh ArT_o{ From 29c15703fd3108bccfd072550591723237c9dfb7 Mon Sep 17 00:00:00 2001 From: Keefer Taylor | Tessellated Date: Fri, 16 Aug 2024 16:00:16 -0700 Subject: [PATCH 5/5] remove overloaded API --- health/client.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/health/client.go b/health/client.go index 8f49519..05dc77e 100644 --- a/health/client.go +++ b/health/client.go @@ -16,7 +16,6 @@ type HealthClient interface { SendSuccess(slug string) error SendFailure(slug string) error - SendFailureWithErr(slug string, err error) error } type CreateCheckPayload struct { @@ -90,10 +89,6 @@ func (hc *healthClient) SendSuccess(slug string) error { } func (hc *healthClient) SendFailure(slug string) error { - return hc.SendFailureWithErr(slug, nil) -} - -func (hc *healthClient) SendFailureWithErr(slug string, err error) error { hc.logger.Info().Str("slug", slug).Msg("sending failure") if hc.createNewChecks {