Openstatus www.openstatus.dev
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

authored by

Arthur EICHELBERGER and committed by
GitHub
6546cf12 73a2d6ac

+19 -38
+1 -1
apps/checker/Dockerfile
··· 11 11 12 12 ARG VERSION 13 13 14 - RUN go build -o openstatus-checker . 14 + RUN go build -o openstatus-checker ./cmd 15 15 16 16 FROM golang:1.21 17 17
+9 -8
apps/checker/main.go apps/checker/cmd/main.go
··· 10 10 "syscall" 11 11 12 12 "github.com/gin-gonic/gin" 13 + "github.com/openstatushq/openstatus/apps/checker" 13 14 "github.com/openstatushq/openstatus/apps/checker/request" 14 15 ) 15 16 ··· 58 59 return 59 60 } 60 61 61 - response, error := ping(httpClient, req) 62 + response, error := checker.Ping(httpClient, req) 62 63 if error != nil { 63 64 // Add one more retry 64 - response, error = ping(httpClient, req) 65 + response, error = checker.Ping(httpClient, req) 65 66 if error != nil { 66 - sendToTinybirdNew(PingData{ 67 + checker.SendToTinyBird(checker.PingData{ 67 68 URL: req.URL, 68 69 Region: flyRegion, 69 70 Message: error.Error(), ··· 73 74 WorkspaceID: req.WorkspaceID, 74 75 }) 75 76 if req.Status == "active" { 76 - updateStatus(UpdateData{ 77 + checker.UpdateStatus(checker.UpdateData{ 77 78 MonitorId: req.MonitorID, 78 79 Status: "error", 79 80 Message: error.Error(), ··· 88 89 89 90 if response.StatusCode < 200 || response.StatusCode >= 300 { 90 91 // Add one more retry 91 - response, error = ping(httpClient, req) 92 + response, error = checker.Ping(httpClient, req) 92 93 if response.StatusCode < 200 || response.StatusCode >= 300 && req.Status == "active" { 93 94 // If the status code is not within the 200 range, we update the status to error 94 - updateStatus(UpdateData{ 95 + checker.UpdateStatus(checker.UpdateData{ 95 96 MonitorId: req.MonitorID, 96 97 Status: "error", 97 98 StatusCode: response.StatusCode, ··· 103 104 // If the status was error and the status code is within the 200 range, we update the status to active 104 105 if req.Status == "error" && response.StatusCode >= 200 && response.StatusCode < 300 { 105 106 // If the status was error, we update it to active 106 - updateStatus(UpdateData{ 107 + checker.UpdateStatus(checker.UpdateData{ 107 108 MonitorId: req.MonitorID, 108 109 Status: "active", 109 110 Region: flyRegion, ··· 111 112 }) 112 113 } 113 114 // We send the data to Tinybird 114 - sendToTinybirdNew(response) 115 + checker.SendToTinyBird(response) 115 116 116 117 c.JSON(http.StatusOK, gin.H{"message": "ok"}) 117 118 return
+3 -23
apps/checker/ping.go
··· 1 - package main 1 + package checker 2 2 3 3 import ( 4 4 "bytes" ··· 25 25 Message string `json:"message,omitempty"` 26 26 } 27 27 28 - func sendToTinybird(pingData PingData) { 29 - url := "https://api.tinybird.co/v0/events?name=golang_ping_response__v3" 30 - fmt.Printf("📈 Sending data to Tinybird for %+v \n", pingData) 31 - bearer := "Bearer " + os.Getenv("TINYBIRD_TOKEN") 32 - payloadBuf := new(bytes.Buffer) 33 - json.NewEncoder(payloadBuf).Encode(pingData) 34 - req, err := http.NewRequest("POST", url, payloadBuf) 35 - req.Header.Set("Authorization", bearer) 36 - req.Header.Set("Content-Type", "application/json") 37 - 38 - client := &http.Client{Timeout: time.Second * 10} 39 - _, err = client.Do(req) 40 - if err != nil { 41 - fmt.Println(err) 42 - panic(err) 43 - } 44 - // Should we add a retry mechanism here? 45 - 46 - } 47 - 48 - func sendToTinybirdNew(pingData PingData) { 28 + func SendToTinyBird(pingData PingData) { 49 29 url := "https://api.tinybird.co/v0/events?name=ping_response__v5" 50 30 fmt.Printf("📈 Sending data to Tinybird for %+v \n", pingData) 51 31 bearer := "Bearer " + os.Getenv("TINYBIRD_TOKEN") ··· 65 45 66 46 } 67 47 68 - func ping(client *http.Client, inputData request.CheckerRequest) (PingData, error) { 48 + func Ping(client *http.Client, inputData request.CheckerRequest) (PingData, error) { 69 49 70 50 region := os.Getenv("FLY_REGION") 71 51 request, err := http.NewRequest(inputData.Method, inputData.URL, bytes.NewReader([]byte(inputData.Body)))
+4 -4
apps/checker/ping_test.go
··· 1 - package main 1 + package checker 2 2 3 3 import ( 4 4 "net/http" ··· 26 26 } 27 27 for _, tt := range tests { 28 28 t.Run(tt.name, func(t *testing.T) { 29 - got, err := ping(tt.args.client, tt.args.inputData) 29 + got, err := Ping(tt.args.client, tt.args.inputData) 30 30 31 31 if (err != nil) != tt.wantErr { 32 - t.Errorf("ping() error = %v, wantErr %v", err, tt.wantErr) 32 + t.Errorf("Ping() error = %v, wantErr %v", err, tt.wantErr) 33 33 return 34 34 } 35 35 if got.StatusCode != tt.want.StatusCode { 36 - t.Errorf("ping() = %v, want %v", got, tt.want) 36 + t.Errorf("Ping() = %v, want %v", got, tt.want) 37 37 } 38 38 }) 39 39 }
+2 -2
apps/checker/update.go
··· 1 - package main 1 + package checker 2 2 3 3 import ( 4 4 "bytes" ··· 17 17 Region string `json:"region"` 18 18 } 19 19 20 - func updateStatus(updateData UpdateData) { 20 + func UpdateStatus(updateData UpdateData) { 21 21 url := "https://openstatus-api.fly.dev/updateStatus" 22 22 fmt.Println("URL:>", url) 23 23 basic := "Basic " + os.Getenv("CRON_SECRET")