-
Notifications
You must be signed in to change notification settings - Fork 99
/
standard_normalizer.go
419 lines (393 loc) · 19 KB
/
standard_normalizer.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package normalization
import (
"time"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector/internal/datapointstorage"
)
// NewStandardNormalizer performs normalization on cumulative points which:
//
// (a) don't have a start time, OR
// (b) have been sent a preceding "reset" point as described in https://github.com/open-telemetry/opentelemetry-specification/blob/9555f9594c7ffe5dc333b53da5e0f880026cead1/specification/metrics/datamodel.md#resets-and-gaps
//
// The first point without a start time or the reset point is cached, and is
// NOT exported. Subsequent points "subtract" the initial point prior to exporting.
// This normalizer also detects subsequent resets, and produces a new start time for those points.
// It doesn't modify values after a reset, but does give it a new start time.
func NewStandardNormalizer(shutdown <-chan struct{}, logger *zap.Logger) Normalizer {
return &standardNormalizer{
startCache: datapointstorage.NewCache(shutdown),
previousCache: datapointstorage.NewCache(shutdown),
log: logger,
}
}
type standardNormalizer struct {
startCache *datapointstorage.Cache
previousCache *datapointstorage.Cache
log *zap.Logger
}
func (s *standardNormalizer) NormalizeExponentialHistogramDataPoint(point pmetric.ExponentialHistogramDataPoint, identifier string) (pmetric.ExponentialHistogramDataPoint, bool) {
start, hasStart := s.startCache.GetExponentialHistogramDataPoint(identifier)
if !hasStart {
if point.StartTimestamp() == 0 || !point.StartTimestamp().AsTime().Before(point.Timestamp().AsTime()) {
// This is the first time we've seen this metric, or we received
// an explicit reset point as described in
// https://github.com/open-telemetry/opentelemetry-specification/blob/9555f9594c7ffe5dc333b53da5e0f880026cead1/specification/metrics/datamodel.md#resets-and-gaps
// Record it in history and drop the point.
s.startCache.SetExponentialHistogramDataPoint(identifier, point)
s.previousCache.SetExponentialHistogramDataPoint(identifier, point)
return pmetric.ExponentialHistogramDataPoint{}, false
}
// No normalization required, since we haven't cached anything, and the start TS is non-zero.
return point, true
}
// TODO(#366): It is possible, but difficult to compare exponential
// histograms with different scales. For now, treat a change in
// scale as a reset. Drop this point, and normalize against it for
// subsequent points.
if point.Scale() != start.Scale() {
s.startCache.SetExponentialHistogramDataPoint(identifier, point)
s.previousCache.SetExponentialHistogramDataPoint(identifier, point)
return pmetric.ExponentialHistogramDataPoint{}, false
}
previous, hasPrevious := s.previousCache.GetExponentialHistogramDataPoint(identifier)
if !hasPrevious {
// This should never happen, but fall-back to the start point if we
// don't find a previous point
previous = start
}
if !point.StartTimestamp().AsTime().Before(point.Timestamp().AsTime()) ||
(point.StartTimestamp() == 0 && lessThanExponentialHistogramDataPoint(point, previous)) {
// Make a copy so we don't mutate underlying data
newPoint := pmetric.NewExponentialHistogramDataPoint()
// This is a reset point, but we have seen this timeseries before, so we know the reset happened in the time period since the last point.
// Assume the reset occurred at T - 1 ms, and leave the value untouched.
point.CopyTo(newPoint)
newPoint.SetStartTimestamp(pcommon.Timestamp(uint64(point.Timestamp()) - uint64(time.Millisecond)))
s.previousCache.SetExponentialHistogramDataPoint(identifier, newPoint)
// For subsequent points, we don't want to modify the value, but we do
// want to make the start timestamp match the point we write here.
// Store a point with the same timestamps, but zero value to achieve
// that behavior.
zeroPoint := pmetric.NewExponentialHistogramDataPoint()
zeroPoint.SetTimestamp(newPoint.StartTimestamp())
zeroPoint.SetScale(newPoint.Scale())
s.startCache.SetExponentialHistogramDataPoint(identifier, zeroPoint)
return newPoint, true
}
if !start.Timestamp().AsTime().Before(point.Timestamp().AsTime()) {
// We found a cached start timestamp that wouldn't produce a valid point.
// Drop it and log.
s.log.Info(
"data point being processed older than last recorded reset, will not be emitted",
zap.String("lastRecordedReset", start.Timestamp().String()),
zap.String("dataPoint", point.Timestamp().String()),
)
return pmetric.ExponentialHistogramDataPoint{}, false
}
// There was no reset, so normalize the point against the start point
newPoint := subtractExponentialHistogramDataPoint(point, start)
s.previousCache.SetExponentialHistogramDataPoint(identifier, newPoint)
return newPoint, true
}
// lessThanExponentialHistogramDataPoint returns a < b.
func lessThanExponentialHistogramDataPoint(a, b pmetric.ExponentialHistogramDataPoint) bool {
return a.Count() < b.Count() || a.Sum() < b.Sum()
}
// subtractExponentialHistogramDataPoint returns a - b.
func subtractExponentialHistogramDataPoint(a, b pmetric.ExponentialHistogramDataPoint) pmetric.ExponentialHistogramDataPoint {
// Make a copy so we don't mutate underlying data
newPoint := pmetric.NewExponentialHistogramDataPoint()
a.CopyTo(newPoint)
// Use the timestamp from the normalization point
newPoint.SetStartTimestamp(b.Timestamp())
// Adjust the value based on the start point's value
newPoint.SetCount(a.Count() - b.Count())
// We drop points without a sum, so no need to check here.
newPoint.SetSum(a.Sum() - b.Sum())
newPoint.SetZeroCount(a.ZeroCount() - b.ZeroCount())
newPoint.Positive().BucketCounts().FromRaw(subtractExponentialBuckets(a.Positive(), b.Positive()))
newPoint.Negative().BucketCounts().FromRaw(subtractExponentialBuckets(a.Negative(), b.Negative()))
return newPoint
}
// subtractExponentialBuckets returns a - b.
func subtractExponentialBuckets(a, b pmetric.ExponentialHistogramDataPointBuckets) []uint64 {
newBuckets := make([]uint64, a.BucketCounts().Len())
offsetDiff := int(a.Offset() - b.Offset())
for i := 0; i < a.BucketCounts().Len(); i++ {
bOffset := i + offsetDiff
// if there is no corresponding bucket for the starting BucketCounts, don't normalize
if bOffset < 0 || bOffset >= b.BucketCounts().Len() {
newBuckets[i] = a.BucketCounts().At(i)
} else {
newBuckets[i] = a.BucketCounts().At(i) - b.BucketCounts().At(bOffset)
}
}
return newBuckets
}
func (s *standardNormalizer) NormalizeHistogramDataPoint(point pmetric.HistogramDataPoint, identifier string) (pmetric.HistogramDataPoint, bool) {
start, hasStart := s.startCache.GetHistogramDataPoint(identifier)
if !hasStart {
if point.StartTimestamp() == 0 || !point.StartTimestamp().AsTime().Before(point.Timestamp().AsTime()) {
// This is the first time we've seen this metric, or we received
// an explicit reset point as described in
// https://github.com/open-telemetry/opentelemetry-specification/blob/9555f9594c7ffe5dc333b53da5e0f880026cead1/specification/metrics/datamodel.md#resets-and-gaps
// Record it in history and drop the point.
s.startCache.SetHistogramDataPoint(identifier, point)
s.previousCache.SetHistogramDataPoint(identifier, point)
return pmetric.HistogramDataPoint{}, false
}
// No normalization required, since we haven't cached anything, and the start TS is non-zero.
return point, true
}
// The number of buckets changed, so we can't normalize points anymore.
// Treat this as a reset.
if !bucketBoundariesEqual(point.ExplicitBounds(), start.ExplicitBounds()) {
s.startCache.SetHistogramDataPoint(identifier, point)
s.previousCache.SetHistogramDataPoint(identifier, point)
return pmetric.HistogramDataPoint{}, false
}
previous, hasPrevious := s.previousCache.GetHistogramDataPoint(identifier)
if !hasPrevious {
// This should never happen, but fall-back to the start point if we
// don't find a previous point
previous = start
}
if !point.StartTimestamp().AsTime().Before(point.Timestamp().AsTime()) ||
(point.StartTimestamp() == 0 && lessThanHistogramDataPoint(point, previous)) {
// Make a copy so we don't mutate underlying data
newPoint := pmetric.NewHistogramDataPoint()
// This is a reset point, but we have seen this timeseries before, so we know the reset happened in the time period since the last point.
// Assume the reset occurred at T - 1 ms, and leave the value untouched.
point.CopyTo(newPoint)
newPoint.SetStartTimestamp(pcommon.Timestamp(uint64(point.Timestamp()) - uint64(time.Millisecond)))
s.previousCache.SetHistogramDataPoint(identifier, newPoint)
// For subsequent points, we don't want to modify the value, but we do
// want to make the start timestamp match the point we write here.
// Store a point with the same timestamps, but zero value to achieve
// that behavior.
zeroPoint := pmetric.NewHistogramDataPoint()
zeroPoint.SetTimestamp(newPoint.StartTimestamp())
newPoint.ExplicitBounds().CopyTo(zeroPoint.ExplicitBounds())
zeroPoint.BucketCounts().FromRaw(make([]uint64, newPoint.BucketCounts().Len()))
s.startCache.SetHistogramDataPoint(identifier, zeroPoint)
return newPoint, true
}
if !start.Timestamp().AsTime().Before(point.Timestamp().AsTime()) {
// We found a cached start timestamp that wouldn't produce a valid point.
// Drop it and log.
s.log.Info(
"data point being processed older than last recorded reset, will not be emitted",
zap.String("lastRecordedReset", start.Timestamp().String()),
zap.String("dataPoint", point.Timestamp().String()),
)
return pmetric.HistogramDataPoint{}, false
}
// There was no reset, so normalize the point against the start point
newPoint := subtractHistogramDataPoint(point, start)
s.previousCache.SetHistogramDataPoint(identifier, newPoint)
return newPoint, true
}
// lessThanHistogramDataPoint returns a < b.
func lessThanHistogramDataPoint(a, b pmetric.HistogramDataPoint) bool {
return a.Count() < b.Count() || a.Sum() < b.Sum()
}
// subtractHistogramDataPoint returns a - b.
func subtractHistogramDataPoint(a, b pmetric.HistogramDataPoint) pmetric.HistogramDataPoint {
// Make a copy so we don't mutate underlying data
newPoint := pmetric.NewHistogramDataPoint()
a.CopyTo(newPoint)
// Use the timestamp from the normalization point
newPoint.SetStartTimestamp(b.Timestamp())
// Adjust the value based on the start point's value
newPoint.SetCount(a.Count() - b.Count())
// We drop points without a sum, so no need to check here.
newPoint.SetSum(a.Sum() - b.Sum())
aBuckets := a.BucketCounts()
bBuckets := b.BucketCounts()
newBuckets := make([]uint64, aBuckets.Len())
for i := 0; i < aBuckets.Len(); i++ {
newBuckets[i] = aBuckets.At(i) - bBuckets.At(i)
}
newPoint.BucketCounts().FromRaw(newBuckets)
return newPoint
}
func bucketBoundariesEqual(a, b pcommon.Float64Slice) bool {
if a.Len() != b.Len() {
return false
}
for i := 0; i < a.Len(); i++ {
if a.At(i) != b.At(i) {
return false
}
}
return true
}
// NormalizeNumberDataPoint normalizes a cumulative, monotonic sum.
// It returns the normalized point, and true if the point should be kept.
func (s *standardNormalizer) NormalizeNumberDataPoint(point pmetric.NumberDataPoint, identifier string) (pmetric.NumberDataPoint, bool) {
start, hasStart := s.startCache.GetNumberDataPoint(identifier)
if !hasStart {
if point.StartTimestamp() == 0 || !point.StartTimestamp().AsTime().Before(point.Timestamp().AsTime()) {
// This is the first time we've seen this metric, or we received
// an explicit reset point as described in
// https://github.com/open-telemetry/opentelemetry-specification/blob/9555f9594c7ffe5dc333b53da5e0f880026cead1/specification/metrics/datamodel.md#resets-and-gaps
// Record it in history and drop the point.
s.startCache.SetNumberDataPoint(identifier, point)
s.previousCache.SetNumberDataPoint(identifier, point)
return pmetric.NumberDataPoint{}, false
}
// No normalization required, since we haven't cached anything, and the start TS is non-zer0.
return point, true
}
previous, hasPrevious := s.previousCache.GetNumberDataPoint(identifier)
if !hasPrevious {
// This should never happen, but fall-back to the start point if we
// don't find a previous point
previous = start
}
if !point.StartTimestamp().AsTime().Before(point.Timestamp().AsTime()) || (point.StartTimestamp() == 0 && lessThanNumberDataPoint(point, previous)) {
// Make a copy so we don't mutate underlying data
newPoint := pmetric.NewNumberDataPoint()
// This is a reset point, but we have seen this timeseries before, so we know the reset happened in the time period since the last point.
// Assume the reset occurred at T - 1 ms, and leave the value untouched.
point.CopyTo(newPoint)
newPoint.SetStartTimestamp(pcommon.Timestamp(uint64(point.Timestamp()) - uint64(time.Millisecond)))
s.previousCache.SetNumberDataPoint(identifier, newPoint)
// For subsequent points, we don't want to modify the value, but we do
// want to make the start timestamp match the point we write here.
// Store a point with the same timestamps, but zero value to achieve
// that behavior.
zeroPoint := pmetric.NewNumberDataPoint()
zeroPoint.SetTimestamp(newPoint.StartTimestamp())
s.startCache.SetNumberDataPoint(identifier, zeroPoint)
return newPoint, true
}
if !start.Timestamp().AsTime().Before(point.Timestamp().AsTime()) {
// We found a cached start timestamp that wouldn't produce a valid point.
// Drop it and log.
s.log.Info(
"data point being processed older than last recorded reset, will not be emitted",
zap.String("lastRecordedReset", start.Timestamp().String()),
zap.String("dataPoint", point.Timestamp().String()),
)
return pmetric.NumberDataPoint{}, false
}
// There was no reset, so normalize the point against the start point
newPoint := subtractNumberDataPoint(point, start)
s.previousCache.SetNumberDataPoint(identifier, newPoint)
return newPoint, true
}
// lessThanNumberDataPoint returns a < b.
func lessThanNumberDataPoint(a, b pmetric.NumberDataPoint) bool {
switch a.ValueType() {
case pmetric.NumberDataPointValueTypeInt:
return a.IntValue() < b.IntValue()
case pmetric.NumberDataPointValueTypeDouble:
return a.DoubleValue() < b.DoubleValue()
}
return false
}
// subtractNumberDataPoint returns a - b.
func subtractNumberDataPoint(a, b pmetric.NumberDataPoint) pmetric.NumberDataPoint {
// Make a copy so we don't mutate underlying data
newPoint := pmetric.NewNumberDataPoint()
a.CopyTo(newPoint)
// Use the timestamp from the normalization point
newPoint.SetStartTimestamp(b.Timestamp())
// Adjust the value based on the start point's value
switch newPoint.ValueType() {
case pmetric.NumberDataPointValueTypeInt:
newPoint.SetIntValue(a.IntValue() - b.IntValue())
case pmetric.NumberDataPointValueTypeDouble:
newPoint.SetDoubleValue(a.DoubleValue() - b.DoubleValue())
}
return newPoint
}
func (s *standardNormalizer) NormalizeSummaryDataPoint(point pmetric.SummaryDataPoint, identifier string) (pmetric.SummaryDataPoint, bool) {
start, hasStart := s.startCache.GetSummaryDataPoint(identifier)
if !hasStart {
if point.StartTimestamp() == 0 || !point.StartTimestamp().AsTime().Before(point.Timestamp().AsTime()) {
// This is the first time we've seen this metric, or we received
// an explicit reset point as described in
// https://github.com/open-telemetry/opentelemetry-specification/blob/9555f9594c7ffe5dc333b53da5e0f880026cead1/specification/metrics/datamodel.md#resets-and-gaps
// Record it in history and drop the point.
s.startCache.SetSummaryDataPoint(identifier, point)
s.previousCache.SetSummaryDataPoint(identifier, point)
return pmetric.SummaryDataPoint{}, false
}
// No normalization required, since we haven't cached anything, and the start TS is non-zer0.
return point, true
}
previous, hasPrevious := s.previousCache.GetSummaryDataPoint(identifier)
if !hasPrevious {
// This should never happen, but fall-back to the start point if we
// don't find a previous point
previous = start
}
if !point.StartTimestamp().AsTime().Before(point.Timestamp().AsTime()) || (point.StartTimestamp() == 0 && lessThanSummaryDataPoint(point, previous)) {
// Make a copy so we don't mutate underlying data
newPoint := pmetric.NewSummaryDataPoint()
// This is a reset point, but we have seen this timeseries before, so we know the reset happened in the time period since the last point.
// Assume the reset occurred at T - 1 ms, and leave the value untouched.
point.CopyTo(newPoint)
newPoint.SetStartTimestamp(pcommon.Timestamp(uint64(point.Timestamp()) - uint64(time.Millisecond)))
s.previousCache.SetSummaryDataPoint(identifier, newPoint)
// For subsequent points, we don't want to modify the value, but we do
// want to make the start timestamp match the point we write here.
// Store a point with the same timestamps, but zero value to achieve
// that behavior.
zeroPoint := pmetric.NewSummaryDataPoint()
zeroPoint.SetTimestamp(newPoint.StartTimestamp())
s.startCache.SetSummaryDataPoint(identifier, zeroPoint)
return newPoint, true
}
if !start.Timestamp().AsTime().Before(point.Timestamp().AsTime()) {
// We found a cached start timestamp that wouldn't produce a valid point.
// Drop it and log.
s.log.Info(
"data point being processed older than last recorded reset, will not be emitted",
zap.String("lastRecordedReset", start.Timestamp().String()),
zap.String("dataPoint", point.Timestamp().String()),
)
return pmetric.SummaryDataPoint{}, false
}
// There was no reset, so normalize the point against the start point
newPoint := subtractSummaryDataPoint(point, start)
s.previousCache.SetSummaryDataPoint(identifier, newPoint)
return newPoint, true
}
// lessThanSummaryDataPoint returns a < b.
func lessThanSummaryDataPoint(a, b pmetric.SummaryDataPoint) bool {
return a.Count() < b.Count() || a.Sum() < b.Sum()
}
// subtractSummaryDataPoint returns a - b.
func subtractSummaryDataPoint(a, b pmetric.SummaryDataPoint) pmetric.SummaryDataPoint {
// Make a copy so we don't mutate underlying data.
newPoint := pmetric.NewSummaryDataPoint()
// Quantile values are copied, and are not modified. Quantiles are
// computed over the same time period as sum and count, but it isn't
// possible to normalize them.
a.CopyTo(newPoint)
// Use the timestamp from the normalization point
newPoint.SetStartTimestamp(b.Timestamp())
// Adjust the value based on the start point's value
newPoint.SetCount(a.Count() - b.Count())
// We drop points without a sum, so no need to check here.
newPoint.SetSum(a.Sum() - b.Sum())
return newPoint
}