Files
upstream-centrifugo/internal/usage/metric.go
upstream-mirror b352a9a30d
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s
snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:17:21 +05:00

42 lines
890 B
Go

package usage
import (
"bytes"
"crypto/sha256"
"fmt"
"sort"
)
// metric represents stats point.
type metric struct {
Id string `json:"id"`
OrgId int `json:"org_id"`
Name string `json:"name"`
Metric string `json:"metric"`
Interval int `json:"interval"`
Value float64 `json:"value"`
Unit string `json:"unit"`
Time int64 `json:"time"`
Type string `json:"mtype"`
Tags []string `json:"tags"`
}
func (m *metric) SetId() {
sort.Strings(m.Tags)
buffer := bytes.NewBufferString(m.Metric)
buffer.WriteByte(0)
buffer.WriteString(m.Unit)
buffer.WriteByte(0)
buffer.WriteString(m.Type)
buffer.WriteByte(0)
_, _ = fmt.Fprintf(buffer, "%d", m.Interval)
for _, k := range m.Tags {
buffer.WriteByte(0)
buffer.WriteString(k)
}
hash := sha256.Sum256(buffer.Bytes())
m.Id = fmt.Sprintf("%d.%x", m.OrgId, hash[:16])
}