This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
/
raw_metric.go
74 lines (59 loc) · 1.93 KB
/
raw_metric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package instrumentation
import (
"encoding/json"
"strings"
"hash/fnv"
)
// rawMetric - Raw Metric - represents the data needed to send a new metric.
type rawMetric struct {
ComponentName string
ReleaseTrain string
MdmAccount string
MdmNamespace string
MetricName string
Dimensions []*Dimension
Value uint32
}
// newRawMetric constructor for metric
func newRawMetric(releaseTrain, componentName, mdmAccount, mdmNamespace, metricName string, dimensions []*Dimension, value uint32) *rawMetric {
m := &rawMetric{
ComponentName: componentName,
ReleaseTrain: releaseTrain,
MdmAccount: mdmAccount,
MdmNamespace: mdmNamespace,
MetricName: metricName,
Dimensions: dimensions,
Value: value,
}
return m
}
// String returns rawMetric represented as json string. it is implemented for implement fmt.Stringer interface.
func (raw *rawMetric) String() string {
out, _ := json.Marshal(raw)
return string(out)
}
func rawMetricFromString(rawMetricBytes []byte) (*rawMetric, error) {
data := rawMetric{}
err := json.Unmarshal(rawMetricBytes, &data)
if err != nil {
return nil, err
}
return &data, nil
}
// GetDimensionsString returns the dimensaion as string.
func GetDimensionsString(dimensions []*Dimension) string {
var stringBuilder strings.Builder
for _, dimension := range dimensions {
stringBuilder.WriteString(dimension.Key)
stringBuilder.WriteString(dimension.Value)
}
return stringBuilder.String()
}
// GetHashExcludingValue - get hash of the metric object, excluding the metric value from the hashed value calculation.
// It is done as we want to group similar instances together but we don't want the value to be part of this calculation.
func (raw *rawMetric) GetHashExcludingValue() uint32 {
str := raw.MetricName + raw.ComponentName + raw.MdmAccount + raw.MdmNamespace + raw.ReleaseTrain + GetDimensionsString(raw.Dimensions)
h := fnv.New32a()
h.Write([]byte(str))
return h.Sum32()
}