@@ -19,95 +19,107 @@ import "encoding/json"
19
19
20
20
// TelemetryCounter tracks the number of times a set of resource and attribute dimensions have been seen.
21
21
type TelemetryCounter struct {
22
- resources map [string ]* ResourceCounter
22
+ resources map [string ]* resourceCounter
23
+ commands chan func ()
23
24
}
24
25
25
26
// NewTelemetryCounter creates a new TelemetryCounter.
26
27
func NewTelemetryCounter () * TelemetryCounter {
27
- return & TelemetryCounter {
28
- resources : make (map [string ]* ResourceCounter ),
28
+ t := & TelemetryCounter {
29
+ resources : make (map [string ]* resourceCounter ),
30
+ commands : make (chan func ()),
29
31
}
32
+ go t .run ()
33
+ return t
30
34
}
31
35
32
- // Add increments the counter with the supplied dimensions.
33
- func (t * TelemetryCounter ) Add (resource , attributes map [string ]any ) {
34
- key := getDimensionKey (resource )
35
- if _ , ok := t .resources [key ]; ! ok {
36
- t .resources [key ] = NewResourceCounter (resource )
36
+ // run listens for commands to modify or read the resources.
37
+ func (t * TelemetryCounter ) run () {
38
+ for cmd := range t .commands {
39
+ cmd ()
37
40
}
38
-
39
- t .resources [key ].Add (attributes )
40
41
}
41
42
42
- // Resources returns a map of resource ID to a counter for that resource.
43
- func (t TelemetryCounter ) Resources () map [string ]* ResourceCounter {
44
- return t .resources
43
+ // Add increments the counter with the supplied dimensions.
44
+ func (t * TelemetryCounter ) Add (resource , attributes map [string ]any ) {
45
+ t .commands <- func () {
46
+ key := getDimensionKey (resource )
47
+ if _ , ok := t .resources [key ]; ! ok {
48
+ t .resources [key ] = newResourceCounter (resource )
49
+ }
50
+ t .resources [key ].add (attributes )
51
+ }
45
52
}
46
53
47
- // Reset resets the counter.
48
- func (t * TelemetryCounter ) Reset () {
49
- t .resources = make (map [string ]* ResourceCounter )
54
+ // Resources returns a map of resource ID to a counter for that resource and resets the counter.
55
+ func (t * TelemetryCounter ) Resources () map [string ]* resourceCounter {
56
+ result := make (chan map [string ]* resourceCounter )
57
+ t .commands <- func () {
58
+ result <- t .resources
59
+ t .resources = make (map [string ]* resourceCounter ) // Reset the counter
60
+ }
61
+ return <- result
50
62
}
51
63
52
- // ResourceCounter dimensions the counter by resource.
53
- type ResourceCounter struct {
64
+ // resourceCounter dimensions the counter by resource.
65
+ type resourceCounter struct {
54
66
values map [string ]any
55
- attributes map [string ]* AttributeCounter
67
+ attributes map [string ]* attributeCounter
56
68
}
57
69
58
- // NewResourceCounter creates a new ResourceCounter.
59
- func NewResourceCounter (values map [string ]any ) * ResourceCounter {
60
- return & ResourceCounter {
70
+ // newResourceCounter creates a new ResourceCounter.
71
+ func newResourceCounter (values map [string ]any ) * resourceCounter {
72
+ return & resourceCounter {
61
73
values : values ,
62
- attributes : map [string ]* AttributeCounter {},
74
+ attributes : map [string ]* attributeCounter {},
63
75
}
64
76
}
65
77
66
- // Add increments the counter with the supplied dimensions.
67
- func (r * ResourceCounter ) Add (attributes map [string ]any ) {
78
+ // add increments the counter with the supplied dimensions.
79
+ func (r * resourceCounter ) add (attributes map [string ]any ) {
68
80
key := getDimensionKey (attributes )
69
81
if _ , ok := r .attributes [key ]; ! ok {
70
- r .attributes [key ] = NewAttributeCounter (attributes )
82
+ r .attributes [key ] = newAttributeCounter (attributes )
71
83
}
72
84
73
- r .attributes [key ].Add ()
85
+ r .attributes [key ].add ()
74
86
}
75
87
76
88
// Attributes returns a map of attribute set ID to a counter for that attribute set.
77
- func (r ResourceCounter ) Attributes () map [string ]* AttributeCounter {
89
+ func (r resourceCounter ) Attributes () map [string ]* attributeCounter {
78
90
return r .attributes
79
91
}
80
92
81
93
// Values returns the raw map value of the resource that this counter counts.
82
- func (r ResourceCounter ) Values () map [string ]any {
94
+ func (r resourceCounter ) Values () map [string ]any {
83
95
return r .values
84
96
}
85
97
86
- // AttributeCounter dimensions the counter by attributes.
87
- type AttributeCounter struct {
98
+ // attributeCounter dimensions the counter by attributes.
99
+ type attributeCounter struct {
88
100
values map [string ]any
89
101
count int
90
102
}
91
103
92
- // NewAttributeCounter creates a new AttributeCounter.
93
- func NewAttributeCounter (values map [string ]any ) * AttributeCounter {
94
- return & AttributeCounter {
104
+ // newAttributeCounter creates a new AttributeCounter.
105
+ func newAttributeCounter (values map [string ]any ) * attributeCounter {
106
+ return & attributeCounter {
95
107
values : values ,
96
108
}
97
109
}
98
110
99
- // Add increments the counter.
100
- func (a * AttributeCounter ) Add () {
111
+ // add increments the counter.
112
+ func (a * attributeCounter ) add () {
101
113
a .count ++
102
114
}
103
115
104
116
// Count returns the number of counts for this attribute counter.
105
- func (a AttributeCounter ) Count () int {
117
+ func (a attributeCounter ) Count () int {
106
118
return a .count
107
119
}
108
120
109
121
// Values returns the attribute map that this counter tracks.
110
- func (a AttributeCounter ) Values () map [string ]any {
122
+ func (a attributeCounter ) Values () map [string ]any {
111
123
return a .values
112
124
}
113
125
0 commit comments