Go SDK
The 9n9s-go package provides a simple way to integrate Heartbeat Monitoring into your Go applications.
Installation
Section titled “Installation”go get github.com/9n9s-io/9n9s-goInitializing the Client
Section titled “Initializing the Client”First, initialize a client with your Heartbeat Monitor’s UUID. It’s best practice to store this in an environment variable.
import ( "os" "github.com/9n9s-io/9n9s-go")
nines := nines.New(os.Getenv("MY_JOB_MONITOR_ID"))Signaling Success
Section titled “Signaling Success”To send a simple success pulse. The SDK methods are non-blocking and send requests in a separate goroutine.
nines.Pulse()Tracking Runtimes
Section titled “Tracking Runtimes”To track the execution time of a function, you can use the Time() method with a callback.
// Your function that does workmyImportantJob := func() error { // ... your job logic ... fmt.Println("Doing work!") // return an error to signal failure return nil}
// The Time method will handle the start and completion/failure pulseserr := nines.Time(myImportantJob)if err != nil { // This will be the error returned from myImportantJob}You can also do this manually:
nines.Start()err := doWork()if err != nil { nines.Fail(err.Error())} else { nines.Pulse("Work complete")}Sending Payloads
Section titled “Sending Payloads”You can send logs, error details, or any other string payload with your pulses.
// Send a simple log messagenines.Pulse("Processed 100 items.")
// Send an error on failureerr := doSomething()if err != nil { nines.Fail(err.Error())}Use with Goroutines
Section titled “Use with Goroutines”The client is safe for concurrent use. You can easily monitor background workers running in goroutines.
func worker(id int, jobs <-chan int, nines *nines.Client) { for j := range jobs { // Use a new Nines client for each job execution if you want to track them separately // or use the same client to monitor the worker's lifecycle.
// Here, we use the Time function to monitor each job's execution jobFunc := func() error { fmt.Printf("worker %d started job %d\n", id, j) time.Sleep(time.Second) fmt.Printf("worker %d finished job %d\n", id, j) return nil }
nines.Time(jobFunc) }}