@@ -26,10 +26,10 @@ import (
26
26
)
27
27
28
28
type accumulatedValue struct {
29
- // value contains a metric with exactly one aggregated datapoint
29
+ // value contains a metric with exactly one aggregated datapoint.
30
30
value pdata.Metric
31
- // stored indicates when metric was stored
32
- stored time.Time
31
+ // updated indicates when metric was last changed.
32
+ updated time.Time
33
33
34
34
instrumentationLibrary pdata.InstrumentationLibrary
35
35
}
@@ -49,7 +49,7 @@ type lastValueAccumulator struct {
49
49
registeredMetrics sync.Map
50
50
51
51
// metricExpiration contains duration for which metric
52
- // should be served after it was stored
52
+ // should be served after it was updated
53
53
metricExpiration time.Duration
54
54
}
55
55
@@ -63,104 +63,103 @@ func newAccumulator(logger *zap.Logger, metricExpiration time.Duration) accumula
63
63
64
64
// Accumulate stores one datapoint per metric
65
65
func (a * lastValueAccumulator ) Accumulate (rm pdata.ResourceMetrics ) (n int ) {
66
+ now := time .Now ()
66
67
ilms := rm .InstrumentationLibraryMetrics ()
67
68
68
69
for i := 0 ; i < ilms .Len (); i ++ {
69
70
ilm := ilms .At (i )
70
71
71
72
metrics := ilm .Metrics ()
72
73
for j := 0 ; j < metrics .Len (); j ++ {
73
- n += a .addMetric (metrics .At (j ), ilm .InstrumentationLibrary ())
74
+ n += a .addMetric (metrics .At (j ), ilm .InstrumentationLibrary (), now )
74
75
}
75
76
}
76
77
77
78
return
78
79
}
79
80
80
- func (a * lastValueAccumulator ) addMetric (metric pdata.Metric , il pdata.InstrumentationLibrary ) int {
81
+ func (a * lastValueAccumulator ) addMetric (metric pdata.Metric , il pdata.InstrumentationLibrary , now time. Time ) int {
81
82
a .logger .Debug (fmt .Sprintf ("accumulating metric: %s" , metric .Name ()))
82
83
83
84
switch metric .DataType () {
84
85
case pdata .MetricDataTypeIntGauge :
85
- return a .accumulateIntGauge (metric , il )
86
+ return a .accumulateIntGauge (metric , il , now )
86
87
case pdata .MetricDataTypeIntSum :
87
- return a .accumulateIntSum (metric , il )
88
+ return a .accumulateIntSum (metric , il , now )
88
89
case pdata .MetricDataTypeDoubleGauge :
89
- return a .accumulateDoubleGauge (metric , il )
90
+ return a .accumulateDoubleGauge (metric , il , now )
90
91
case pdata .MetricDataTypeDoubleSum :
91
- return a .accumulateDoubleSum (metric , il )
92
+ return a .accumulateDoubleSum (metric , il , now )
92
93
case pdata .MetricDataTypeIntHistogram :
93
- return a .accumulateIntHistogram (metric , il )
94
+ return a .accumulateIntHistogram (metric , il , now )
94
95
case pdata .MetricDataTypeHistogram :
95
- return a .accumulateDoubleHistogram (metric , il )
96
+ return a .accumulateDoubleHistogram (metric , il , now )
96
97
}
97
98
98
99
return 0
99
100
}
100
101
101
- func (a * lastValueAccumulator ) accumulateIntGauge (metric pdata.Metric , il pdata.InstrumentationLibrary ) (n int ) {
102
+ func (a * lastValueAccumulator ) accumulateIntGauge (metric pdata.Metric , il pdata.InstrumentationLibrary , now time. Time ) (n int ) {
102
103
dps := metric .IntGauge ().DataPoints ()
103
104
for i := 0 ; i < dps .Len (); i ++ {
104
105
ip := dps .At (i )
105
106
106
- ts := ip .Timestamp ().AsTime ()
107
107
signature := timeseriesSignature (il .Name (), metric , ip .LabelsMap ())
108
108
109
109
v , ok := a .registeredMetrics .Load (signature )
110
110
if ! ok {
111
111
m := createMetric (metric )
112
112
m .IntGauge ().DataPoints ().Append (ip )
113
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
113
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
114
114
n ++
115
115
continue
116
116
}
117
117
mv := v .(* accumulatedValue )
118
118
119
- if ts .Before (mv .value .IntGauge ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
119
+ if ip . Timestamp (). AsTime () .Before (mv .value .IntGauge ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
120
120
// only keep datapoint with latest timestamp
121
121
continue
122
122
}
123
123
124
124
m := createMetric (metric )
125
125
m .IntGauge ().DataPoints ().Append (ip )
126
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
126
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
127
127
n ++
128
128
}
129
129
return
130
130
}
131
131
132
- func (a * lastValueAccumulator ) accumulateDoubleGauge (metric pdata.Metric , il pdata.InstrumentationLibrary ) (n int ) {
132
+ func (a * lastValueAccumulator ) accumulateDoubleGauge (metric pdata.Metric , il pdata.InstrumentationLibrary , now time. Time ) (n int ) {
133
133
dps := metric .DoubleGauge ().DataPoints ()
134
134
for i := 0 ; i < dps .Len (); i ++ {
135
135
ip := dps .At (i )
136
136
137
- ts := ip .Timestamp ().AsTime ()
138
137
signature := timeseriesSignature (il .Name (), metric , ip .LabelsMap ())
139
138
140
139
v , ok := a .registeredMetrics .Load (signature )
141
140
if ! ok {
142
141
m := createMetric (metric )
143
142
m .DoubleGauge ().DataPoints ().Append (ip )
144
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
143
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
145
144
n ++
146
145
continue
147
146
}
148
147
mv := v .(* accumulatedValue )
149
148
150
- if ts .Before (mv .value .DoubleGauge ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
149
+ if ip . Timestamp (). AsTime () .Before (mv .value .DoubleGauge ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
151
150
// only keep datapoint with latest timestamp
152
151
continue
153
152
}
154
153
155
154
m := createMetric (metric )
156
155
m .DoubleGauge ().DataPoints ().Append (ip )
157
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
156
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
158
157
n ++
159
158
}
160
159
return
161
160
}
162
161
163
- func (a * lastValueAccumulator ) accumulateIntSum (metric pdata.Metric , il pdata.InstrumentationLibrary ) (n int ) {
162
+ func (a * lastValueAccumulator ) accumulateIntSum (metric pdata.Metric , il pdata.InstrumentationLibrary , now time. Time ) (n int ) {
164
163
intSum := metric .IntSum ()
165
164
166
165
// Drop metrics with non-cumulative aggregations
@@ -172,7 +171,6 @@ func (a *lastValueAccumulator) accumulateIntSum(metric pdata.Metric, il pdata.In
172
171
for i := 0 ; i < dps .Len (); i ++ {
173
172
ip := dps .At (i )
174
173
175
- ts := ip .Timestamp ().AsTime ()
176
174
signature := timeseriesSignature (il .Name (), metric , ip .LabelsMap ())
177
175
178
176
v , ok := a .registeredMetrics .Load (signature )
@@ -181,13 +179,13 @@ func (a *lastValueAccumulator) accumulateIntSum(metric pdata.Metric, il pdata.In
181
179
m .IntSum ().SetIsMonotonic (metric .IntSum ().IsMonotonic ())
182
180
m .IntSum ().SetAggregationTemporality (pdata .AggregationTemporalityCumulative )
183
181
m .IntSum ().DataPoints ().Append (ip )
184
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
182
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
185
183
n ++
186
184
continue
187
185
}
188
186
mv := v .(* accumulatedValue )
189
187
190
- if ts .Before (mv .value .IntSum ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
188
+ if ip . Timestamp (). AsTime () .Before (mv .value .IntSum ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
191
189
// only keep datapoint with latest timestamp
192
190
continue
193
191
}
@@ -196,13 +194,13 @@ func (a *lastValueAccumulator) accumulateIntSum(metric pdata.Metric, il pdata.In
196
194
m .IntSum ().SetIsMonotonic (metric .IntSum ().IsMonotonic ())
197
195
m .IntSum ().SetAggregationTemporality (pdata .AggregationTemporalityCumulative )
198
196
m .IntSum ().DataPoints ().Append (ip )
199
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
197
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
200
198
n ++
201
199
}
202
200
return
203
201
}
204
202
205
- func (a * lastValueAccumulator ) accumulateDoubleSum (metric pdata.Metric , il pdata.InstrumentationLibrary ) (n int ) {
203
+ func (a * lastValueAccumulator ) accumulateDoubleSum (metric pdata.Metric , il pdata.InstrumentationLibrary , now time. Time ) (n int ) {
206
204
doubleSum := metric .DoubleSum ()
207
205
208
206
// Drop metrics with non-cumulative aggregations
@@ -214,7 +212,6 @@ func (a *lastValueAccumulator) accumulateDoubleSum(metric pdata.Metric, il pdata
214
212
for i := 0 ; i < dps .Len (); i ++ {
215
213
ip := dps .At (i )
216
214
217
- ts := ip .Timestamp ().AsTime ()
218
215
signature := timeseriesSignature (il .Name (), metric , ip .LabelsMap ())
219
216
220
217
v , ok := a .registeredMetrics .Load (signature )
@@ -223,13 +220,13 @@ func (a *lastValueAccumulator) accumulateDoubleSum(metric pdata.Metric, il pdata
223
220
m .DoubleSum ().SetIsMonotonic (metric .DoubleSum ().IsMonotonic ())
224
221
m .DoubleSum ().SetAggregationTemporality (pdata .AggregationTemporalityCumulative )
225
222
m .DoubleSum ().DataPoints ().Append (ip )
226
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
223
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
227
224
n ++
228
225
continue
229
226
}
230
227
mv := v .(* accumulatedValue )
231
228
232
- if ts .Before (mv .value .DoubleSum ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
229
+ if ip . Timestamp (). AsTime () .Before (mv .value .DoubleSum ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
233
230
// only keep datapoint with latest timestamp
234
231
continue
235
232
}
@@ -238,13 +235,13 @@ func (a *lastValueAccumulator) accumulateDoubleSum(metric pdata.Metric, il pdata
238
235
m .DoubleSum ().SetIsMonotonic (metric .DoubleSum ().IsMonotonic ())
239
236
m .DoubleSum ().SetAggregationTemporality (pdata .AggregationTemporalityCumulative )
240
237
m .DoubleSum ().DataPoints ().Append (ip )
241
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
238
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
242
239
n ++
243
240
}
244
241
return
245
242
}
246
243
247
- func (a * lastValueAccumulator ) accumulateIntHistogram (metric pdata.Metric , il pdata.InstrumentationLibrary ) (n int ) {
244
+ func (a * lastValueAccumulator ) accumulateIntHistogram (metric pdata.Metric , il pdata.InstrumentationLibrary , now time. Time ) (n int ) {
248
245
intHistogram := metric .IntHistogram ()
249
246
250
247
// Drop metrics with non-cumulative aggregations
@@ -256,34 +253,33 @@ func (a *lastValueAccumulator) accumulateIntHistogram(metric pdata.Metric, il pd
256
253
for i := 0 ; i < dps .Len (); i ++ {
257
254
ip := dps .At (i )
258
255
259
- ts := ip .Timestamp ().AsTime ()
260
256
signature := timeseriesSignature (il .Name (), metric , ip .LabelsMap ())
261
257
262
258
v , ok := a .registeredMetrics .Load (signature )
263
259
if ! ok {
264
260
m := createMetric (metric )
265
261
m .IntHistogram ().DataPoints ().Append (ip )
266
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
262
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
267
263
n ++
268
264
continue
269
265
}
270
266
mv := v .(* accumulatedValue )
271
267
272
- if ts .Before (mv .value .IntHistogram ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
268
+ if ip . Timestamp (). AsTime () .Before (mv .value .IntHistogram ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
273
269
// only keep datapoint with latest timestamp
274
270
continue
275
271
}
276
272
277
273
m := createMetric (metric )
278
274
m .IntHistogram ().DataPoints ().Append (ip )
279
275
m .IntHistogram ().SetAggregationTemporality (pdata .AggregationTemporalityCumulative )
280
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
276
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
281
277
n ++
282
278
}
283
279
return
284
280
}
285
281
286
- func (a * lastValueAccumulator ) accumulateDoubleHistogram (metric pdata.Metric , il pdata.InstrumentationLibrary ) (n int ) {
282
+ func (a * lastValueAccumulator ) accumulateDoubleHistogram (metric pdata.Metric , il pdata.InstrumentationLibrary , now time. Time ) (n int ) {
287
283
doubleHistogram := metric .Histogram ()
288
284
289
285
// Drop metrics with non-cumulative aggregations
@@ -295,28 +291,27 @@ func (a *lastValueAccumulator) accumulateDoubleHistogram(metric pdata.Metric, il
295
291
for i := 0 ; i < dps .Len (); i ++ {
296
292
ip := dps .At (i )
297
293
298
- ts := ip .Timestamp ().AsTime ()
299
294
signature := timeseriesSignature (il .Name (), metric , ip .LabelsMap ())
300
295
301
296
v , ok := a .registeredMetrics .Load (signature )
302
297
if ! ok {
303
298
m := createMetric (metric )
304
299
m .Histogram ().DataPoints ().Append (ip )
305
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
300
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
306
301
n ++
307
302
continue
308
303
}
309
304
mv := v .(* accumulatedValue )
310
305
311
- if ts .Before (mv .value .Histogram ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
306
+ if ip . Timestamp (). AsTime () .Before (mv .value .Histogram ().DataPoints ().At (0 ).Timestamp ().AsTime ()) {
312
307
// only keep datapoint with latest timestamp
313
308
continue
314
309
}
315
310
316
311
m := createMetric (metric )
317
312
m .Histogram ().DataPoints ().Append (ip )
318
313
m .Histogram ().SetAggregationTemporality (pdata .AggregationTemporalityCumulative )
319
- a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , stored : time . Now () })
314
+ a .registeredMetrics .Store (signature , & accumulatedValue {value : m , instrumentationLibrary : il , updated : now })
320
315
n ++
321
316
}
322
317
return
@@ -326,11 +321,12 @@ func (a *lastValueAccumulator) accumulateDoubleHistogram(metric pdata.Metric, il
326
321
func (a * lastValueAccumulator ) Collect () []pdata.Metric {
327
322
a .logger .Debug ("Accumulator collect called" )
328
323
329
- res := make ([]pdata.Metric , 0 )
324
+ var res []pdata.Metric
325
+ expirationTime := time .Now ().Add (- a .metricExpiration )
330
326
331
327
a .registeredMetrics .Range (func (key , value interface {}) bool {
332
328
v := value .(* accumulatedValue )
333
- if time . Now (). After (v .stored . Add ( a . metricExpiration ) ) {
329
+ if expirationTime . After (v .updated ) {
334
330
a .logger .Debug (fmt .Sprintf ("metric expired: %s" , v .value .Name ()))
335
331
a .registeredMetrics .Delete (key )
336
332
return true
0 commit comments