Skip to content

Commit

Permalink
feat: add heartbeat logging functionality using slog (#187)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
hezhangjian authored Oct 20, 2024
1 parent d46551c commit 886a613
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/libgox/addr v0.2.0
github.com/prometheus/client_golang v1.20.5
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoG
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
Expand Down
4 changes: 4 additions & 0 deletions opengemini/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"crypto/tls"
"time"

"golang.org/x/exp/slog"

"github.com/libgox/addr"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -140,6 +142,8 @@ type Config struct {
TlsConfig *tls.Config
// CustomMetricsLabels add custom labels to all the metrics reported by this client instance
CustomMetricsLabels map[string]string
// Logger structured logger for logging operations
Logger *slog.Logger
}

// Address configuration for providing service.
Expand Down
9 changes: 9 additions & 0 deletions opengemini/client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"sync/atomic"
"time"

"golang.org/x/exp/slog"

"github.com/libgox/addr"
)

Expand All @@ -42,6 +44,8 @@ type client struct {

batchContext context.Context
batchContextCancel context.CancelFunc

logger *slog.Logger
}

func newClient(c *Config) (Client, error) {
Expand Down Expand Up @@ -89,6 +93,11 @@ func newClient(c *Config) (Client, error) {
// if there are multiple addresses, start the health check
go dbClient.endpointsCheck(ctx)
}
if c.Logger != nil {
dbClient.logger = c.Logger
} else {
dbClient.logger = slog.Default()
}
return dbClient, nil
}

Expand Down
6 changes: 6 additions & 0 deletions opengemini/servers_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ func (c *client) checkUpOrDown(ctx context.Context) {
defer func() {
wg.Done()
if err := recover(); err != nil {
c.logger.Error("panic recovered during endpoint check", "index", idx, "error", err)
return
}
}()
err := c.ping(ctx, idx)
if err != nil {
c.logger.Error("ping failed", "index", idx, "error", err)
} else {
c.logger.Info("ping succeeded", "index", idx)
}
c.endpoints[idx].isDown.Store(err != nil)
}(i)
}
Expand Down
3 changes: 3 additions & 0 deletions opengemini/servers_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"testing"
"time"

"golang.org/x/exp/slog"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -76,6 +78,7 @@ func TestServerCheck(t *testing.T) {
},
},
prevIdx: atomic.Int32{},
logger: slog.Default(),
}
cli.prevIdx.Store(-1)
var ctx context.Context
Expand Down

0 comments on commit 886a613

Please sign in to comment.