Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBG-3637: Stat definitions use a named object for each stat definition #6592

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions base/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"fmt"
"log"
"math"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -785,39 +786,58 @@ type SgwStat struct {
statDesc *prometheus.Desc
}

// Name returns the fully qualified name of the stat.
// Currently only used for the stat metadata exporter tool.
func (s SgwStat) Name() string {
return s.statFQN
}

// Unit returns the units the stat uses for example, seconds.
// Currently only used for the stat metadata exporter tool.
func (s SgwStat) Unit() string {
return s.unit
}

// Help returns the help text for the stat.
// Currently only used for the stat metadata exporter tool.
func (s SgwStat) Help() string {
return s.help
}

// AddedVersion returns the version of Sync Gateway this stat was added.
// Currently only used for the stat metadata exporter tool.
func (s SgwStat) AddedVersion() string {
return s.addedVersion
}

// DeprecatedVersion returns the version of Sync Gateway this stat was deprecated.
// Currently only used for the stat metadata exporter tool.
func (s SgwStat) DeprecatedVersion() string {
return s.deprecatedVersion
}

// Stability returns if there is a commitment to keep this stat stable.
// Currently only used for the stat metadata exporter tool.
func (s SgwStat) Stability() string {
return s.stability
}

// LabelKeys returns the label keys for the stat in deterministic order.
// Currently only used for the stat metadata exporter tool.
func (s SgwStat) LabelKeys() []string {
labelKeys := make([]string, 0, len(s.labels))
for labelKey := range s.labels {
labelKeys = append(labelKeys, labelKey)
}

// Sort the label keys so that the order is deterministic
slices.Sort(labelKeys)

return labelKeys
}

// ValueTypeString returns the string representation of the prometheus.ValueType
// Currently only used for the stat metadata exporter tool.
func (s SgwStat) ValueTypeString() string {
switch s.statValueType {
case prometheus.CounterValue:
Expand Down
19 changes: 14 additions & 5 deletions tools/stats-definition-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,24 @@ func closeAndLogError(logger *log.Logger, c io.Closer) {
}
}

func getStats(logger *log.Logger) ([]StatDefinition, error) {
func getStats(logger *log.Logger) (StatDefinitions, error) {
globalStats, dbStats, err := registerStats()
if err != nil {
return nil, fmt.Errorf("could not register stats: %w", err)
}

// Append the db stat definitions on to the global stat definitions
stats := traverseAndRetrieveStats(logger, globalStats)
stats = append(stats, traverseAndRetrieveStats(logger, dbStats)...)
// Get all the stats
globalStatsDefinitions := traverseAndRetrieveStats(logger, globalStats)
dbStatDefinitions := traverseAndRetrieveStats(logger, dbStats)

// Merge the two maps
stats := make(StatDefinitions, len(globalStatsDefinitions)+len(dbStatDefinitions))
for k, v := range globalStatsDefinitions {
stats[k] = v
}
for k, v := range dbStatDefinitions {
stats[k] = v
}

return stats, nil
}
Expand Down Expand Up @@ -110,7 +119,7 @@ func registerStats() (*base.GlobalStat, *base.DbStats, error) {
return sgStats.GlobalStats, dbStats, nil
}

func writeStats(stats []StatDefinition, writer io.Writer) error {
func writeStats(stats StatDefinitions, writer io.Writer) error {
encoder := json.NewEncoder(writer)

encoder.SetIndent("", "\t")
Expand Down
5 changes: 3 additions & 2 deletions tools/stats-definition-exporter/stat_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/couchbase/sync_gateway/base"
)

// StatDefinitions is a map of the stats fully qualified name to a StatDefinition
type StatDefinitions map[string]StatDefinition

type StatDefinition struct {
Name string `json:"name"` // The fully qualified name of the stat
Unit string `json:"unit,omitempty"` // What units the stat value is using such as seconds.
Labels []string `json:"labels,omitempty"` // The labels that Prometheus uses to organise some of the stats such as database, collection, etc
Help string `json:"help,omitempty"` // A description of what the stat does
Expand All @@ -26,7 +28,6 @@ type StatDefinition struct {

func newStatDefinition(stat base.SgwStatWrapper) StatDefinition {
return StatDefinition{
Name: stat.Name(),
Unit: stat.Unit(),
Labels: stat.LabelKeys(),
Help: stat.Help(),
Expand Down
12 changes: 7 additions & 5 deletions tools/stats-definition-exporter/stat_traverser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/couchbase/sync_gateway/base"
)

func traverseAndRetrieveStats(logger *log.Logger, s any) []StatDefinition {
var stats []StatDefinition
func traverseAndRetrieveStats(logger *log.Logger, s any) StatDefinitions {
stats := make(StatDefinitions)

topLevel := reflect.ValueOf(s)
if topLevel.IsNil() {
Expand All @@ -40,7 +40,7 @@ func traverseAndRetrieveStats(logger *log.Logger, s any) []StatDefinition {
stat := field.Interface()
statWrapper, ok := stat.(base.SgwStatWrapper)
if ok {
stats = append(stats, newStatDefinition(statWrapper))
stats[statWrapper.Name()] = newStatDefinition(statWrapper)
continue
}

Expand All @@ -56,8 +56,10 @@ func traverseAndRetrieveStats(logger *log.Logger, s any) []StatDefinition {
field = values.Value()
}

// Follow to struct down a level
stats = append(stats, traverseAndRetrieveStats(logger, field.Interface())...)
// Follow to struct down a level and append the stats to the map
for k, v := range traverseAndRetrieveStats(logger, field.Interface()) {
stats[k] = v
}
}

return stats
Expand Down