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

feat: labels sorted for metrics #991

Merged
merged 1 commit into from
Jul 2, 2024
Merged
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
35 changes: 26 additions & 9 deletions types/sample.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"sort"
"strings"
"time"

Expand All @@ -10,12 +11,19 @@ import (
"flashcat.cloud/categraf/pkg/conv"
)

type Sample struct {
Metric string `json:"metric"`
Timestamp time.Time `json:"timestamp"`
Value interface{} `json:"value"`
Labels map[string]string `json:"labels"`
}
type (
pair struct {
key string
val string
}

Sample struct {
Metric string `json:"metric"`
Timestamp time.Time `json:"timestamp"`
Value interface{} `json:"value"`
Labels map[string]string `json:"labels"`
}
)

var (
labelReplacer = strings.NewReplacer("-", "_", ".", "_", " ", "_", "/", "_")
Expand Down Expand Up @@ -74,11 +82,20 @@ func (item *Sample) ConvertTimeSeries(precision string) *prompb.TimeSeries {
Value: item.Metric,
})

// add other labels
// sort labels
pairs := make([]pair, 0, len(item.Labels))
for k, v := range item.Labels {
pairs = append(pairs, pair{k, v})
}
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].key < pairs[j].key
})

// add other labels
for _, p := range pairs {
pt.Labels = append(pt.Labels, prompb.Label{
Name: labelReplacer.Replace(k),
Value: v,
Name: labelReplacer.Replace(p.key),
Value: p.val,
})
}

Expand Down
Loading