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

Commit

Permalink
Add GetMetricPrefix func as an option to support dynamic custom metri…
Browse files Browse the repository at this point in the history
…c prefix (#238)

* add GetMetricPrefix

* change comment

* change display in tests

* fix typo
  • Loading branch information
yanweiguo authored and songy23 committed Nov 8, 2019
1 parent 61e3caa commit 59d068f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
7 changes: 5 additions & 2 deletions metrics_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var percentileLabelKey = &metricspb.LabelKey{
Description: "the value at a given percentile of a distribution",
}
var globalResource = &resource.Resource{Type: "global"}
var domains = []string{"googleapis.com", "kubernetes.io", "istio.io"}
var domains = []string{"googleapis.com", "kubernetes.io", "istio.io", "knative.dev"}

// PushMetricsProto exports OpenCensus Metrics Proto to Stackdriver Monitoring synchronously,
// without de-duping or adding proto metrics to the bundler.
Expand Down Expand Up @@ -410,8 +410,11 @@ func labelDescriptorsFromProto(defaults map[string]labelValue, protoLabelKeys []

func (se *statsExporter) metricTypeFromProto(name string) string {
prefix := se.o.MetricPrefix
if se.o.GetMetricPrefix != nil {
prefix = se.o.GetMetricPrefix(name)
}
if prefix != "" {
name = prefix + name
name = path.Join(prefix, name)
}
if !hasDomain(name) {
// Still needed because the name may or may not have a "/" at the beginning.
Expand Down
4 changes: 0 additions & 4 deletions metrics_proto_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func TestMetricsWithPrefix(t *testing.T) {
metricName := "ocagent.io"
saveMetricType := tc.outTSR[0].TimeSeries[0].Metric.Type
saveMDRName := tc.outMDR[0].MetricDescriptor.Name
saveMDRDispName := tc.outMDR[0].MetricDescriptor.DisplayName

for _, prefix := range prefixes {
opts := defaultOpts
Expand All @@ -104,7 +103,6 @@ func TestMetricsWithPrefix(t *testing.T) {
mt := strings.Replace(saveMetricType, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Name = strings.Replace(saveMDRName, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Type = mt
tc.outMDR[0].MetricDescriptor.DisplayName = strings.Replace(saveMDRDispName, "OpenCensus/"+metricName, (prefix + metricName), -1)

tc.outTSR[0].TimeSeries[0].Metric.Type = mt

Expand All @@ -125,7 +123,6 @@ func TestMetricsWithPrefixWithDomain(t *testing.T) {
metricName := "ocagent.io"
saveMetricType := strings.Replace(tc.outTSR[0].TimeSeries[0].Metric.Type, "custom.googleapis.com/opencensus/", "", -1)
saveMDRName := strings.Replace(tc.outMDR[0].MetricDescriptor.Name, "custom.googleapis.com/opencensus/", "", -1)
saveMDRDispName := tc.outMDR[0].MetricDescriptor.DisplayName

for _, prefix := range prefixes {
opts := defaultOpts
Expand All @@ -135,7 +132,6 @@ func TestMetricsWithPrefixWithDomain(t *testing.T) {
mt := strings.Replace(saveMetricType, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Name = strings.Replace(saveMDRName, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Type = mt
tc.outMDR[0].MetricDescriptor.DisplayName = strings.Replace(saveMDRDispName, "OpenCensus/"+metricName, (prefix + metricName), -1)

tc.outTSR[0].TimeSeries[0].Metric.Type = mt

Expand Down
45 changes: 45 additions & 0 deletions metrics_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,14 @@ func TestMetricPrefix(t *testing.T) {
},
want: "custom.googleapis.com/opencensus/prefix/my_metric",
},
{
name: "Has a prefix without `/` ending but prefix doesn't have a domain",
in: "my_metric",
statsExporter: &statsExporter{
o: Options{ProjectID: "foo", MetricPrefix: "prefix"},
},
want: "custom.googleapis.com/opencensus/prefix/my_metric",
},
{
name: "Has a prefix and prefix has a domain",
in: "my_metric",
Expand All @@ -1012,6 +1020,43 @@ func TestMetricPrefix(t *testing.T) {
},
want: "appengine.googleapis.com/my_metric",
},
{
name: "Has a GetMetricPrefix func but result doesn't have a domain",
in: "my_metric",
statsExporter: &statsExporter{
o: Options{
ProjectID: "foo",
GetMetricPrefix: func(name string) string {
return "prefix"
}},
},
want: "custom.googleapis.com/opencensus/prefix/my_metric",
},
{
name: "Has a GetMetricPrefix func and result has a domain",
in: "my_metric",
statsExporter: &statsExporter{
o: Options{
ProjectID: "foo",
GetMetricPrefix: func(name string) string {
return "knative.dev/serving"
}},
},
want: "knative.dev/serving/my_metric",
},
{
name: "Has both a prefix and GetMetricPrefix func",
in: "my_metric",
statsExporter: &statsExporter{
o: Options{
ProjectID: "foo",
MetricPrefix: "appengine.googleapis.com/",
GetMetricPrefix: func(name string) string {
return "knative.dev/serving"
}},
},
want: "knative.dev/serving/my_metric",
},
}

for _, tt := range tests {
Expand Down
16 changes: 11 additions & 5 deletions stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,9 @@ type Options struct {
// conversions from auto-detected resources to well-known Stackdriver monitored resources.
MapResource func(*resource.Resource) *monitoredrespb.MonitoredResource

// MetricPrefix overrides the prefix of a Stackdriver metric display names.
// Optional. If unset defaults to "OpenCensus/".
// Deprecated: Provide GetMetricDisplayName to change the display name of
// the metric.
// If GetMetricDisplayName is non-nil, this option is ignored.
// MetricPrefix overrides the prefix of a Stackdriver metric names.
// Optional. If unset defaults to "custom.googleapis.com/opencensus/".
// If GetMetricPrefix is non-nil, this option is ignored.
MetricPrefix string

// GetMetricDisplayName allows customizing the display name for the metric
Expand All @@ -203,8 +201,16 @@ type Options struct {
// "custom.googleapis.com/opencensus/" + view.Name
//
// See: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors#MetricDescriptor
// Depreacted. Use GetMetricPrefix instead.
GetMetricType func(view *view.View) string

// GetMetricPrefix allows customizing the metric prefix for the given metric name.
// If it is not set, MetricPrefix is used. If MetricPrefix is not set, it defaults to:
// "custom.googleapis.com/opencensus/"
//
// See: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors#MetricDescriptor
GetMetricPrefix func(name string) string

// DefaultTraceAttributes will be appended to every span that is exported to
// Stackdriver Trace.
DefaultTraceAttributes map[string]interface{}
Expand Down
6 changes: 1 addition & 5 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,7 @@ func (e *statsExporter) createMetricDescriptorFromView(ctx context.Context, v *v
}

func (e *statsExporter) displayName(suffix string) string {
displayNamePrefix := defaultDisplayNamePrefix
if e.o.MetricPrefix != "" {
displayNamePrefix = e.o.MetricPrefix
}
return path.Join(displayNamePrefix, suffix)
return path.Join(defaultDisplayNamePrefix, suffix)
}

func (e *statsExporter) combineTimeSeriesToCreateTimeSeriesRequest(ts []*monitoringpb.TimeSeries) (ctsreql []*monitoringpb.CreateTimeSeriesRequest) {
Expand Down

0 comments on commit 59d068f

Please sign in to comment.