Skip to content

Commit

Permalink
Merge pull request #607 from ripienaar/check_helpers
Browse files Browse the repository at this point in the history
Add some check helpers
  • Loading branch information
ripienaar authored Jan 8, 2025
2 parents 0a2034f + de499da commit 8f24f76
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 191 deletions.
79 changes: 79 additions & 0 deletions audit/archive/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"archive/zip"
"encoding/json"
"fmt"
"reflect"

"github.com/nats-io/nats-server/v2/server"
"io"
"os"
"slices"
Expand Down Expand Up @@ -375,3 +378,79 @@ func shrinkMapOfSets[T any](m map[string]map[string]T) ([]string, map[string][]s
slices.Sort(keysList)
return keysList, newMap
}

// EachClusterServerVarz iterates over all servers ordered by cluster and calls the callback function with the loaded Varz response
//
// The callback function will receive any error encountered during loading the server varz file and should check that and handle it
// If the callback returns an error iteration is stopped and that error is returned
//
// Errors returned match those documented in Load() otherwise any other error that are encountered
func (r *Reader) EachClusterServerVarz(cb func(clusterTag *Tag, serverTag *Tag, err error, vz *server.ServerAPIVarzResponse) error) (int, error) {
return r.eachClusterServer(TagServerVars(), server.ServerAPIVarzResponse{}, func(clusterTag *Tag, serverTag *Tag, err error, resp any) error {
vz := resp.(*server.ServerAPIVarzResponse)
if vz == nil || vz.Data == nil {
err = ErrNoMatches
}

return cb(clusterTag, serverTag, err, vz)
})
}

// EachClusterServerHealthz iterates over all servers ordered by cluster and calls the callback function with the loaded Healthz response
//
// The callback function will receive any error encountered during loading the server varz file and should check that and handle it
// If the callback returns an error iteration is stopped and that error is returned
//
// Errors returned match those documented in Load() otherwise any other error that are encountered
func (r *Reader) EachClusterServerHealthz(cb func(clusterTag *Tag, serverTag *Tag, err error, vz *server.ServerAPIHealthzResponse) error) (int, error) {
return r.eachClusterServer(TagServerJetStream(), server.ServerAPIHealthzResponse{}, func(clusterTag *Tag, serverTag *Tag, err error, resp any) error {
hz := resp.(*server.ServerAPIHealthzResponse)
if hz == nil || hz.Data == nil {
err = ErrNoMatches
}

return cb(clusterTag, serverTag, err, hz)
})
}

// EachClusterServerJsz iterates over all servers ordered by cluster and calls the callback function with the loaded Jsz response
//
// The callback function will receive any error encountered during loading the server varz file and should check that and handle it
// If the callback returns an error iteration is stopped and that error is returned
//
// Errors returned match those documented in Load() otherwise any other error that are encountered
func (r *Reader) EachClusterServerJsz(cb func(clusterTag *Tag, serverTag *Tag, err error, jsz *server.ServerAPIJszResponse) error) (int, error) {
return r.eachClusterServer(TagServerJetStream(), server.ServerAPIJszResponse{}, func(clusterTag *Tag, serverTag *Tag, err error, resp any) error {
jsz := resp.(*server.ServerAPIJszResponse)
if jsz == nil || jsz.Data == nil {
err = ErrNoMatches
}

return cb(clusterTag, serverTag, err, jsz)
})
}

// helper to iterate all servers, creates instances of targetType based on tag. targetType must be a non pointer like server.ServerAPIJszResponse{}
func (r *Reader) eachClusterServer(tag *Tag, targetType any, cb func(clusterTag *Tag, serverTag *Tag, err error, resp any) error) (int, error) {
found := 0

for _, clusterName := range r.ClusterNames() {
clusterTag := TagCluster(clusterName)
servers := r.ClusterServerNames(clusterName)
found = len(servers)

for _, serverName := range servers {
serverTag := TagServer(serverName)

resp := reflect.New(reflect.TypeOf(targetType)).Interface()
err := r.Load(&resp, clusterTag, serverTag, tag)

err = cb(clusterTag, serverTag, err, resp)
if err != nil {
return found, err
}
}
}

return found, nil
}
2 changes: 2 additions & 0 deletions audit/archive/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Tag struct {
Value string
}

func (t *Tag) String() string { return t.Value }

const (
serverTagLabel TagLabel = "server"
clusterTagLabel TagLabel = "cluster"
Expand Down
7 changes: 7 additions & 0 deletions audit/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ func (c *CheckCollection) ConfigurationItems() []*CheckConfiguration {
}

sort.Slice(res, func(i, j int) bool {
switch strings.Compare(res[i].Check, res[j].Check) {
case -1:
return true
case 1:
return false
}

return res[i].Key < res[j].Key
})

Expand Down
3 changes: 2 additions & 1 deletion audit/examples_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (c *ExamplesCollection) Add(format string, a ...any) {
c.Examples = append(c.Examples, fmt.Sprintf(format, a...))
}

func (c *ExamplesCollection) clear() {
// Clear removes all added examples
func (c *ExamplesCollection) Clear() {
c.Examples = []string{}
}

Expand Down
2 changes: 1 addition & 1 deletion audit/gather/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ func Gather(nc *nats.Conn, conf *Configuration) error {
cfg: conf,
nc: nc,
capture: &captureLogBuffer,
log: NewLogger(&captureLogBuffer, conf.LogLevel),
log: newLogger(&captureLogBuffer, conf.LogLevel),
}

return g.start()
Expand Down
2 changes: 1 addition & 1 deletion audit/gather/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type logger struct {
logFunc func(format string, a ...any)
}

func NewLogger(capture io.Writer, level api.Level) api.Logger {
func newLogger(capture io.Writer, level api.Level) api.Logger {
l := &logger{
lvl: level,
logFunc: log.Printf,
Expand Down
Loading

0 comments on commit 8f24f76

Please sign in to comment.