Skip to content

Commit 6d00884

Browse files
[exporter/awsemf] Drop metrics with NaN values (#26344)
**Description:** Metrics with NaN values for float types would cause the EMF Exporter to error out during JSON Marshaling. This PR introduces a change to drop metric values that contain NaN. **Link to tracking Issue:** Fixes #26267 **Testing:** Added unit tests at several different points with varying levels of specificity. Unit tests are quite verbose in this example but I have followed the format of existing tests while doing very little refactoring. **Documentation:** Update README
1 parent f1fd087 commit 6d00884

File tree

7 files changed

+548
-15
lines changed

7 files changed

+548
-15
lines changed

.chloggen/awsemf_dropnan.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: awsemfexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: AWS EMF Exporter will not drop any metrics that contain NaN values to avoid JSON marshal errors.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [26267]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

exporter/awsemfexporter/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ and then sends them directly to CloudWatch Logs using the
2020
[PutLogEvents](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html) API.
2121

2222
## Data Conversion
23-
Convert OpenTelemetry ```Int64DataPoints```, ```DoubleDataPoints```, ```SummaryDataPoints``` metrics datapoints into CloudWatch ```EMF``` structured log formats and send it to CloudWatch. Logs and Metrics will be displayed in CloudWatch console.
23+
Convert OpenTelemetry ```Int64DataPoints```, ```DoubleDataPoints```, ```SummaryDataPoints``` metrics datapoints into
24+
CloudWatch ```EMF``` structured log formats and send it to CloudWatch. Logs and Metrics will be displayed in
25+
CloudWatch console. NaN values are not supported by CloudWatch EMF and will be dropped by the exporter.
2426

2527
## Exporter Configuration
2628

exporter/awsemfexporter/datapoint.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ type dataPoints interface {
6161
// retained: indicates whether the data point is valid for further process
6262
// NOTE: It is an expensive call as it calculates the metric value.
6363
CalculateDeltaDatapoints(i int, instrumentationScopeName string, detailedMetrics bool, calculators *emfCalculators) (dataPoint []dataPoint, retained bool)
64+
// IsStaleOrNaN returns true if metric value has NoRecordedValue flag set or if any metric value contains a NaN.
65+
// When return value is true, IsStaleOrNaN also returns the attributes attached to the metric which can be used for
66+
// logging purposes.
67+
IsStaleOrNaN(i int) (bool, pcommon.Map)
6468
}
6569

6670
// deltaMetricMetadata contains the metadata required to perform rate/delta calculation
@@ -145,6 +149,17 @@ func (dps numberDataPointSlice) CalculateDeltaDatapoints(i int, instrumentationS
145149
return []dataPoint{{name: dps.metricName, value: metricVal, labels: labels, timestampMs: timestampMs}}, retained
146150
}
147151

152+
func (dps numberDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
153+
metric := dps.NumberDataPointSlice.At(i)
154+
if metric.Flags().NoRecordedValue() {
155+
return true, metric.Attributes()
156+
}
157+
if metric.ValueType() == pmetric.NumberDataPointValueTypeDouble {
158+
return math.IsNaN(metric.DoubleValue()), metric.Attributes()
159+
}
160+
return false, pcommon.Map{}
161+
}
162+
148163
// CalculateDeltaDatapoints retrieves the HistogramDataPoint at the given index.
149164
func (dps histogramDataPointSlice) CalculateDeltaDatapoints(i int, instrumentationScopeName string, _ bool, _ *emfCalculators) ([]dataPoint, bool) {
150165
metric := dps.HistogramDataPointSlice.At(i)
@@ -164,6 +179,17 @@ func (dps histogramDataPointSlice) CalculateDeltaDatapoints(i int, instrumentati
164179
}}, true
165180
}
166181

182+
func (dps histogramDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
183+
metric := dps.HistogramDataPointSlice.At(i)
184+
if metric.Flags().NoRecordedValue() {
185+
return true, metric.Attributes()
186+
}
187+
if math.IsNaN(metric.Max()) || math.IsNaN(metric.Sum()) || math.IsNaN(metric.Min()) {
188+
return true, metric.Attributes()
189+
}
190+
return false, pcommon.Map{}
191+
}
192+
167193
// CalculateDeltaDatapoints retrieves the ExponentialHistogramDataPoint at the given index.
168194
func (dps exponentialHistogramDataPointSlice) CalculateDeltaDatapoints(idx int, instrumentationScopeName string, _ bool, _ *emfCalculators) ([]dataPoint, bool) {
169195
metric := dps.ExponentialHistogramDataPointSlice.At(idx)
@@ -246,6 +272,20 @@ func (dps exponentialHistogramDataPointSlice) CalculateDeltaDatapoints(idx int,
246272
}}, true
247273
}
248274

275+
func (dps exponentialHistogramDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
276+
metric := dps.ExponentialHistogramDataPointSlice.At(i)
277+
if metric.Flags().NoRecordedValue() {
278+
return true, metric.Attributes()
279+
}
280+
if math.IsNaN(metric.Max()) ||
281+
math.IsNaN(metric.Min()) ||
282+
math.IsNaN(metric.Sum()) {
283+
return true, metric.Attributes()
284+
}
285+
286+
return false, pcommon.Map{}
287+
}
288+
249289
// CalculateDeltaDatapoints retrieves the SummaryDataPoint at the given index and perform calculation with sum and count while retain the quantile value.
250290
func (dps summaryDataPointSlice) CalculateDeltaDatapoints(i int, instrumentationScopeName string, detailedMetrics bool, calculators *emfCalculators) ([]dataPoint, bool) {
251291
metric := dps.SummaryDataPointSlice.At(i)
@@ -303,6 +343,17 @@ func (dps summaryDataPointSlice) CalculateDeltaDatapoints(i int, instrumentation
303343
return datapoints, retained
304344
}
305345

346+
func (dps summaryDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
347+
metric := dps.SummaryDataPointSlice.At(i)
348+
if metric.Flags().NoRecordedValue() {
349+
return true, metric.Attributes()
350+
}
351+
if math.IsNaN(metric.Sum()) {
352+
return true, metric.Attributes()
353+
}
354+
return false, metric.Attributes()
355+
}
356+
306357
// createLabels converts OTel AttributesMap attributes to a map
307358
// and optionally adds in the OTel instrumentation library name
308359
func createLabels(attributes pcommon.Map, instrLibName string) map[string]string {

0 commit comments

Comments
 (0)