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.5 kB ยท 108 lines
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109package main
import ( "context" "fmt" "net/http"
"os" "os/signal" "syscall" "time"
"connectrpc.com/connect" "github.com/madflojo/tasks" "github.com/openstatushq/openstatus/apps/checker/pkg/job" "github.com/openstatushq/openstatus/apps/checker/pkg/scheduler"
v1 "github.com/openstatushq/openstatus/apps/checker/proto/private_location/v1")
const ( configRefreshInterval = 10 * time.Minute platformTimeout = 30 * time.Second)
func main() { apiKey := getEnv("OPENSTATUS_KEY", "") if apiKey == "" { fmt.Fprintln(os.Stderr, "OPENSTATUS_KEY is required: the probe cannot authenticate against openstatus") os.Exit(1) }
ctx, cancel := context.WithCancel(context.Background()) defer cancel()
// Graceful shutdown on interrupt sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) go func() { <-sigChan cancel() }() fmt.Println("Launching openstatus private location checker") s := tasks.New() defer s.Stop()
monitorManager := scheduler.MonitorManager{ Client: getClient(apiKey), JobRunner: job.NewJobRunner(), Scheduler: s, } configTicker := time.NewTicker(configRefreshInterval) defer configTicker.Stop()
monitorManager.UpdateMonitors(ctx) for { select { case <-ctx.Done(): return case <-configTicker.C: fmt.Println("fetching monitors") monitorManager.UpdateMonitors(ctx) } }}
func getEnv(key, fallback string) string { if value, ok := os.LookupEnv(key); ok { return value } return fallback}
func getClient(apiKey string) v1.PrivateLocationServiceClient { ingestUrl := getEnv("OPENSTATUS_INGEST_URL", "https://openstatus-private-location.fly.dev")
// Tasks run with RunSingleInstance, so an untimed request that hangs would // wedge that monitor: it never checks again until the probe restarts. httpClient := &http.Client{Timeout: platformTimeout}
client := v1.NewPrivateLocationServiceClient( httpClient, ingestUrl, connect.WithHTTPGet(), connect.WithInterceptors(NewAuthInterceptor(apiKey)), )
return client}
func NewAuthInterceptor(token string) connect.UnaryInterceptorFunc {
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc { return connect.UnaryFunc(func( ctx context.Context, req connect.AnyRequest, ) (connect.AnyResponse, error) { if req.Spec().IsClient { // Send a token with client requests. req.Header().Set("openstatus-token", token) }
return next(ctx, req) }) } return connect.UnaryInterceptorFunc(interceptor)
}