Skip to content

Commit

Permalink
GoogleCloudExporter: Fix empty metrics StartTimestamp in datapoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Modelski authored and Tomasz Modelski committed Apr 30, 2021
1 parent 9784576 commit d6795c9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions exporter/googlecloudexporter/googlecloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func newGoogleCloudMetricsExporter(cfg *Config, params component.ExporterCreateP

// pushMetrics calls StackdriverExporter.PushMetricsProto on each element of the given metrics
func (me *metricsExporter) pushMetrics(ctx context.Context, m pdata.Metrics) error {
// 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
fixEmptyStartTime(m)

// PushMetricsProto doesn't bundle subsequent calls, so we need to
// combine the data here to avoid generating too many RPC calls.
mds := exportAdditionalLabels(internaldata.MetricsToOC(m))
Expand Down
52 changes: 52 additions & 0 deletions exporter/googlecloudexporter/googlecloudfixes.go
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)
}
}
}

0 comments on commit d6795c9

Please sign in to comment.