Skip to content

Commit

Permalink
feat: add heartbeat logging functionality using slog
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <[email protected]>
  • Loading branch information
ZhangJian He committed Oct 20, 2024
1 parent d46551c commit a70896d
Show file tree
Hide file tree
Showing 5 changed files with 20 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
3 changes: 3 additions & 0 deletions opengemini/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package opengemini
import (
"context"
"crypto/tls"
"golang.org/x/exp/slog"
"time"

"github.com/libgox/addr"
Expand Down Expand Up @@ -140,6 +141,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
8 changes: 8 additions & 0 deletions opengemini/client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package opengemini
import (
"context"
"errors"
"golang.org/x/exp/slog"
"net"
"net/http"
"strconv"
Expand All @@ -42,6 +43,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 +92,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

0 comments on commit a70896d

Please sign in to comment.