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

Commit

Permalink
Remove the call to getMetricDescriptor and try to always create the M…
Browse files Browse the repository at this point in the history
…etricDescriptor.
  • Loading branch information
Bogdan Drutu committed Mar 25, 2018
1 parent 3c2218f commit ecf530f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
36 changes: 8 additions & 28 deletions exporter/stackdriver/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"log"
"net/url"
"os"
"path"
"strconv"
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 9 additions & 17 deletions exporter/stackdriver/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})}
Expand Down Expand Up @@ -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
}()

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}()

Expand All @@ -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)
}
Expand Down

0 comments on commit ecf530f

Please sign in to comment.