@@ -52,6 +52,7 @@ type Sink struct {
5252
5353 bucketer BucketFn
5454 extractor ExtractLabelsFn
55+ prefix string
5556 taskInfo * taskInfo
5657
5758 mu sync.Mutex
@@ -69,6 +70,9 @@ type Config struct {
6970 // variable parameters within a metric name.
7071 // Optional. Defaults to DefaultLabelExtractor.
7172 LabelExtractor ExtractLabelsFn
73+ // Prefix of the metrics recorded. Defaults to "go-metrics/" so your metric "foo" will be recorded as
74+ // "custom.googleapis.com/go-metrics/foo".
75+ Prefix * string
7276 // The bucketer is used to determine histogram bucket boundaries
7377 // for the sampled metrics. This will execute before the LabelExtractor.
7478 // Optional. Defaults to DefaultBucketer.
@@ -147,6 +151,7 @@ func NewSink(client *monitoring.MetricClient, config *Config) *Sink {
147151 s := & Sink {
148152 client : client ,
149153 extractor : config .LabelExtractor ,
154+ prefix : "go-metrics/" ,
150155 bucketer : config .Bucketer ,
151156 interval : config .ReportingInterval ,
152157 taskInfo : & taskInfo {
@@ -159,6 +164,13 @@ func NewSink(client *monitoring.MetricClient, config *Config) *Sink {
159164 debugLogs : config .DebugLogs ,
160165 }
161166
167+ if config .Prefix != nil {
168+ if isValidMetricsPrefix (* config .Prefix ) {
169+ s .prefix = * config .Prefix
170+ } else {
171+ log .Printf ("%s is not valid string to be used as metrics name, using default value 'go-metrics/'" , * config .Prefix )
172+ }
173+ }
162174 // apply defaults if not configured explicitly
163175 if s .extractor == nil {
164176 s .extractor = DefaultLabelExtractor
@@ -207,6 +219,12 @@ func NewSink(client *monitoring.MetricClient, config *Config) *Sink {
207219 return s
208220}
209221
222+ func isValidMetricsPrefix (s string ) bool {
223+ // start with alphanumeric, can contain underscore in path (expect first char), slash is used to separate path.
224+ match , err := regexp .MatchString ("^(?:[a-z0-9](?:[a-z0-9_]*)/?)*$" , s )
225+ return err == nil && match
226+ }
227+
210228func (s * Sink ) flushMetrics (ctx context.Context ) {
211229 if s .interval == 0 * time .Second {
212230 return
@@ -297,7 +315,7 @@ func (s *Sink) report(ctx context.Context) {
297315 }
298316 ts = append (ts , & monitoringpb.TimeSeries {
299317 Metric : & metricpb.Metric {
300- Type : path . Join ("custom.googleapis.com" , "go-metrics" , name ),
318+ Type : fmt . Sprintf ("custom.googleapis.com/%s%s " , s . prefix , name ),
301319 Labels : labels ,
302320 },
303321 MetricKind : metric .MetricDescriptor_GAUGE ,
@@ -330,7 +348,7 @@ func (s *Sink) report(ctx context.Context) {
330348 }
331349 ts = append (ts , & monitoringpb.TimeSeries {
332350 Metric : & metricpb.Metric {
333- Type : path . Join ("custom.googleapis.com" , "go-metrics" , name ),
351+ Type : fmt . Sprintf ("custom.googleapis.com/%s%s " , s . prefix , name ),
334352 Labels : labels ,
335353 },
336354 MetricKind : metric .MetricDescriptor_GAUGE ,
@@ -370,7 +388,7 @@ func (s *Sink) report(ctx context.Context) {
370388
371389 ts = append (ts , & monitoringpb.TimeSeries {
372390 Metric : & metricpb.Metric {
373- Type : path . Join ("custom.googleapis.com" , "go-metrics" , name ),
391+ Type : fmt . Sprintf ("custom.googleapis.com/%s%s " , s . prefix , name ),
374392 Labels : labels ,
375393 },
376394 MetricKind : metric .MetricDescriptor_CUMULATIVE ,
0 commit comments