From ecf530f9d00f1065001b691e20d035112a09aef0 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 23 Mar 2018 12:07:05 -0700 Subject: [PATCH] Remove the call to getMetricDescriptor and try to always create the MetricDescriptor. --- exporter/stackdriver/stats.go | 36 +++++++----------------------- exporter/stackdriver/stats_test.go | 26 ++++++++------------- 2 files changed, 17 insertions(+), 45 deletions(-) diff --git a/exporter/stackdriver/stats.go b/exporter/stackdriver/stats.go index d37b4c523..dbd1cba0c 100644 --- a/exporter/stackdriver/stats.go +++ b/exporter/stackdriver/stats.go @@ -19,7 +19,6 @@ import ( "errors" "fmt" "log" - "net/url" "os" "path" "strconv" @@ -43,8 +42,6 @@ import ( metricpb "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" - "google.golang.org/grpc/codes" ) const maxTimeSeriesPerUpload = 200 @@ -206,7 +203,7 @@ func (e *statsExporter) makeReq(vds []*view.Data, limit int) []*monitoringpb.Cre for _, row := range vd.Rows { ts := &monitoringpb.TimeSeries{ Metric: &metricpb.Metric{ - Type: namespacedViewName(vd.View.Name, false), + Type: namespacedViewName(vd.View.Name), Labels: newLabels(row.Tags, e.taskValue), }, Resource: resource, @@ -247,21 +244,7 @@ func (e *statsExporter) createMeasure(ctx context.Context, vd *view.Data) error return equalAggTagKeys(md, agg, tagKeys) } - metricName := monitoring.MetricMetricDescriptorPath(e.o.ProjectID, namespacedViewName(viewName, true)) - md, err := getMetricDescriptor(ctx, e.c, &monitoringpb.GetMetricDescriptorRequest{ - Name: metricName, - }) - if err == nil { - if err := equalAggTagKeys(md, agg, tagKeys); err != nil { - return err - } - e.createdViews[viewName] = md - return nil - } - if grpc.Code(err) != codes.NotFound { - return err - } - + metricType := namespacedViewName(viewName) var valueType metricpb.MetricDescriptor_ValueType unit := m.Unit() @@ -287,13 +270,14 @@ func (e *statsExporter) createMeasure(ctx context.Context, vd *view.Data) error displayNamePrefix = e.o.MetricPrefix } - md, err = createMetricDescriptor(ctx, e.c, &monitoringpb.CreateMetricDescriptorRequest{ - Name: monitoring.MetricProjectPath(e.o.ProjectID), + md, err := createMetricDescriptor(ctx, e.c, &monitoringpb.CreateMetricDescriptorRequest{ + Name: fmt.Sprintf("projects/%s", e.o.ProjectID), MetricDescriptor: &metricpb.MetricDescriptor{ + Name: fmt.Sprintf("projects/%s/metricDescriptors/%s", e.o.ProjectID, metricType), DisplayName: path.Join(displayNamePrefix, viewName), Description: vd.View.Description, Unit: unit, - Type: namespacedViewName(viewName, false), + Type: metricType, MetricKind: metricKind, ValueType: valueType, Labels: newLabelDescriptors(vd.View.TagKeys), @@ -374,12 +358,8 @@ func newTypedValue(vd *view.View, r *view.Row) *monitoringpb.TypedValue { return nil } -func namespacedViewName(v string, escaped bool) string { - p := path.Join("opencensus", v) - if escaped { - p = url.PathEscape(p) - } - return path.Join("custom.googleapis.com", p) +func namespacedViewName(v string) string { + return path.Join("custom.googleapis.com", "opencensus", v) } func newLabels(tags []tag.Tag, taskValue string) map[string]string { diff --git a/exporter/stackdriver/stats_test.go b/exporter/stackdriver/stats_test.go index be8305217..169755ead 100644 --- a/exporter/stackdriver/stats_test.go +++ b/exporter/stackdriver/stats_test.go @@ -33,8 +33,6 @@ import ( monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres" monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" "google.golang.org/grpc" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) var authOptions = []option.ClientOption{option.WithGRPCConn(&grpc.ClientConn{})} @@ -562,11 +560,9 @@ func TestEqualAggWindowTagKeys(t *testing.T) { } func TestExporter_createMeasure(t *testing.T) { - oldGetMetricDescriptor := getMetricDescriptor oldCreateMetricDescriptor := createMetricDescriptor defer func() { - getMetricDescriptor = oldGetMetricDescriptor createMetricDescriptor = oldCreateMetricDescriptor }() @@ -589,15 +585,15 @@ func TestExporter_createMeasure(t *testing.T) { e := &statsExporter{ createdViews: make(map[string]*metricpb.MetricDescriptor), + o:Options{ProjectID: "test_project"}, } - var getCalls, createCalls int - getMetricDescriptor = func(ctx context.Context, c *monitoring.MetricClient, mdr *monitoringpb.GetMetricDescriptorRequest) (*metric.MetricDescriptor, error) { - getCalls++ - return nil, status.Error(codes.NotFound, "") - } + var createCalls int createMetricDescriptor = func(ctx context.Context, c *monitoring.MetricClient, mdr *monitoringpb.CreateMetricDescriptorRequest) (*metric.MetricDescriptor, error) { createCalls++ + if got, want := mdr.MetricDescriptor.Name, "projects/test_project/metricDescriptors/custom.googleapis.com/opencensus/test_view_sum"; got != want { + t.Errorf("MetricDescriptor.Name = %q; want %q", got, want) + } if got, want := mdr.MetricDescriptor.Type, "custom.googleapis.com/opencensus/test_view_sum"; got != want { t.Errorf("MetricDescriptor.Type = %q; want %q", got, want) } @@ -634,9 +630,6 @@ func TestExporter_createMeasure(t *testing.T) { if err := e.createMeasure(ctx, vd); err != nil { t.Errorf("Exporter.createMeasure() error = %v", err) } - if count := getCalls; count != 1 { - t.Errorf("getMetricDescriptor needs to be called for once; called %v times", count) - } if count := createCalls; count != 1 { t.Errorf("createMetricDescriptor needs to be called for once; called %v times", count) } @@ -646,11 +639,9 @@ func TestExporter_createMeasure(t *testing.T) { } func TestExporter_createMeasure_CountAggregation(t *testing.T) { - oldGetMetricDescriptor := getMetricDescriptor oldCreateMetricDescriptor := createMetricDescriptor defer func() { - getMetricDescriptor = oldGetMetricDescriptor createMetricDescriptor = oldCreateMetricDescriptor }() @@ -673,12 +664,13 @@ func TestExporter_createMeasure_CountAggregation(t *testing.T) { e := &statsExporter{ createdViews: make(map[string]*metricpb.MetricDescriptor), + o:Options{ProjectID: "test_project"}, } - getMetricDescriptor = func(ctx context.Context, c *monitoring.MetricClient, mdr *monitoringpb.GetMetricDescriptorRequest) (*metric.MetricDescriptor, error) { - return nil, status.Error(codes.NotFound, "") - } createMetricDescriptor = func(ctx context.Context, c *monitoring.MetricClient, mdr *monitoringpb.CreateMetricDescriptorRequest) (*metric.MetricDescriptor, error) { + if got, want := mdr.MetricDescriptor.Name, "projects/test_project/metricDescriptors/custom.googleapis.com/opencensus/test_view_count"; got != want { + t.Errorf("MetricDescriptor.Name = %q; want %q", got, want) + } if got, want := mdr.MetricDescriptor.Type, "custom.googleapis.com/opencensus/test_view_count"; got != want { t.Errorf("MetricDescriptor.Type = %q; want %q", got, want) }