Skip to content

Go SDK

The 9n9s-go package provides a simple way to integrate Heartbeat Monitoring into your Go applications.

Terminal window
go get github.com/9n9s-io/9n9s-go

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"))

To send a simple success pulse. The SDK methods are non-blocking and send requests in a separate goroutine.

nines.Pulse()

To track the execution time of a function, you can use the Time() method with a callback.

// Your function that does work
myImportantJob := 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 pulses
err := 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")
}

You can send logs, error details, or any other string payload with your pulses.

// Send a simple log message
nines.Pulse("Processed 100 items.")
// Send an error on failure
err := doSomething()
if err != nil {
nines.Fail(err.Error())
}

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)
}
}