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

Commit 59d068f

Browse files
yanweiguosongy23
authored andcommitted
Add GetMetricPrefix func as an option to support dynamic custom metric prefix (#238)
* add GetMetricPrefix * change comment * change display in tests * fix typo
1 parent 61e3caa commit 59d068f

5 files changed

Lines changed: 62 additions & 16 deletions

File tree

metrics_proto.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var percentileLabelKey = &metricspb.LabelKey{
4545
Description: "the value at a given percentile of a distribution",
4646
}
4747
var globalResource = &resource.Resource{Type: "global"}
48-
var domains = []string{"googleapis.com", "kubernetes.io", "istio.io"}
48+
var domains = []string{"googleapis.com", "kubernetes.io", "istio.io", "knative.dev"}
4949

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

411411
func (se *statsExporter) metricTypeFromProto(name string) string {
412412
prefix := se.o.MetricPrefix
413+
if se.o.GetMetricPrefix != nil {
414+
prefix = se.o.GetMetricPrefix(name)
415+
}
413416
if prefix != "" {
414-
name = prefix + name
417+
name = path.Join(prefix, name)
415418
}
416419
if !hasDomain(name) {
417420
// Still needed because the name may or may not have a "/" at the beginning.

metrics_proto_api_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ func TestMetricsWithPrefix(t *testing.T) {
9494
metricName := "ocagent.io"
9595
saveMetricType := tc.outTSR[0].TimeSeries[0].Metric.Type
9696
saveMDRName := tc.outMDR[0].MetricDescriptor.Name
97-
saveMDRDispName := tc.outMDR[0].MetricDescriptor.DisplayName
9897

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

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

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

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

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

metrics_proto_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,14 @@ func TestMetricPrefix(t *testing.T) {
10041004
},
10051005
want: "custom.googleapis.com/opencensus/prefix/my_metric",
10061006
},
1007+
{
1008+
name: "Has a prefix without `/` ending but prefix doesn't have a domain",
1009+
in: "my_metric",
1010+
statsExporter: &statsExporter{
1011+
o: Options{ProjectID: "foo", MetricPrefix: "prefix"},
1012+
},
1013+
want: "custom.googleapis.com/opencensus/prefix/my_metric",
1014+
},
10071015
{
10081016
name: "Has a prefix and prefix has a domain",
10091017
in: "my_metric",
@@ -1012,6 +1020,43 @@ func TestMetricPrefix(t *testing.T) {
10121020
},
10131021
want: "appengine.googleapis.com/my_metric",
10141022
},
1023+
{
1024+
name: "Has a GetMetricPrefix func but result doesn't have a domain",
1025+
in: "my_metric",
1026+
statsExporter: &statsExporter{
1027+
o: Options{
1028+
ProjectID: "foo",
1029+
GetMetricPrefix: func(name string) string {
1030+
return "prefix"
1031+
}},
1032+
},
1033+
want: "custom.googleapis.com/opencensus/prefix/my_metric",
1034+
},
1035+
{
1036+
name: "Has a GetMetricPrefix func and result has a domain",
1037+
in: "my_metric",
1038+
statsExporter: &statsExporter{
1039+
o: Options{
1040+
ProjectID: "foo",
1041+
GetMetricPrefix: func(name string) string {
1042+
return "knative.dev/serving"
1043+
}},
1044+
},
1045+
want: "knative.dev/serving/my_metric",
1046+
},
1047+
{
1048+
name: "Has both a prefix and GetMetricPrefix func",
1049+
in: "my_metric",
1050+
statsExporter: &statsExporter{
1051+
o: Options{
1052+
ProjectID: "foo",
1053+
MetricPrefix: "appengine.googleapis.com/",
1054+
GetMetricPrefix: func(name string) string {
1055+
return "knative.dev/serving"
1056+
}},
1057+
},
1058+
want: "knative.dev/serving/my_metric",
1059+
},
10151060
}
10161061

10171062
for _, tt := range tests {

stackdriver.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,9 @@ type Options struct {
186186
// conversions from auto-detected resources to well-known Stackdriver monitored resources.
187187
MapResource func(*resource.Resource) *monitoredrespb.MonitoredResource
188188

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

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

207+
// GetMetricPrefix allows customizing the metric prefix for the given metric name.
208+
// If it is not set, MetricPrefix is used. If MetricPrefix is not set, it defaults to:
209+
// "custom.googleapis.com/opencensus/"
210+
//
211+
// See: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors#MetricDescriptor
212+
GetMetricPrefix func(name string) string
213+
208214
// DefaultTraceAttributes will be appended to every span that is exported to
209215
// Stackdriver Trace.
210216
DefaultTraceAttributes map[string]interface{}

stats.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,7 @@ func (e *statsExporter) createMetricDescriptorFromView(ctx context.Context, v *v
365365
}
366366

367367
func (e *statsExporter) displayName(suffix string) string {
368-
displayNamePrefix := defaultDisplayNamePrefix
369-
if e.o.MetricPrefix != "" {
370-
displayNamePrefix = e.o.MetricPrefix
371-
}
372-
return path.Join(displayNamePrefix, suffix)
368+
return path.Join(defaultDisplayNamePrefix, suffix)
373369
}
374370

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

0 commit comments

Comments
 (0)