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

Commit

Permalink
Reproduce bug in stackdriver exporter. (#199)
Browse files Browse the repository at this point in the history
* Reproduce bug in stackdriver exporter.

* Update test.

* Fix more tests, remove unnecessary time helper
  • Loading branch information
bogdandrutu authored and songy23 committed Sep 5, 2019
1 parent b5c7a2c commit 2fdcf7b
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 23 deletions.
3 changes: 0 additions & 3 deletions equivalence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ import (
)

func TestStatsAndMetricsEquivalence(t *testing.T) {
_, _, stop := createFakeServer(t)
defer stop()

startTime := time.Unix(1000, 0)
startTimePb := &timestamp.Timestamp{Seconds: 1000}
md := metricdata.Descriptor{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/aws/aws-sdk-go v1.22.1
github.com/census-instrumentation/opencensus-proto v0.2.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.0
github.com/golang/protobuf v1.3.2
github.com/google/addlicense v0.0.0-20190510175307-22550fa7c1b0 // indirect
github.com/google/go-cmp v0.3.1
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gogo/protobuf v1.3.0 h1:G8O7TerXerS4F6sx9OV7/nRfJdnXgHZu/S/7F2SN+UE=
github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand All @@ -42,6 +44,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/protobuf v3.9.1+incompatible h1:8oCWqzcHmpI7oK4A2LT3qpYu6SEFRFDECpCtpCWumDU=
github.com/googleapis/gax-go/v2 v2.0.4 h1:hU4mGcQI4DaAYW+IbTun+2qEZVFxK0ySjQLTbS0VQKc=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
Expand All @@ -56,6 +59,8 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5i
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024 h1:rBMNdlhTLzJjJSDIjNEXX1Pz3Hmwmz91v+zycvx9PJc=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
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/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2 h1:1jfy6i1g66ijpffgfaF/7pIFYZnSZzvo9P9DFkFmRIM=
Expand Down Expand Up @@ -126,6 +131,7 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
Expand Down
168 changes: 168 additions & 0 deletions metrics_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,186 @@ import (

resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
"github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/api/option"
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
labelpb "google.golang.org/genproto/googleapis/api/label"
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
"google.golang.org/grpc"

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/google/go-cmp/cmp"
"go.opencensus.io/resource/resourcekeys"
)

func TestExportTimeSeriesWithDifferentLabels(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
defer doneFn()

// Now create a gRPC connection to the agent.
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
t.Fatalf("Failed to make a gRPC connection to the agent: %v", err)
}
defer conn.Close()

// Finally create the OpenCensus stats exporter
exporterOptions := Options{
ProjectID: "equivalence",
MonitoringClientOptions: []option.ClientOption{option.WithGRPCConn(conn)},

// Set empty labels to avoid the opencensus-task
DefaultMonitoringLabels: &Labels{},
}
se, err := NewExporter(exporterOptions)
if err != nil {
t.Fatalf("Failed to create the statsExporter: %v", err)
}

startTimestamp := &timestamp.Timestamp{
Seconds: 1543160298,
Nanos: 100000090,
}
endTimestamp := &timestamp.Timestamp{
Seconds: 1543160298,
Nanos: 100000997,
}

// Generate the proto Metrics.
var metricPbs []*metricspb.Metric
metricPbs = append(metricPbs,
&metricspb.Metric{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: "ocagent.io/calls",
Description: "The number of the various calls",
LabelKeys: []*metricspb.LabelKey{
{
Key: "empty_key",
},
{
Key: "operation_type",
},
},
Unit: "1",
Type: metricspb.MetricDescriptor_CUMULATIVE_INT64,
},
Timeseries: []*metricspb.TimeSeries{
{
StartTimestamp: startTimestamp,
LabelValues: []*metricspb.LabelValue{
{
Value: "",
HasValue: true,
},
{
Value: "test_1",
HasValue: true,
},
},
Points: []*metricspb.Point{
{
Timestamp: endTimestamp,
Value: &metricspb.Point_Int64Value{Int64Value: int64(1)},
},
},
},
{
StartTimestamp: startTimestamp,
LabelValues: []*metricspb.LabelValue{
{
Value: "",
HasValue: true,
},
{
Value: "test_2",
HasValue: true,
},
},
Points: []*metricspb.Point{
{
Timestamp: endTimestamp,
Value: &metricspb.Point_Int64Value{Int64Value: int64(1)},
},
},
},
},
})

var wantTimeSeries []*monitoringpb.CreateTimeSeriesRequest
wantTimeSeries = append(wantTimeSeries, &monitoringpb.CreateTimeSeriesRequest{
Name: "projects/equivalence",
TimeSeries: []*monitoringpb.TimeSeries{
{
Metric: &googlemetricpb.Metric{
Type: "custom.googleapis.com/opencensus/ocagent.io/calls",
Labels: map[string]string{
"empty_key": "",
"operation_type": "test_1",
},
},
Resource: &monitoredrespb.MonitoredResource{
Type: "global",
},
MetricKind: googlemetricpb.MetricDescriptor_CUMULATIVE,
ValueType: googlemetricpb.MetricDescriptor_INT64,
Points: []*monitoringpb.Point{
{
Interval: &monitoringpb.TimeInterval{
StartTime: startTimestamp,
EndTime: endTimestamp,
},
Value: &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_Int64Value{
Int64Value: 1,
},
},
},
},
},
{
Metric: &googlemetricpb.Metric{
Type: "custom.googleapis.com/opencensus/ocagent.io/calls",
Labels: map[string]string{
"empty_key": "",
"operation_type": "test_2",
},
},
Resource: &monitoredrespb.MonitoredResource{
Type: "global",
},
MetricKind: googlemetricpb.MetricDescriptor_CUMULATIVE,
ValueType: googlemetricpb.MetricDescriptor_INT64,
Points: []*monitoringpb.Point{
{
Interval: &monitoringpb.TimeInterval{
StartTime: startTimestamp,
EndTime: endTimestamp,
},
Value: &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_Int64Value{
Int64Value: 1,
},
},
},
},
},
}, //<metric:<type:"custom.googleapis.com/opencensus/ocagent.io/calls" labels:<key:"opencensus_task" value:"[email protected]" > > resource:<type:"global" > metric_kind:CUMULATIVE value_type:INT64 points:<interval:<end_time:<seconds:1001 > start_time:<seconds:1000 > > value:<int64_value:8 > > > time_series:<metric:<type:"custom.googleapis.com/opencensus/ocagent.io/calls" labels:<key:"opencensus_task" value:"[email protected]" > > resource:<type:"global" > metric_kind:CUMULATIVE value_type:INT64 points:<interval:<end_time:<seconds:1001 > start_time:<seconds:1000 > > value:<int64_value:8 > > > `,
})

// Export the proto Metrics to the Stackdriver backend.
se.PushMetricsProto(context.Background(), nil, nil, metricPbs)
se.Flush()

var gotTimeSeries []*monitoringpb.CreateTimeSeriesRequest
server.forEachStackdriverTimeSeries(func(sdt *monitoringpb.CreateTimeSeriesRequest) {
gotTimeSeries = append(gotTimeSeries, sdt)
})

requireTimeSeriesRequestEqual(t, gotTimeSeries, wantTimeSeries)
}

func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
startTimestamp := &timestamp.Timestamp{
Seconds: 1543160298,
Expand Down
22 changes: 12 additions & 10 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/golang/protobuf/ptypes/timestamp"

Expand Down Expand Up @@ -90,11 +90,12 @@ func TestMetricToCreateTimeSeriesRequest(t *testing.T) {
Seconds: 1543160298,
Nanos: 100000090,
}
startTime := time.Unix(1543160298, 100000090)
startTime, _ := ptypes.Timestamp(startTimestamp)
endTimestamp := &timestamp.Timestamp{
Seconds: 1543160298,
Nanos: 100000997,
}
endTime, _ := ptypes.Timestamp(endTimestamp)

// TODO:[rghetia] add test for built-in metrics.
tests := []struct {
Expand All @@ -113,10 +114,10 @@ func TestMetricToCreateTimeSeriesRequest(t *testing.T) {
Resource: nil,
TimeSeries: []*metricdata.TimeSeries{
{
StartTime: timestampToTime(startTimestamp),
StartTime: startTime,
Points: []metricdata.Point{
{
Time: timestampToTime(endTimestamp),
Time: endTime,
Value: &metricdata.Distribution{
Count: 1,
Sum: 11.9,
Expand Down Expand Up @@ -201,10 +202,10 @@ func TestMetricToCreateTimeSeriesRequest(t *testing.T) {
Resource: nil,
TimeSeries: []*metricdata.TimeSeries{
{
StartTime: timestampToTime(startTimestamp),
StartTime: startTime,
Points: []metricdata.Point{
{
Time: timestampToTime(endTimestamp),
Time: endTime,
Value: &metricdata.Distribution{
Count: 1,
Sum: 11.9,
Expand Down Expand Up @@ -430,11 +431,12 @@ func TestMetricsToMonitoringMetrics_fromProtoPoint(t *testing.T) {
Seconds: 1543160298,
Nanos: 100000090,
}
startTime := time.Unix(1543160298, 100000090)
startTime, _ := ptypes.Timestamp(startTimestamp)
endTimestamp := &timestamp.Timestamp{
Seconds: 1543160298,
Nanos: 100000997,
}
endTime, _ := ptypes.Timestamp(endTimestamp)

traceID := trace.TraceID{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 4, 8, 16, 32, 64, 128}
spanID := trace.SpanID{1, 2, 4, 8, 16, 32, 64, 128}
Expand All @@ -452,7 +454,7 @@ func TestMetricsToMonitoringMetrics_fromProtoPoint(t *testing.T) {
}{
{
in: &metricdata.Point{
Time: timestampToTime(endTimestamp),
Time: endTime,
Value: &metricdata.Distribution{
Count: 1,
Sum: 11.9,
Expand Down Expand Up @@ -509,7 +511,7 @@ func TestMetricsToMonitoringMetrics_fromProtoPoint(t *testing.T) {
},
{
in: &metricdata.Point{
Time: timestampToTime(endTimestamp),
Time: endTime,
Value: float64(50.0),
},
want: &monitoringpb.Point{
Expand All @@ -524,7 +526,7 @@ func TestMetricsToMonitoringMetrics_fromProtoPoint(t *testing.T) {
},
{
in: &metricdata.Point{
Time: timestampToTime(endTimestamp),
Time: endTime,
Value: int64(17),
},
want: &monitoringpb.Point{
Expand Down
25 changes: 15 additions & 10 deletions metrics_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,33 @@ Common test utilities for comparing Stackdriver metrics.
*/

import (
"github.com/golang/protobuf/ptypes/timestamp"
"testing"

"github.com/golang/protobuf/proto"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"

"time"
)

func timestampToTime(ts *timestamp.Timestamp) time.Time {
if ts == nil {
return time.Unix(0, 0).UTC()
}
return time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
}

func cmpResource(got, want *monitoredrespb.MonitoredResource) string {
return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoredrespb.MonitoredResource{}))
}

func requireTimeSeriesRequestEqual(t *testing.T, got, want []*monitoringpb.CreateTimeSeriesRequest) {
if len(got) != len(want) {
t.Fatalf("Unexpected slice len got: %d want: %d", len(got), len(want))
}
for i, g := range got {
w := want[i]
if !proto.Equal(g, w) {
t.Fatalf("Unexpected proto difference got: %s want: %s", proto.MarshalTextString(g), proto.MarshalTextString(w))
}
}
}

func cmpTSReqs(got, want []*monitoringpb.CreateTimeSeriesRequest) string {
return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoringpb.CreateTimeSeriesRequest{}), cmpopts.IgnoreTypes(googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED, googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED))
}
Expand Down

0 comments on commit 2fdcf7b

Please sign in to comment.