Skip to content

Open-source status page, incident response and uptime monitoring

feat(checker): extract checker command (#548)

Arthur EICHELBERGER6546cf12a2a8
diff --git a/apps/checker/Dockerfile b/apps/checker/Dockerfile
index 87d51c4fd..6bc505d65 100644
--- a/apps/checker/Dockerfile
+++ b/apps/checker/Dockerfile
@@ -11,7 +11,7 @@ COPY . .
ARG VERSION
-RUN go build -o openstatus-checker .
+RUN go build -o openstatus-checker ./cmd
FROM golang:1.21
diff --git a/apps/checker/main.go b/apps/checker/cmd/main.go
similarity index 88%
rename from apps/checker/main.go
rename to apps/checker/cmd/main.go
index 4c80a4acf..31fa75d06 100644
--- a/apps/checker/main.go
+++ b/apps/checker/cmd/main.go
@@ -10,6 +10,7 @@ import (
"syscall"
"github.com/gin-gonic/gin"
+ "github.com/openstatushq/openstatus/apps/checker"
"github.com/openstatushq/openstatus/apps/checker/request"
)
@@ -58,12 +59,12 @@ func main() {
return
}
- response, error := ping(httpClient, req)
+ response, error := checker.Ping(httpClient, req)
if error != nil {
// Add one more retry
- response, error = ping(httpClient, req)
+ response, error = checker.Ping(httpClient, req)
if error != nil {
- sendToTinybirdNew(PingData{
+ checker.SendToTinyBird(checker.PingData{
URL: req.URL,
Region: flyRegion,
Message: error.Error(),
@@ -73,7 +74,7 @@ func main() {
WorkspaceID: req.WorkspaceID,
})
if req.Status == "active" {
- updateStatus(UpdateData{
+ checker.UpdateStatus(checker.UpdateData{
MonitorId: req.MonitorID,
Status: "error",
Message: error.Error(),
@@ -88,10 +89,10 @@ func main() {
if response.StatusCode < 200 || response.StatusCode >= 300 {
// Add one more retry
- response, error = ping(httpClient, req)
+ response, error = checker.Ping(httpClient, req)
if response.StatusCode < 200 || response.StatusCode >= 300 && req.Status == "active" {
// If the status code is not within the 200 range, we update the status to error
- updateStatus(UpdateData{
+ checker.UpdateStatus(checker.UpdateData{
MonitorId: req.MonitorID,
Status: "error",
StatusCode: response.StatusCode,
@@ -103,7 +104,7 @@ func main() {
// If the status was error and the status code is within the 200 range, we update the status to active
if req.Status == "error" && response.StatusCode >= 200 && response.StatusCode < 300 {
// If the status was error, we update it to active
- updateStatus(UpdateData{
+ checker.UpdateStatus(checker.UpdateData{
MonitorId: req.MonitorID,
Status: "active",
Region: flyRegion,
@@ -111,7 +112,7 @@ func main() {
})
}
// We send the data to Tinybird
- sendToTinybirdNew(response)
+ checker.SendToTinyBird(response)
c.JSON(http.StatusOK, gin.H{"message": "ok"})
return
diff --git a/apps/checker/ping.go b/apps/checker/ping.go
index 3b42fcdf0..850b6e230 100644
--- a/apps/checker/ping.go
+++ b/apps/checker/ping.go
@@ -1,4 +1,4 @@
-package main
+package checker
import (
"bytes"
@@ -25,27 +25,7 @@ type PingData struct {
Message string `json:"message,omitempty"`
}
-func sendToTinybird(pingData PingData) {
- url := "https://api.tinybird.co/v0/events?name=golang_ping_response__v3"
- fmt.Printf("πŸ“ˆ Sending data to Tinybird for %+v \n", pingData)
- bearer := "Bearer " + os.Getenv("TINYBIRD_TOKEN")
- payloadBuf := new(bytes.Buffer)
- json.NewEncoder(payloadBuf).Encode(pingData)
- req, err := http.NewRequest("POST", url, payloadBuf)
- req.Header.Set("Authorization", bearer)
- req.Header.Set("Content-Type", "application/json")
-
- client := &http.Client{Timeout: time.Second * 10}
- _, err = client.Do(req)
- if err != nil {
- fmt.Println(err)
- panic(err)
- }
- // Should we add a retry mechanism here?
-
-}
-
-func sendToTinybirdNew(pingData PingData) {
+func SendToTinyBird(pingData PingData) {
url := "https://api.tinybird.co/v0/events?name=ping_response__v5"
fmt.Printf("πŸ“ˆ Sending data to Tinybird for %+v \n", pingData)
bearer := "Bearer " + os.Getenv("TINYBIRD_TOKEN")
@@ -65,7 +45,7 @@ func sendToTinybirdNew(pingData PingData) {
}
-func ping(client *http.Client, inputData request.CheckerRequest) (PingData, error) {
+func Ping(client *http.Client, inputData request.CheckerRequest) (PingData, error) {
region := os.Getenv("FLY_REGION")
request, err := http.NewRequest(inputData.Method, inputData.URL, bytes.NewReader([]byte(inputData.Body)))
diff --git a/apps/checker/ping_test.go b/apps/checker/ping_test.go
index 1d89fec9f..ff31091ed 100644
--- a/apps/checker/ping_test.go
+++ b/apps/checker/ping_test.go
@@ -1,4 +1,4 @@
-package main
+package checker
import (
"net/http"
@@ -26,14 +26,14 @@ func Test_ping(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- got, err := ping(tt.args.client, tt.args.inputData)
+ got, err := Ping(tt.args.client, tt.args.inputData)
if (err != nil) != tt.wantErr {
- t.Errorf("ping() error = %v, wantErr %v", err, tt.wantErr)
+ t.Errorf("Ping() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got.StatusCode != tt.want.StatusCode {
- t.Errorf("ping() = %v, want %v", got, tt.want)
+ t.Errorf("Ping() = %v, want %v", got, tt.want)
}
})
}
diff --git a/apps/checker/update.go b/apps/checker/update.go
index 37f85135c..1828529c1 100644
--- a/apps/checker/update.go
+++ b/apps/checker/update.go
@@ -1,4 +1,4 @@
-package main
+package checker
import (
"bytes"
@@ -17,7 +17,7 @@ type UpdateData struct {
Region string `json:"region"`
}
-func updateStatus(updateData UpdateData) {
+func UpdateStatus(updateData UpdateData) {
url := "https://openstatus-api.fly.dev/updateStatus"
fmt.Println("URL:>", url)
basic := "Basic " + os.Getenv("CRON_SECRET")