Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.1 kB ยท 79 lines
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980package tinybird
import ( "bytes" "context" "encoding/json" "fmt" "net/http" "net/url" "os"
"github.com/rs/zerolog/log")
func getBaseURL() string { // Use local Tinybird container if available (Docker/self-hosted) // https://www.tinybird.co/docs/api-reference if tinybirdURL := os.Getenv("TINYBIRD_URL"); tinybirdURL != "" { return tinybirdURL + "/v0/events" } return "https://api.tinybird.co/v0/events"}
type Client interface { SendEvent(ctx context.Context, event any, dataSourceName string) error}
type client struct { httpClient *http.Client apiKey string baseURL string}
func NewClient(httpClient *http.Client, apiKey string) Client { return client{ httpClient: httpClient, apiKey: apiKey, baseURL: getBaseURL(), }}
func (c client) SendEvent(ctx context.Context, event any, dataSourceName string) error { requestURL, err := url.Parse(c.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", dataSourceName) 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.StatusAccepted { log.Ctx(ctx).Error().Str("status", resp.Status).Msg("unexpected status code") return fmt.Errorf("unexpected status code: %d", resp.StatusCode) }
return nil}