Skip to content

Commit

Permalink
🔥 Go tweak (#495)
Browse files Browse the repository at this point in the history
* 🔥 to the moon

* 🔥 to the moon

* 🔥 to the moon

* 🔥 to the moon

* 🔥 to the moon
  • Loading branch information
thibaultleouay authored Dec 2, 2023
1 parent 5d55273 commit 920ab4f
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 66 deletions.
30 changes: 30 additions & 0 deletions apps/checker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# OpenStatus Checker

The checker service to ping external service.

It pings the service and save thedata to the tinybird

## How to run

```bash
go run *.go
```

## How to build

```bash
go build -o checker *.go
```

## How to run in docker

```bash
docker build -t checker .
docker run -p 8080:8080 checker
```

## How to deploy

```bash
fly deploy
```
17 changes: 12 additions & 5 deletions apps/checker/fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ primary_region = "ams"
[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
auto_stop_machines = false
auto_start_machines = false
processes = ["app"]

[[vm]]
cpu_kind = "shared"
cpus = 2
memory_mb = 512
cpus = 1
memory_mb = 256


[[http_service.checks]]
grace_period = "10s"
interval = "15s"
method = "GET"
timeout = "5s"
path = "/ping"
184 changes: 135 additions & 49 deletions apps/checker/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"strconv"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
Expand All @@ -31,79 +30,165 @@ func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Basic "+ os.Getenv("CRON_SECRET") {
if r.Header.Get("Authorization") != "Basic "+os.Getenv("CRON_SECRET") {
http.Error(w, "Unauthorized", 401)
return
}
region := os.Getenv("FLY_REGION")
}
i, err := strconv.Atoi(r.Header.Get("X-CloudTasks-TaskRetryCount"))
if err != nil {
http.Error(w, "Something went whont", 400)
return
}
// If something went wrong we only try it twice
if i > 1 {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Ok"))
return
}

if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
var u InputData
// region := os.Getenv("FLY_REGION")

err := json.NewDecoder(r.Body).Decode(&u)
err = json.NewDecoder(r.Body).Decode(&u)

fmt.Printf("Start checker for %+v", u)
fmt.Printf("🚀 Start checker for %+v \n", u)

if err != nil {
http.Error(w, err.Error(), 400)
w.Write([]byte("Ok"))
w.WriteHeader(200)
return
}
request, error := http.NewRequest(u.Method, u.Url, bytes.NewReader([]byte(u.Body)))

// Setting headers
for _, header := range u.Headers {
fmt.Printf("%+v", header)
if header.Key != "" && header.Value != "" {
request.Header.Set(header.Key, header.Value)
client := &http.Client{}
defer client.CloseIdleConnections()

response, error := ping(client, u)

if error != nil {
sendToTinybird(response)
if u.Status == "active" {
// updateStatus(UpdateData{
// MonitorId: u.MonitorId,
// Status: "error",
// Message: error.Error(),
// Region: region,
// })
}
w.Write([]byte("Ok"))
w.WriteHeader(200)
return
}

if error != nil {
fmt.Println(error)
sendToTinybird(response)

if response.StatusCode < 200 || response.StatusCode >= 300 {
// If the status code is not within the 200 range, we update the status to error
// updateStatus(UpdateData{
// MonitorId: u.MonitorId,
// Status: "error",
// StatusCode: response.StatusCode,
// Region: region,
// })
}
if u.Status == "error" {
// If the status was error, we update it to active
// updateStatus(UpdateData{
// MonitorId: u.MonitorId,
// Status: "active",
// Region: region,
// StatusCode: response.StatusCode,
// })
}

fmt.Printf("⏱️ End checker for %+v with latency %+d and statusCode %+d", u, response.Latency, response.StatusCode)
w.Write([]byte("Ok"))
w.WriteHeader(200)
return
})
// That's the new checker sending to the correct ingest endpoint
r.Post("/checker", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Basic "+os.Getenv("CRON_SECRET") {
http.Error(w, "Unauthorized", 401)
return
}
i, err := strconv.Atoi(r.Header.Get("X-CloudTasks-TaskRetryCount"))
if err != nil {
http.Error(w, "Something went whont", 400)
return
}
// If something went wrong we only try it twice
if i > 1 {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Ok"))
return
}

if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
var u InputData
// region := os.Getenv("FLY_REGION")

err = json.NewDecoder(r.Body).Decode(&u)

fmt.Printf("🚀 Start checker for %+v \n", u)

if err != nil {
w.Write([]byte("Ok"))
w.WriteHeader(200)
return
}

client := &http.Client{}
start := time.Now().UTC().UnixMilli()
response, error := client.Do(request)
end := time.Now().UTC().UnixMilli()
defer client.CloseIdleConnections()

response, error := ping(client, u)

// Retry if error
if error != nil {
response, error = client.Do(request)
end = time.Now().UTC().UnixMilli()
sendToTinybirdNew(response)
if u.Status == "active" {
// updateStatus(UpdateData{
// MonitorId: u.MonitorId,
// Status: "error",
// Message: error.Error(),
// Region: region,
// })
}
w.Write([]byte("Ok"))
w.WriteHeader(200)
return
}

latency := end - start
fmt.Println("🚀 Checked url: %v with latency %v in region %v ", u.Url, latency, region)
fmt.Printf("Response %+v for %+v", response, u)
if error != nil {
tiny((PingData{
Latency: (latency),
MonitorId: u.MonitorId,
Region: region,
WorkspaceId: u.WorkspaceId,
Timestamp: time.Now().UTC().UnixMilli(),
Url: u.Url,
Message: error.Error(),
}))
} else {
tiny((PingData{
Latency: (latency),
MonitorId: u.MonitorId,
Region: region,
WorkspaceId: u.WorkspaceId,
StatusCode: int16(response.StatusCode),
Timestamp: time.Now().UTC().UnixMilli(),
Url: u.Url,
}))
}

fmt.Printf("End checker for %+v", u)
sendToTinybirdNew(response)

if response.StatusCode < 200 || response.StatusCode >= 300 {
// If the status code is not within the 200 range, we update the status to error
// updateStatus(UpdateData{
// MonitorId: u.MonitorId,
// Status: "error",
// StatusCode: response.StatusCode,
// Region: region,
// })
}
if u.Status == "error" {
// If the status was error, we update it to active
// updateStatus(UpdateData{
// MonitorId: u.MonitorId,
// Status: "active",
// Region: region,
// StatusCode: response.StatusCode,
// })
}

fmt.Printf("⏱️ End checker for %+v with latency %+d and statusCode %+d", u, response.Latency, response.StatusCode)
w.Write([]byte("Ok"))
w.WriteHeader(200)
return
})

r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -116,8 +201,9 @@ func main() {
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(data)
return
})

http.ListenAndServe(":8080", r)
Expand Down
Loading

0 comments on commit 920ab4f

Please sign in to comment.