-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
85 lines (69 loc) · 1.6 KB
/
metrics.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
75
76
77
78
79
80
81
82
83
84
85
package main
// Tag for metric.
type Tag struct {
Name string
Value string
}
// Field is a collection for specific metric value.
type Field struct {
Name string
Value interface{}
}
// Metric is a collection with many Tags and Fields.
type Metric struct {
Measurement string
Tags []Tag
Fields []Field
}
// Metrics of a colletion of metric.
type Metrics []Metric
// Verify Metric is not empty on Tags or Fields.
func (m *Metric) Empty() bool {
return ((*m).CountTags() == 0) || ((*m).CountFields() == 0)
}
// Add is aggregator for metric into metrics.
func (m *Metric) AddTag(i Tag) {
(*m).Tags = append((*m).Tags, i)
}
// Add is aggregator for field into metrics.
func (m *Metric) AddField(i Field) {
(*m).Fields = append((*m).Fields, i)
}
// Count Tags on metric.
func (m *Metric) CountTags() int {
return len((*m).Tags)
}
// Count Fields on metric.
func (m *Metric) CountFields() int {
return len((*m).Fields)
}
// Convert Tags to Map.
func (m *Metric) TagsToMap() map[string]string {
tags := make(map[string]string)
for _, tag := range (*m).Tags {
tags[tag.Name] = tag.Value
}
return tags
}
// Convert Fields to Map.
func (m *Metric) FieldsToMap() map[string]interface{} {
fields := make(map[string]interface{})
for _, field := range (*m).Fields {
fields[field.Name] = field.Value
}
return fields
}
// Count all metrics in metrics.
func (m *Metrics) Count() int {
return len(*m)
}
// Reset the metric metrics.
func (m *Metrics) Reset() {
*m = (*m)[:0]
}
// Add is aggregator for metric into metrics.
func (m *Metrics) Add(i Metric) {
if !i.Empty() {
*m = append(*m, i)
}
}