|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package internal |
| 5 | + |
| 6 | +import ( |
| 7 | + "log" |
| 8 | + "math" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/aws/amazon-cloudwatch-agent/internal/mapWithExpiry" |
| 12 | + "github.com/prometheus/prometheus/model/value" |
| 13 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 14 | + "go.opentelemetry.io/collector/pdata/pmetric" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + CleanUpTimeThreshold = 60 * 1000 // 1 minute |
| 19 | + CacheTTL = 5 * time.Minute // 5 minutes |
| 20 | +) |
| 21 | + |
| 22 | +type dataPoint struct { |
| 23 | + value float64 |
| 24 | + timestamp pcommon.Timestamp |
| 25 | +} |
| 26 | +type DeltaCalculator struct { |
| 27 | + preDataPoints *mapWithExpiry.MapWithExpiry |
| 28 | + lastCleanUpTime pcommon.Timestamp |
| 29 | +} |
| 30 | + |
| 31 | +func (dc *DeltaCalculator) Calculate(m pmetric.Metric) { |
| 32 | + |
| 33 | + switch m.Type() { |
| 34 | + case pmetric.MetricTypeSum: |
| 35 | + // only calculate delta if it's a cumulative sum metric |
| 36 | + if m.Sum().AggregationTemporality() != pmetric.AggregationTemporalityCumulative { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + dps := m.Sum().DataPoints() |
| 41 | + dps.RemoveIf(func(dp pmetric.NumberDataPoint) bool { |
| 42 | + identity := MetricIdentity{ |
| 43 | + name: m.Name(), |
| 44 | + tags: dp.Attributes().AsRaw(), |
| 45 | + } |
| 46 | + metricKey := identity.getKey() |
| 47 | + |
| 48 | + var curVal float64 |
| 49 | + switch dp.ValueType() { |
| 50 | + case pmetric.NumberDataPointValueTypeInt: |
| 51 | + curVal = float64(dp.IntValue()) |
| 52 | + case pmetric.NumberDataPointValueTypeDouble: |
| 53 | + curVal = dp.DoubleValue() |
| 54 | + case pmetric.NumberDataPointValueTypeEmpty: |
| 55 | + fallthrough |
| 56 | + default: |
| 57 | + // cannot handle the type so drop the data point |
| 58 | + log.Printf("D! DeltaCalculator.calculate: Drop metric with value type: %v", dp.ValueType()) |
| 59 | + return true |
| 60 | + } |
| 61 | + |
| 62 | + if !isValueValid(dp.DoubleValue()) { |
| 63 | + log.Printf("D! DeltaCalculator.calculate: Drop metric with NaN or Inf value: %v", curVal) |
| 64 | + //When the raws values are like this: 1, 2, 3, 4, NaN, NaN, NaN, ..., 100, 101, 102, |
| 65 | + //and the previous value is not reset, we will get a wrong delta value (at 100) as 100 - 4 = 96 |
| 66 | + //To avoid this issue, we reset the previous value whenever an invalid value is encountered |
| 67 | + dc.preDataPoints.Delete(metricKey) |
| 68 | + return true |
| 69 | + } |
| 70 | + |
| 71 | + curTime := dp.Timestamp() |
| 72 | + |
| 73 | + dropValue := false |
| 74 | + if newVal, ok := dc.calculateDatapoint(metricKey, curVal, curTime); ok { |
| 75 | + switch dp.ValueType() { |
| 76 | + case pmetric.NumberDataPointValueTypeInt: |
| 77 | + dp.SetIntValue(int64(newVal)) |
| 78 | + case pmetric.NumberDataPointValueTypeDouble: |
| 79 | + dp.SetDoubleValue(newVal) |
| 80 | + case pmetric.NumberDataPointValueTypeEmpty: |
| 81 | + fallthrough |
| 82 | + default: |
| 83 | + log.Printf("D! DeltaCalculator.calculate: Drop metric with value type: %v", dp.ValueType()) |
| 84 | + return true |
| 85 | + } |
| 86 | + } else { |
| 87 | + // Drop the initial value for delta calculations |
| 88 | + dropValue = true |
| 89 | + } |
| 90 | + |
| 91 | + // Clean up the stale cache periodically |
| 92 | + if curTime-dc.lastCleanUpTime >= CleanUpTimeThreshold { |
| 93 | + dc.preDataPoints.CleanUp(time.Now()) |
| 94 | + dc.lastCleanUpTime = curTime |
| 95 | + } |
| 96 | + |
| 97 | + dc.preDataPoints.Set(metricKey, dataPoint{value: curVal, timestamp: curTime}) |
| 98 | + |
| 99 | + return dropValue |
| 100 | + }) |
| 101 | + |
| 102 | + case pmetric.MetricTypeSummary: |
| 103 | + dps := m.Summary().DataPoints() |
| 104 | + dps.RemoveIf(func(dp pmetric.SummaryDataPoint) bool { |
| 105 | + sumIdentity := MetricIdentity{ |
| 106 | + name: m.Name() + "_sum", |
| 107 | + tags: dp.Attributes().AsRaw(), |
| 108 | + } |
| 109 | + sumKey := sumIdentity.getKey() |
| 110 | + |
| 111 | + curSum := dp.Sum() |
| 112 | + if !isValueValid(curSum) { |
| 113 | + log.Printf("D! DeltaCalculator.calculate: Drop metric with NaN or Inf value: %v", curSum) |
| 114 | + //When the raws values are like this: 1, 2, 3, 4, NaN, NaN, NaN, ..., 100, 101, 102, |
| 115 | + //and the previous value is not reset, we will get a wrong delta value (at 100) as 100 - 4 = 96 |
| 116 | + //To avoid this issue, we reset the previous value whenever an invalid value is encountered |
| 117 | + dc.preDataPoints.Delete(sumKey) |
| 118 | + return true |
| 119 | + } |
| 120 | + |
| 121 | + curTime := dp.Timestamp() |
| 122 | + curCount := float64(dp.Count()) |
| 123 | + countIdentity := MetricIdentity{ |
| 124 | + name: m.Name() + "_count", |
| 125 | + tags: dp.Attributes().AsRaw(), |
| 126 | + } |
| 127 | + countKey := countIdentity.getKey() |
| 128 | + |
| 129 | + dropValue := false |
| 130 | + if newVal, ok := dc.calculateDatapoint(sumKey, curSum, curTime); ok { |
| 131 | + dp.SetSum(newVal) |
| 132 | + } else { |
| 133 | + dropValue = true |
| 134 | + } |
| 135 | + if newVal, ok := dc.calculateDatapoint(countKey, curCount, curTime); ok { |
| 136 | + dp.SetCount(uint64(newVal)) |
| 137 | + } else { |
| 138 | + // we shouldn't ever see sum without count, but drop the entire summary if either are not present |
| 139 | + dropValue = true |
| 140 | + } |
| 141 | + |
| 142 | + // Clean up the stale cache periodically |
| 143 | + if curTime-dc.lastCleanUpTime >= CleanUpTimeThreshold { |
| 144 | + dc.preDataPoints.CleanUp(time.Now()) |
| 145 | + dc.lastCleanUpTime = curTime |
| 146 | + } |
| 147 | + |
| 148 | + dc.preDataPoints.Set(countKey, dataPoint{value: curCount, timestamp: curTime}) |
| 149 | + dc.preDataPoints.Set(sumKey, dataPoint{value: curSum, timestamp: curTime}) |
| 150 | + |
| 151 | + return dropValue |
| 152 | + }) |
| 153 | + |
| 154 | + case pmetric.MetricTypeEmpty, pmetric.MetricTypeGauge, pmetric.MetricTypeExponentialHistogram: |
| 155 | + fallthrough |
| 156 | + default: |
| 157 | + log.Printf("W! DeltaCalculator.Calculate: ignoring metric %s", m.Name()) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +func (dc *DeltaCalculator) calculateDatapoint(key string, value float64, curTime pcommon.Timestamp) (float64, bool) { |
| 164 | + if v, ok := dc.preDataPoints.Get(key); ok { |
| 165 | + preDataPoint := v.(dataPoint) |
| 166 | + newVal := value |
| 167 | + if curTime > preDataPoint.timestamp { |
| 168 | + if value >= preDataPoint.value { |
| 169 | + newVal = value - preDataPoint.value |
| 170 | + } else { |
| 171 | + // the counter has been reset, keep the current value as delta |
| 172 | + } |
| 173 | + } |
| 174 | + return newVal, true |
| 175 | + } |
| 176 | + return value, false |
| 177 | +} |
| 178 | + |
| 179 | +func NewDeltaCalculator() *DeltaCalculator { |
| 180 | + return &DeltaCalculator{preDataPoints: mapWithExpiry.NewMapWithExpiry(CacheTTL), lastCleanUpTime: 0} |
| 181 | +} |
| 182 | + |
| 183 | +func isValueValid(v float64) bool { |
| 184 | + //treat NaN and +/-Inf values as invalid as emf log doesn't support them |
| 185 | + return !value.IsStaleNaN(v) && !math.IsNaN(v) && !math.IsInf(v, 0) |
| 186 | +} |
0 commit comments