@@ -32,8 +32,10 @@ func calculateSummaryDelta(prev *aws.MetricValue, val interface{}, timestampMs t
32
32
countDelta := metricEntry .count
33
33
if prev != nil {
34
34
prevSummaryEntry := prev .RawValue .(summaryMetricEntry )
35
- summaryDelta = summaryDelta - prevSummaryEntry .sum
36
- countDelta = countDelta - prevSummaryEntry .count
35
+ summaryDelta = metricEntry .sum - prevSummaryEntry .sum
36
+ countDelta = metricEntry .count - prevSummaryEntry .count
37
+ } else {
38
+ return summaryMetricEntry {summaryDelta , countDelta }, false
37
39
}
38
40
return summaryMetricEntry {summaryDelta , countDelta }, true
39
41
}
@@ -53,8 +55,11 @@ type DataPoint struct {
53
55
// - pdata.SummaryDataPointSlice
54
56
type DataPoints interface {
55
57
Len () int
56
- // NOTE: At() is an expensive call as it calculates the metric's value
57
- At (i int ) DataPoint
58
+ // At gets the adjusted datapoint from the DataPointSlice at i-th index.
59
+ // dataPoint: the adjusted data point
60
+ // retained: indicates whether the data point is valid for further process
61
+ // NOTE: It is an expensive call as it calculates the metric value.
62
+ At (i int ) (dataPoint DataPoint , retained bool )
58
63
}
59
64
60
65
// deltaMetricMetadata contains the metadata required to perform rate/delta calculation
@@ -112,49 +117,67 @@ type summaryMetricEntry struct {
112
117
}
113
118
114
119
// At retrieves the IntDataPoint at the given index and performs rate/delta calculation if necessary.
115
- func (dps IntDataPointSlice ) At (i int ) DataPoint {
120
+ func (dps IntDataPointSlice ) At (i int ) ( DataPoint , bool ) {
116
121
metric := dps .IntDataPointSlice .At (i )
117
122
timestampMs := unixNanoToMilliseconds (metric .Timestamp ())
118
123
labels := createLabels (metric .LabelsMap (), dps .instrumentationLibraryName )
119
124
120
125
var metricVal float64
121
126
metricVal = float64 (metric .Value ())
127
+ retained := true
122
128
if dps .adjustToDelta {
123
- deltaVal , _ := deltaMetricCalculator .Calculate (dps .metricName , mergeLabels (dps .deltaMetricMetadata , labels ),
129
+ var deltaVal interface {}
130
+ deltaVal , retained = deltaMetricCalculator .Calculate (dps .metricName , mergeLabels (dps .deltaMetricMetadata , labels ),
124
131
metricVal , metric .Timestamp ().AsTime ())
125
- metricVal = deltaVal .(float64 )
132
+ if ! retained {
133
+ return DataPoint {}, retained
134
+ }
135
+ // It should not happen in practice that the previous metric value is smaller than the current one.
136
+ // If it happens, we assume that the metric is reset for some reason.
137
+ if deltaVal .(float64 ) >= 0 {
138
+ metricVal = deltaVal .(float64 )
139
+ }
126
140
}
127
141
128
142
return DataPoint {
129
143
Value : metricVal ,
130
144
Labels : labels ,
131
145
TimestampMs : timestampMs ,
132
- }
146
+ }, retained
133
147
}
134
148
135
149
// At retrieves the DoubleDataPoint at the given index and performs rate/delta calculation if necessary.
136
- func (dps DoubleDataPointSlice ) At (i int ) DataPoint {
150
+ func (dps DoubleDataPointSlice ) At (i int ) ( DataPoint , bool ) {
137
151
metric := dps .DoubleDataPointSlice .At (i )
138
152
labels := createLabels (metric .LabelsMap (), dps .instrumentationLibraryName )
139
153
timestampMs := unixNanoToMilliseconds (metric .Timestamp ())
140
154
141
155
var metricVal float64
142
156
metricVal = metric .Value ()
157
+ retained := true
143
158
if dps .adjustToDelta {
144
- deltaVal , _ := deltaMetricCalculator .Calculate (dps .metricName , mergeLabels (dps .deltaMetricMetadata , labels ),
159
+ var deltaVal interface {}
160
+ deltaVal , retained = deltaMetricCalculator .Calculate (dps .metricName , mergeLabels (dps .deltaMetricMetadata , labels ),
145
161
metricVal , metric .Timestamp ().AsTime ())
146
- metricVal = deltaVal .(float64 )
162
+ if ! retained {
163
+ return DataPoint {}, retained
164
+ }
165
+ // It should not happen in practice that the previous metric value is smaller than the current one.
166
+ // If it happens, we assume that the metric is reset for some reason.
167
+ if deltaVal .(float64 ) >= 0 {
168
+ metricVal = deltaVal .(float64 )
169
+ }
147
170
}
148
171
149
172
return DataPoint {
150
173
Value : metricVal ,
151
174
Labels : labels ,
152
175
TimestampMs : timestampMs ,
153
- }
176
+ }, retained
154
177
}
155
178
156
179
// At retrieves the HistogramDataPoint at the given index.
157
- func (dps HistogramDataPointSlice ) At (i int ) DataPoint {
180
+ func (dps HistogramDataPointSlice ) At (i int ) ( DataPoint , bool ) {
158
181
metric := dps .HistogramDataPointSlice .At (i )
159
182
labels := createLabels (metric .LabelsMap (), dps .instrumentationLibraryName )
160
183
timestamp := unixNanoToMilliseconds (metric .Timestamp ())
@@ -166,20 +189,25 @@ func (dps HistogramDataPointSlice) At(i int) DataPoint {
166
189
},
167
190
Labels : labels ,
168
191
TimestampMs : timestamp ,
169
- }
192
+ }, true
170
193
}
171
194
172
195
// At retrieves the SummaryDataPoint at the given index.
173
- func (dps SummaryDataPointSlice ) At (i int ) DataPoint {
196
+ func (dps SummaryDataPointSlice ) At (i int ) ( DataPoint , bool ) {
174
197
metric := dps .SummaryDataPointSlice .At (i )
175
198
labels := createLabels (metric .LabelsMap (), dps .instrumentationLibraryName )
176
199
timestampMs := unixNanoToMilliseconds (metric .Timestamp ())
177
200
178
201
sum := metric .Sum ()
179
202
count := metric .Count ()
203
+ retained := true
180
204
if dps .adjustToDelta {
181
- delta , _ := summaryMetricCalculator .Calculate (dps .metricName , mergeLabels (dps .deltaMetricMetadata , labels ),
205
+ var delta interface {}
206
+ delta , retained = summaryMetricCalculator .Calculate (dps .metricName , mergeLabels (dps .deltaMetricMetadata , labels ),
182
207
summaryMetricEntry {metric .Sum (), metric .Count ()}, metric .Timestamp ().AsTime ())
208
+ if ! retained {
209
+ return DataPoint {}, retained
210
+ }
183
211
summaryMetricDelta := delta .(summaryMetricEntry )
184
212
sum = summaryMetricDelta .sum
185
213
count = summaryMetricDelta .count
@@ -198,7 +226,7 @@ func (dps SummaryDataPointSlice) At(i int) DataPoint {
198
226
Value : metricVal ,
199
227
Labels : labels ,
200
228
TimestampMs : timestampMs ,
201
- }
229
+ }, retained
202
230
}
203
231
204
232
// createLabels converts OTel StringMap labels to a map
0 commit comments