@@ -19,6 +19,7 @@ package dtap
19
19
import (
20
20
"context"
21
21
"strings"
22
+ "sync"
22
23
"time"
23
24
24
25
log "github.com/sirupsen/logrus"
@@ -42,7 +43,7 @@ type DnstapPrometheusOutputMetrics struct {
42
43
Name string
43
44
Vec * prometheus.CounterVec
44
45
LabelKeys []string
45
- LabelValues map [string ]* DnstapPrometheusOutputMetricsValues
46
+ LabelValues sync. Map // map[string]*DnstapPrometheusOutputMetricsValues
46
47
Interval int
47
48
Expire int
48
49
CancelFunc context.CancelFunc
@@ -75,18 +76,18 @@ func NewDnstapPrometheusOutputMetrics(counterConfig OutputPrometheusMetrics) *Dn
75
76
Help : counterConfig .GetHelp (),
76
77
}, counterConfig .GetLabels ()),
77
78
LabelKeys : counterConfig .GetLabels (),
78
- LabelValues : map [ string ] * DnstapPrometheusOutputMetricsValues {},
79
+ LabelValues : sync. Map {},
79
80
Expire : counterConfig .GetExpireSec (),
80
81
Interval : counterConfig .GetExpireInterval (),
81
82
}
82
83
}
83
84
84
85
func (d * DnstapPrometheusOutputMetrics ) Inc (values []string ) {
85
86
d .Vec .WithLabelValues (values ... ).Inc ()
86
- d .LabelValues [ strings .Join (values , "," )] = & DnstapPrometheusOutputMetricsValues {
87
+ d .LabelValues . Store ( strings .Join (values , "," ), & DnstapPrometheusOutputMetricsValues {
87
88
Values : values ,
88
89
LastUpdate : time .Now (),
89
- }
90
+ })
90
91
}
91
92
92
93
func (d * DnstapPrometheusOutputMetrics ) Flush (ctx context.Context ) {
@@ -96,12 +97,14 @@ func (d *DnstapPrometheusOutputMetrics) Flush(ctx context.Context) {
96
97
case <- ctx .Done ():
97
98
return
98
99
case <- ticker .C :
99
- for k , value := range d .LabelValues {
100
+ d .LabelValues .Range (func (k , val interface {}) bool {
101
+ value := val .(* DnstapPrometheusOutputMetricsValues )
100
102
if time .Now ().Sub (value .LastUpdate ) > time .Second * time .Duration (d .Expire ) {
101
103
d .Vec .DeleteLabelValues (value .Values ... )
102
- delete ( d .LabelValues , k )
104
+ d .LabelValues . Delete ( k )
103
105
}
104
- }
106
+ return true
107
+ })
105
108
}
106
109
}
107
110
}
0 commit comments