Skip to content

Commit

Permalink
feat: log metrics in deterministic order
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 5, 2024
1 parent e672507 commit 743904d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Print metrics with keys sorted alphabetically. ([@palkan][])

- Upgrade to Go 1.23. ([@palkan][])

- Migrate to our custom telemetry endpoint. ([@palkan][])
Expand Down
8 changes: 7 additions & 1 deletion metrics/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"log/slog"
"slices"
"strings"

"github.com/anycable/anycable-go/utils"
Expand Down Expand Up @@ -55,9 +56,14 @@ func (p *BasePrinter) Write(m *Metrics) error {

// Print logs stats data using global logger with info level
func (p *BasePrinter) Print(snapshot map[string]uint64) {
// Sort keys to provide deterministic output
keys := utils.Keys(snapshot)
slices.Sort(keys)

fields := make([]interface{}, 0)

for k, v := range snapshot {
for _, k := range keys {
v := snapshot[k]
if p.filter == nil {
fields = append(fields, k, v)
} else if _, ok := p.filter[k]; ok {
Expand Down

0 comments on commit 743904d

Please sign in to comment.