Skip to content

Commit

Permalink
[exporter/awsemf] Drop metrics with Inf values (#29714)
Browse files Browse the repository at this point in the history
Description: Metrics with Inf 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 Inf.

Link to tracking Issue: Fixes
#29336

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

---------

Co-authored-by: bryan-aguilar <[email protected]>
  • Loading branch information
jayasai470 and bryan-aguilar authored Dec 21, 2023
1 parent a3011b5 commit 43932b2
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 32 deletions.
27 changes: 27 additions & 0 deletions .chloggen/awsemf_dropinf.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 drop metrics that contain Inf 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: [29336]

# (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]
2 changes: 1 addition & 1 deletion exporter/awsemfexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ and then sends them directly to CloudWatch Logs using the
## 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. NaN values are not supported by CloudWatch EMF and will be dropped by the exporter.
CloudWatch console. NaN, Inf values are not supported by CloudWatch EMF and will be dropped by the exporter.

## Exporter Configuration

Expand Down
30 changes: 18 additions & 12 deletions exporter/awsemfexporter/datapoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +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
// IsStaleNaNInf returns true if metric value has NoRecordedValue flag set or if any metric value contains a NaN or Inf.
// When return value is true, IsStaleNaNInf also returns the attributes attached to the metric which can be used for
// logging purposes.
IsStaleOrNaN(i int) (bool, pcommon.Map)
IsStaleNaNInf(i int) (bool, pcommon.Map)
}

// deltaMetricMetadata contains the metadata required to perform rate/delta calculation
Expand Down Expand Up @@ -149,13 +149,13 @@ 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) {
func (dps numberDataPointSlice) IsStaleNaNInf(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 math.IsNaN(metric.DoubleValue()) || math.IsInf(metric.DoubleValue(), 0), metric.Attributes()
}
return false, pcommon.Map{}
}
Expand All @@ -179,12 +179,14 @@ func (dps histogramDataPointSlice) CalculateDeltaDatapoints(i int, instrumentati
}}, true
}

func (dps histogramDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
func (dps histogramDataPointSlice) IsStaleNaNInf(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()) {
if math.IsNaN(metric.Max()) || math.IsNaN(metric.Sum()) ||
math.IsNaN(metric.Min()) || math.IsInf(metric.Max(), 0) ||
math.IsInf(metric.Sum(), 0) || math.IsInf(metric.Min(), 0) {
return true, metric.Attributes()
}
return false, pcommon.Map{}
Expand Down Expand Up @@ -272,14 +274,17 @@ func (dps exponentialHistogramDataPointSlice) CalculateDeltaDatapoints(idx int,
}}, true
}

func (dps exponentialHistogramDataPointSlice) IsStaleOrNaN(i int) (bool, pcommon.Map) {
func (dps exponentialHistogramDataPointSlice) IsStaleNaNInf(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()) {
math.IsNaN(metric.Sum()) ||
math.IsInf(metric.Max(), 0) ||
math.IsInf(metric.Min(), 0) ||
math.IsInf(metric.Sum(), 0) {
return true, metric.Attributes()
}

Expand Down Expand Up @@ -343,19 +348,20 @@ func (dps summaryDataPointSlice) CalculateDeltaDatapoints(i int, instrumentation
return datapoints, retained
}

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

values := metric.QuantileValues()
for i := 0; i < values.Len(); i++ {
quantile := values.At(i)
if math.IsNaN(quantile.Value()) || math.IsNaN(quantile.Quantile()) {
if math.IsNaN(quantile.Value()) || math.IsNaN(quantile.Quantile()) ||
math.IsInf(quantile.Value(), 0) || math.IsInf(quantile.Quantile(), 0) {
return true, metric.Attributes()
}
}
Expand Down
Loading

0 comments on commit 43932b2

Please sign in to comment.