Skip to content

Open-source status page, incident response and uptime monitoring

feat(checker): extract tinybird in its own package (#569)

Arthur EICHELBERGER440079a2d5c3
diff --git a/apps/checker/cmd/main.go b/apps/checker/cmd/main.go
index bb6467203..0697af97e 100644
--- a/apps/checker/cmd/main.go
+++ b/apps/checker/cmd/main.go
@@ -13,6 +13,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/openstatushq/openstatus/apps/checker"
"github.com/openstatushq/openstatus/apps/checker/pkg/logger"
+ "github.com/openstatushq/openstatus/apps/checker/pkg/tinybird"
"github.com/openstatushq/openstatus/apps/checker/request"
"github.com/rs/zerolog/log"
)
@@ -32,13 +33,17 @@ func main() {
// environment variables.
flyRegion := env("FLY_REGION", "local")
cronSecret := env("CRON_SECRET", "")
+ tinyBirdToken := env("TINYBIRD_TOKEN", "")
logLevel := env("LOG_LEVEL", "warn")
logger.Configure(logLevel)
+ // packages.
httpClient := &http.Client{}
defer httpClient.CloseIdleConnections()
+ tinybirdClient := tinybird.NewClient(httpClient, tinyBirdToken)
+
router := gin.New()
router.POST("/checker", func(c *gin.Context) {
ctx := c.Request.Context()
@@ -64,17 +69,12 @@ func main() {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
return
}
- fmt.Printf("🚀 Start processing for %+v \n", req)
response, err := checker.Ping(ctx, httpClient, req)
if err != nil {
- fmt.Printf("1️⃣ first retry for %+v \n", req)
- // Add one more retry
response, err = checker.Ping(ctx, httpClient, req)
if err != nil {
- fmt.Printf("2️⃣ second retry for %+v \n", req)
-
- checker.SendToTinyBird(ctx, checker.PingData{
+ if err := tinybirdClient.SendEvent(ctx, checker.PingData{
URL: req.URL,
Region: flyRegion,
Message: err.Error(),
@@ -82,7 +82,10 @@ func main() {
Timestamp: req.CronTimestamp,
MonitorID: req.MonitorID,
WorkspaceID: req.WorkspaceID,
- })
+ }); err != nil {
+ log.Ctx(ctx).Error().Err(err).Msg("failed to send event to tinybird")
+ }
+
if req.Status == "active" {
checker.UpdateStatus(ctx, checker.UpdateData{
MonitorId: req.MonitorID,
@@ -121,8 +124,10 @@ func main() {
StatusCode: response.StatusCode,
})
}
- // We send the data to Tinybird
- checker.SendToTinyBird(ctx, response)
+
+ if err := tinybirdClient.SendEvent(ctx, response); err != nil {
+ log.Ctx(ctx).Error().Err(err).Msg("failed to send event to tinybird")
+ }
c.JSON(http.StatusOK, gin.H{"message": "ok"})
return
diff --git a/apps/checker/go.mod b/apps/checker/go.mod
index d86540014..800f66ca6 100644
--- a/apps/checker/go.mod
+++ b/apps/checker/go.mod
@@ -5,11 +5,13 @@ go 1.21.4
require (
github.com/gin-gonic/gin v1.9.1
github.com/rs/zerolog v1.31.0
+ github.com/stretchr/testify v1.8.3
)
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
+ github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
@@ -24,6 +26,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
+ github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
diff --git a/apps/checker/go.sum b/apps/checker/go.sum
index 11f3db051..5006a1c0f 100644
--- a/apps/checker/go.sum
+++ b/apps/checker/go.sum
@@ -39,7 +39,6 @@ github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNa
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
-github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
@@ -81,7 +80,6 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
diff --git a/apps/checker/ping.go b/apps/checker/ping.go
index 95c3c584f..d1cc96670 100644
--- a/apps/checker/ping.go
+++ b/apps/checker/ping.go
@@ -3,7 +3,6 @@ package checker
import (
"bytes"
"context"
- "encoding/json"
"fmt"
"io"
"net/http"
@@ -27,25 +26,6 @@ type PingData struct {
Message string `json:"message,omitempty"`
}
-func SendToTinyBird(ctx context.Context, 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")
- 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 {
- log.Ctx(ctx).Error().Err(err).Msg("Error while sending data to Tinybird")
- }
- // Should we add a retry mechanism here?
-
-}
-
func Ping(ctx context.Context, client *http.Client, inputData request.CheckerRequest) (PingData, error) {
logger := log.Ctx(ctx).With().Str("monitor", inputData.URL).Logger()
diff --git a/apps/checker/pkg/tinybird/client.go b/apps/checker/pkg/tinybird/client.go
new file mode 100644
index 000000000..e4aef093c
--- /dev/null
+++ b/apps/checker/pkg/tinybird/client.go
@@ -0,0 +1,69 @@
+package tinybird
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "net/url"
+
+ "github.com/rs/zerolog/log"
+)
+
+const baseURL = "https://api.tinybird.co/v0/events"
+
+type Client interface {
+ SendEvent(ctx context.Context, event any) error
+}
+
+type client struct {
+ httpClient *http.Client
+ apiKey string
+}
+
+func NewClient(httpClient *http.Client, apiKey string) Client {
+ return client{
+ httpClient: httpClient,
+ apiKey: apiKey,
+ }
+}
+
+func (c client) SendEvent(ctx context.Context, event any) error {
+ requestURL, err := url.Parse(baseURL)
+ if err != nil {
+ log.Ctx(ctx).Error().Err(err).Msg("unable to parse url")
+ return fmt.Errorf("unable to parse url: %w", err)
+ }
+
+ q := requestURL.Query()
+ q.Add("name", "ping_response__v5")
+ requestURL.RawQuery = q.Encode()
+
+ var payload bytes.Buffer
+ if err := json.NewEncoder(&payload).Encode(event); err != nil {
+ log.Ctx(ctx).Error().Err(err).Msg("unable to encode payload")
+ return fmt.Errorf("unable to encode payload: %w", err)
+ }
+
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL.String(), bytes.NewReader(payload.Bytes()))
+ if err != nil {
+ log.Ctx(ctx).Error().Err(err).Msg("unable to create request")
+ return fmt.Errorf("unable to create request: %w", err)
+ }
+ req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey))
+
+ resp, err := c.httpClient.Do(req)
+ if err != nil {
+ log.Ctx(ctx).Error().Err(err).Msg("unable to send request")
+ return fmt.Errorf("unable to send request: %w", err)
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ log.Ctx(ctx).Error().Str("status", resp.Status).Msg("unexpected status code")
+ return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
+ }
+
+ return nil
+}
diff --git a/apps/checker/pkg/tinybird/client_test.go b/apps/checker/pkg/tinybird/client_test.go
new file mode 100644
index 000000000..c02b8c238
--- /dev/null
+++ b/apps/checker/pkg/tinybird/client_test.go
@@ -0,0 +1,78 @@
+package tinybird_test
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "testing"
+
+ "github.com/openstatushq/openstatus/apps/checker/pkg/tinybird"
+ "github.com/stretchr/testify/require"
+)
+
+type interceptorHTTPClient struct {
+ f func(req *http.Request) (*http.Response, error)
+}
+
+func (i *interceptorHTTPClient) RoundTrip(req *http.Request) (*http.Response, error) {
+ return i.f(req)
+}
+
+func (i *interceptorHTTPClient) GetHTTPClient() *http.Client {
+ return &http.Client{
+ Transport: i,
+ }
+}
+
+func TestSendEvent(t *testing.T) {
+ t.Parallel()
+
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ t.Run("it should return an error if it can not send the event", func(t *testing.T) {
+ interceptor := &interceptorHTTPClient{
+ f: func(req *http.Request) (*http.Response, error) {
+ return nil, fmt.Errorf("unable to send request")
+ },
+ }
+
+ client := tinybird.NewClient(interceptor.GetHTTPClient(), "apiKey")
+
+ err := client.SendEvent(ctx, "event")
+ require.Error(t, err)
+ })
+
+ t.Run("it should return an error if the response status code is not 200", func(t *testing.T) {
+ interceptor := &interceptorHTTPClient{
+ f: func(req *http.Request) (*http.Response, error) {
+ return &http.Response{
+ StatusCode: http.StatusInternalServerError,
+ }, nil
+ },
+ }
+
+ client := tinybird.NewClient(interceptor.GetHTTPClient(), "apiKey")
+
+ err := client.SendEvent(ctx, "event")
+ require.Error(t, err)
+ })
+
+ t.Run("it should succeed and return nothing", func(t *testing.T) {
+ var url string
+ interceptor := &interceptorHTTPClient{
+ f: func(req *http.Request) (*http.Response, error) {
+ url = req.URL.String()
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ }, nil
+ },
+ }
+
+ client := tinybird.NewClient(interceptor.GetHTTPClient(), "apiKey")
+
+ err := client.SendEvent(ctx, "event")
+ require.NoError(t, err)
+ require.Equal(t, "https://api.tinybird.co/v0/events?name=ping_response__v5", url)
+ })
+}