snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
41
internal/usage/metric.go
Normal file
41
internal/usage/metric.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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])
|
||||
}
|
||||
Reference in New Issue
Block a user