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

Commit

Permalink
Ignore staleness markers (#300)
Browse files Browse the repository at this point in the history
* ignore staleness markers

* handle stale NaN if they are integers

* handle stale sum+count NaNs in histograms
  • Loading branch information
dashpole committed Apr 19, 2022
1 parent 074d8ef commit e781cdd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.6
github.com/jstemmer/go-junit-report v0.9.1
github.com/prometheus/prometheus v2.5.0+incompatible
go.opencensus.io v0.23.0
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/prometheus v2.5.0+incompatible h1:7QPitgO2kOFG8ecuRn9O/4L9+10He72rVRJvMXrE9Hg=
github.com/prometheus/prometheus v2.5.0+incompatible/go.mod h1:oAIUtOny2rjMX0OWN5vPR5/q/twIROJvdqnQKDdil/s=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down
16 changes: 16 additions & 0 deletions metrics_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"errors"
"fmt"
"math"
"path"
"strings"

Expand All @@ -32,6 +33,7 @@ import (
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
timestamppb "github.com/golang/protobuf/ptypes/timestamp"
promvalue "github.com/prometheus/prometheus/pkg/value"
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
labelpb "google.golang.org/genproto/googleapis/api/label"
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
Expand Down Expand Up @@ -481,13 +483,20 @@ func protoToMetricPoint(value interface{}) (*monitoringpb.TypedValue, error) {
return nil, fmt.Errorf("protoToMetricPoint: unknown Data type: %T", value)

case *metricspb.Point_Int64Value:
// drop handle stale NaNs that were cast to integers
if isStaleInt64(v.Int64Value) {
return nil, nil
}
return &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_Int64Value{
Int64Value: v.Int64Value,
},
}, nil

case *metricspb.Point_DoubleValue:
if promvalue.IsStaleNaN(v.DoubleValue) {
return nil, nil
}
return &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_DoubleValue{
DoubleValue: v.DoubleValue,
Expand All @@ -498,6 +507,9 @@ func protoToMetricPoint(value interface{}) (*monitoringpb.TypedValue, error) {
dv := v.DistributionValue
var mv *monitoringpb.TypedValue_DistributionValue
if dv != nil {
if isStaleInt64(dv.Count) || promvalue.IsStaleNaN(dv.Sum) {
return nil, nil
}
var mean float64
if dv.Count > 0 {
mean = float64(dv.Sum) / float64(dv.Count)
Expand Down Expand Up @@ -534,6 +546,10 @@ func protoToMetricPoint(value interface{}) (*monitoringpb.TypedValue, error) {
}
}

func isStaleInt64(v int64) bool {
return v == int64(math.Float64frombits(promvalue.StaleNaN))
}

func bucketCounts(buckets []*metricspb.DistributionValue_Bucket) []int64 {
bucketCounts := make([]int64, len(buckets))
for i, bucket := range buckets {
Expand Down

0 comments on commit e781cdd

Please sign in to comment.