Skip to content

Commit 2faec73

Browse files
committed
Remove hashmap
1 parent 425f7d5 commit 2faec73

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

collector_structs.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package main
22

33
import (
4-
"crypto/sha1"
5-
"fmt"
64
"github.com/prometheus/client_golang/prometheus"
7-
"strings"
85
"time"
96
)
107

@@ -13,59 +10,49 @@ type MetricCollectorRow struct {
1310
value float64
1411
}
1512

16-
type MetricCollectorList struct {
17-
list map[string]MetricCollectorRow
18-
}
19-
2013
func NewMetricCollectorList() *MetricCollectorList {
2114
ret := MetricCollectorList{}
2215
ret.Init()
2316
return &ret
2417
}
2518

2619
func (m *MetricCollectorList) Init() {
27-
m.list = map[string]MetricCollectorRow{}
20+
m.list = []MetricCollectorRow{}
2821
}
2922

30-
func (m *MetricCollectorList) hashLabels(labels prometheus.Labels) string {
31-
list := []string{}
32-
33-
for key, value := range labels {
34-
list = append(list, fmt.Sprintf("%s=%s", key, value))
35-
}
36-
37-
return fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(list, "#"))))
23+
type MetricCollectorList struct {
24+
list []MetricCollectorRow
3825
}
3926

4027
func (m *MetricCollectorList) Add(labels prometheus.Labels, value float64) {
41-
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: value}
28+
m.list = append(m.list, MetricCollectorRow{labels: labels, value: value})
4229
}
4330

4431
func (m *MetricCollectorList) AddInfo(labels prometheus.Labels) {
45-
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: 1}
32+
m.list = append(m.list, MetricCollectorRow{labels: labels, value: 1})
4633
}
4734

4835
func (m *MetricCollectorList) AddTime(labels prometheus.Labels, value time.Time) {
4936
timeValue := timeToFloat64(value)
5037

5138
if timeValue > 0 {
52-
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: timeValue}
39+
m.list = append(m.list, MetricCollectorRow{labels: labels, value: timeValue})
5340
}
5441
}
5542

5643
func (m *MetricCollectorList) AddDuration(labels prometheus.Labels, value time.Duration) {
57-
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: value.Seconds()}
44+
m.list = append(m.list, MetricCollectorRow{labels: labels, value: value.Seconds()})
5845
}
5946

6047
func (m *MetricCollectorList) AddIfNotZero(labels prometheus.Labels, value float64) {
6148
if value != 0 {
62-
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: value}
49+
m.list = append(m.list, MetricCollectorRow{labels: labels, value: value})
6350
}
6451
}
6552

6653
func (m *MetricCollectorList) AddIfGreaterZero(labels prometheus.Labels, value float64) {
6754
if value > 0 {
68-
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: value}
55+
m.list = append(m.list, MetricCollectorRow{labels: labels, value: value})
6956
}
7057
}
7158

@@ -80,6 +67,7 @@ func (m *MetricCollectorList) CounterAdd(counter *prometheus.CounterVec) {
8067
counter.With(metric.labels).Add(metric.value)
8168
}
8269
}
70+
8371
func (m *MetricCollectorList) SummarySet(counter *prometheus.SummaryVec) {
8472
for _, metric := range m.list {
8573
counter.With(metric.labels).Observe(metric.value)

0 commit comments

Comments
 (0)