Skip to content

Commit 81f15f0

Browse files
committed
correctly handle nil value and timestamp combinations
Signed-off-by: Lukas Wöhrl <[email protected]>
1 parent 1d3536c commit 81f15f0

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

pkg/promutil/migrate.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,14 @@ func BuildMetrics(results []model.CloudwatchMetricResult, labelsSnakeCase bool,
112112
if dataPoint == nil && metric.MetricMigrationParams.AddCloudwatchTimestamp {
113113
// If we did not get a datapoint then the timestamp is a default value making it unusable in the
114114
// exported metric. Attempting to put a fake timestamp on the metric will likely conflict with
115-
// future CloudWatch timestamps which are always in the past. It's safer to skip here than guess
116-
continue
115+
// future CloudWatch timestamps which are always in the past. It's safer to not export any data.
116+
if metric.MetricMigrationParams.ExportAllDataPoints {
117+
// If we're exporting all data points, we can skip this one and check for a historical datapoint
118+
continue
119+
} else {
120+
// If we are not exporting all data points, we better have nothing exported
121+
break
122+
}
117123
}
118124
if dataPoint == nil {
119125
exportedDatapoint = math.NaN()

pkg/promutil/migrate_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,31 @@ func TestBuildMetrics(t *testing.T) {
409409
ResourceName: "arn:aws:elasticache:us-east-1:123456789012:cluster:redis-cluster",
410410
},
411411
{
412-
//Check that if there is no data point for the latest timestamp it exports the previous one
413412
MetricName: "NetworkPacketsOut",
413+
MetricMigrationParams: model.MetricMigrationParams{
414+
NilToZero: true,
415+
AddCloudwatchTimestamp: true,
416+
ExportAllDataPoints: true,
417+
},
418+
Namespace: "AWS/ElastiCache",
419+
Dimensions: []model.Dimension{
420+
{
421+
Name: "CacheClusterId",
422+
Value: "redis-cluster",
423+
},
424+
},
425+
GetMetricDataResult: &model.GetMetricDataResult{
426+
Statistic: "Average",
427+
Datapoints: []model.DatapointWithTimestamp{
428+
model.NewDataPoint(nil, ts),
429+
model.NewDataPoint(aws.Float64(5), ts.Add(-1*time.Minute)),
430+
model.NewDataPoint(aws.Float64(6), ts.Add(-2*time.Minute)),
431+
},
432+
},
433+
ResourceName: "arn:aws:elasticache:us-east-1:123456789012:cluster:redis-cluster",
434+
},
435+
{
436+
MetricName: "NetworkMaxBytesIn",
414437
MetricMigrationParams: model.MetricMigrationParams{
415438
NilToZero: true,
416439
AddCloudwatchTimestamp: true,
@@ -530,6 +553,18 @@ func TestBuildMetrics(t *testing.T) {
530553
"dimension_CacheClusterId": "redis-cluster",
531554
},
532555
},
556+
{
557+
Name: "aws_elasticache_network_packets_out_average",
558+
Value: 6,
559+
Timestamp: ts.Add(-2 * time.Minute),
560+
IncludeTimestamp: true,
561+
Labels: map[string]string{
562+
"account_id": "123456789012",
563+
"name": "arn:aws:elasticache:us-east-1:123456789012:cluster:redis-cluster",
564+
"region": "us-east-1",
565+
"dimension_CacheClusterId": "redis-cluster",
566+
},
567+
},
533568
},
534569
expectedLabels: map[string]model.LabelSet{
535570
"aws_elasticache_cpuutilization_average": {

0 commit comments

Comments
 (0)