forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GoogleCloudExporter: Fix empty metrics StartTimestamp in datapoints
- Loading branch information
Tomasz Modelski
authored and
Tomasz Modelski
committed
Apr 30, 2021
1 parent
9784576
commit d6795c9
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package googlecloudexporter | ||
|
||
import "go.opentelemetry.io/collector/consumer/pdata" | ||
|
||
/* Fixes empty StartTimestamp field in input metrics datapoints by updating StartTimestamp = Timestamp - 1 micro sec | ||
Some metrics from Hostmetric receiver has Datapoints with empty StartTimestamp = 0 | ||
This causes that later on StartTimestamp is assigned as Timestamp and such datapoints are rejected by google monitoring api */ | ||
func fixEmptyStartTime(metrics pdata.Metrics) { | ||
resourceMetrics := metrics.ResourceMetrics() | ||
for i := 0; i < resourceMetrics.Len(); i++ { | ||
rm := resourceMetrics.At(i) | ||
ilms := rm.InstrumentationLibraryMetrics() | ||
|
||
for j := 0; j < ilms.Len(); j++ { | ||
ilm := ilms.At(j) | ||
metrics := ilm.Metrics() | ||
|
||
for k := 0; k < metrics.Len(); k++ { | ||
metric := metrics.At(k) | ||
|
||
switch metric.DataType() { | ||
case pdata.MetricDataTypeIntSum: | ||
fixEmptyStartTimestampInIntData(metric.IntSum().DataPoints()) | ||
break | ||
case pdata.MetricDataTypeDoubleSum: | ||
fixEmptyStartTimestampInDoubleData(metric.DoubleSum().DataPoints()) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
const microSecAsNano = 1000 | ||
|
||
func fixEmptyStartTimestampInIntData(dps pdata.IntDataPointSlice) { | ||
for i := 0; i < dps.Len(); i++ { | ||
dp := dps.At(i) | ||
if dp.StartTimestamp() == 0 && dp.Timestamp() > microSecAsNano { | ||
dp.SetStartTimestamp(dp.Timestamp() - microSecAsNano) | ||
} | ||
} | ||
} | ||
|
||
func fixEmptyStartTimestampInDoubleData(dps pdata.DoubleDataPointSlice) { | ||
for i := 0; i < dps.Len(); i++ { | ||
dp := dps.At(i) | ||
if dp.StartTimestamp() == 0 && dp.Timestamp() > microSecAsNano { | ||
dp.SetStartTimestamp(dp.Timestamp() - microSecAsNano) | ||
} | ||
} | ||
} |