Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 68f8441

Browse files
committed
skip exporting points with no value due to prometheus staleness markers
1 parent 5a007e3 commit 68f8441

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

metrics_proto.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ func (se *statsExporter) protoMetricToTimeSeries(ctx context.Context, mappedRsc
257257
mb.recordDroppedTimeseries(1, err)
258258
continue
259259
}
260+
if len(sdPoints) == 0 {
261+
// Sending TimeSeries with no points is not allowed, so skip this one
262+
continue
263+
}
260264

261265
// Each TimeSeries has labelValues which MUST be correlated
262266
// with that from the MetricDescriptor
@@ -355,6 +359,11 @@ func (se *statsExporter) protoTimeSeriesToMonitoringPoints(ts *metricspb.TimeSer
355359
if err != nil {
356360
return nil, err
357361
}
362+
if spt == nil {
363+
// Skip any points that don't result in a correct value
364+
// For example if they are Prometheus staleness markers
365+
continue
366+
}
358367
sptl = append(sptl, spt)
359368
}
360369
return sptl, nil
@@ -444,6 +453,11 @@ func fromProtoPoint(startTime *timestamppb.Timestamp, pt *metricspb.Point) (*mon
444453
if err != nil {
445454
return nil, err
446455
}
456+
// We don't want to send points with no value as those are invalid
457+
// Pass the nil upwards instead to skip this point
458+
if mptv == nil {
459+
return nil, nil
460+
}
447461

448462
endTime := pt.Timestamp
449463
interval := &monitoringpb.TimeInterval{

metrics_proto_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ package stackdriver
1717
import (
1818
"context"
1919
"fmt"
20+
"math"
2021
"strings"
2122
"testing"
2223

2324
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
2425
"github.com/golang/protobuf/ptypes/timestamp"
26+
promvalue "github.com/prometheus/prometheus/model/value"
2527
"google.golang.org/api/option"
2628
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
2729
labelpb "google.golang.org/genproto/googleapis/api/label"
@@ -367,6 +369,35 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
367369
},
368370
},
369371
},
372+
{
373+
name: "Test staleness marker is skipped",
374+
in: &metricspb.Metric{
375+
MetricDescriptor: &metricspb.MetricDescriptor{
376+
Name: "with_metric_descriptor_2",
377+
Description: "This is a test",
378+
Unit: "By",
379+
LabelKeys: []*metricspb.LabelKey{{Key: "key1"}, {Key: "key2"}, {Key: "key3"}},
380+
},
381+
Timeseries: []*metricspb.TimeSeries{
382+
{
383+
StartTimestamp: startTimestamp,
384+
LabelValues: []*metricspb.LabelValue{{}, {}, {HasValue: true, Value: "val3"}},
385+
Points: []*metricspb.Point{
386+
{
387+
Timestamp: endTimestamp,
388+
Value: &metricspb.Point_DoubleValue{
389+
DoubleValue: math.Float64frombits(promvalue.StaleNaN),
390+
},
391+
},
392+
},
393+
},
394+
},
395+
},
396+
statsExporter: &statsExporter{
397+
o: Options{ProjectID: "foo", MapResource: DefaultMapResource},
398+
},
399+
want: nil,
400+
},
370401
}
371402

372403
seenResources := make(map[*resourcepb.Resource]*monitoredrespb.MonitoredResource)
@@ -762,6 +793,20 @@ func TestProtoMetricsToMonitoringMetrics_fromProtoPoint(t *testing.T) {
762793
},
763794
},
764795
},
796+
{
797+
in: &metricspb.Point{
798+
Timestamp: endTimestamp,
799+
Value: &metricspb.Point_Int64Value{Int64Value: int64(math.Float64frombits(promvalue.StaleNaN))},
800+
},
801+
want: nil,
802+
},
803+
{
804+
in: &metricspb.Point{
805+
Timestamp: endTimestamp,
806+
Value: &metricspb.Point_DoubleValue{DoubleValue: math.Float64frombits(promvalue.StaleNaN)},
807+
},
808+
want: nil,
809+
},
765810
}
766811

767812
for i, tt := range tests {

0 commit comments

Comments
 (0)