Skip to content

Commit

Permalink
[exporter/awsemf] Drop metrics with NaN values (#26344)
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
bryan-aguilar authored Sep 19, 2023
1 parent f1fd087 commit 6d00884
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 15 deletions.
27 changes: 27 additions & 0 deletions .chloggen/awsemf_dropnan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: awsemfexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: AWS EMF Exporter will not drop any metrics that contain NaN values to avoid JSON marshal errors.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26267]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
4 changes: 3 additions & 1 deletion exporter/awsemfexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ and then sends them directly to CloudWatch Logs using the
[PutLogEvents](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html) API.

## Data Conversion
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.
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. NaN values are not supported by CloudWatch EMF and will be dropped by the exporter.

## Exporter Configuration

Expand Down
51 changes: 51 additions & 0 deletions exporter/awsemfexporter/datapoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type dataPoints interface {
// retained: indicates whether the data point is valid for further process
// NOTE: It is an expensive call as it calculates the metric value.
CalculateDeltaDatapoints(i int, instrumentationScopeName string, detailedMetrics bool, calculators *emfCalculators) (dataPoint []dataPoint, retained bool)
// IsStaleOrNaN returns true if metric value has NoRecordedValue flag set or if any metric value contains a NaN.
// When return value is true, IsStaleOrNaN also returns the attributes attached to the metric which can be used for
// logging purposes.
IsStaleOrNaN(i int) (bool, pcommon.Map)
}

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

func (dps numberDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
metric := dps.NumberDataPointSlice.At(i)
if metric.Flags().NoRecordedValue() {
return true, metric.Attributes()
}
if metric.ValueType() == pmetric.NumberDataPointValueTypeDouble {
return math.IsNaN(metric.DoubleValue()), metric.Attributes()
}
return false, pcommon.Map{}
}

// CalculateDeltaDatapoints retrieves the HistogramDataPoint at the given index.
func (dps histogramDataPointSlice) CalculateDeltaDatapoints(i int, instrumentationScopeName string, _ bool, _ *emfCalculators) ([]dataPoint, bool) {
metric := dps.HistogramDataPointSlice.At(i)
Expand All @@ -164,6 +179,17 @@ func (dps histogramDataPointSlice) CalculateDeltaDatapoints(i int, instrumentati
}}, true
}

func (dps histogramDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
metric := dps.HistogramDataPointSlice.At(i)
if metric.Flags().NoRecordedValue() {
return true, metric.Attributes()
}
if math.IsNaN(metric.Max()) || math.IsNaN(metric.Sum()) || math.IsNaN(metric.Min()) {
return true, metric.Attributes()
}
return false, pcommon.Map{}
}

// CalculateDeltaDatapoints retrieves the ExponentialHistogramDataPoint at the given index.
func (dps exponentialHistogramDataPointSlice) CalculateDeltaDatapoints(idx int, instrumentationScopeName string, _ bool, _ *emfCalculators) ([]dataPoint, bool) {
metric := dps.ExponentialHistogramDataPointSlice.At(idx)
Expand Down Expand Up @@ -246,6 +272,20 @@ func (dps exponentialHistogramDataPointSlice) CalculateDeltaDatapoints(idx int,
}}, true
}

func (dps exponentialHistogramDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
metric := dps.ExponentialHistogramDataPointSlice.At(i)
if metric.Flags().NoRecordedValue() {
return true, metric.Attributes()
}
if math.IsNaN(metric.Max()) ||
math.IsNaN(metric.Min()) ||
math.IsNaN(metric.Sum()) {
return true, metric.Attributes()
}

return false, pcommon.Map{}
}

// CalculateDeltaDatapoints retrieves the SummaryDataPoint at the given index and perform calculation with sum and count while retain the quantile value.
func (dps summaryDataPointSlice) CalculateDeltaDatapoints(i int, instrumentationScopeName string, detailedMetrics bool, calculators *emfCalculators) ([]dataPoint, bool) {
metric := dps.SummaryDataPointSlice.At(i)
Expand Down Expand Up @@ -303,6 +343,17 @@ func (dps summaryDataPointSlice) CalculateDeltaDatapoints(i int, instrumentation
return datapoints, retained
}

func (dps summaryDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
metric := dps.SummaryDataPointSlice.At(i)
if metric.Flags().NoRecordedValue() {
return true, metric.Attributes()
}
if math.IsNaN(metric.Sum()) {
return true, metric.Attributes()
}
return false, metric.Attributes()
}

// createLabels converts OTel AttributesMap attributes to a map
// and optionally adds in the OTel instrumentation library name
func createLabels(attributes pcommon.Map, instrLibName string) map[string]string {
Expand Down
Loading

0 comments on commit 6d00884

Please sign in to comment.