@@ -52,6 +52,7 @@ type Sink struct {
52
52
53
53
bucketer BucketFn
54
54
extractor ExtractLabelsFn
55
+ prefix string
55
56
taskInfo * taskInfo
56
57
57
58
mu sync.Mutex
@@ -69,6 +70,9 @@ type Config struct {
69
70
// variable parameters within a metric name.
70
71
// Optional. Defaults to DefaultLabelExtractor.
71
72
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
72
76
// The bucketer is used to determine histogram bucket boundaries
73
77
// for the sampled metrics. This will execute before the LabelExtractor.
74
78
// Optional. Defaults to DefaultBucketer.
@@ -147,6 +151,7 @@ func NewSink(client *monitoring.MetricClient, config *Config) *Sink {
147
151
s := & Sink {
148
152
client : client ,
149
153
extractor : config .LabelExtractor ,
154
+ prefix : "go-metrics/" ,
150
155
bucketer : config .Bucketer ,
151
156
interval : config .ReportingInterval ,
152
157
taskInfo : & taskInfo {
@@ -159,6 +164,13 @@ func NewSink(client *monitoring.MetricClient, config *Config) *Sink {
159
164
debugLogs : config .DebugLogs ,
160
165
}
161
166
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
+ }
162
174
// apply defaults if not configured explicitly
163
175
if s .extractor == nil {
164
176
s .extractor = DefaultLabelExtractor
@@ -207,6 +219,12 @@ func NewSink(client *monitoring.MetricClient, config *Config) *Sink {
207
219
return s
208
220
}
209
221
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
+
210
228
func (s * Sink ) flushMetrics (ctx context.Context ) {
211
229
if s .interval == 0 * time .Second {
212
230
return
@@ -297,7 +315,7 @@ func (s *Sink) report(ctx context.Context) {
297
315
}
298
316
ts = append (ts , & monitoringpb.TimeSeries {
299
317
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 ),
301
319
Labels : labels ,
302
320
},
303
321
MetricKind : metric .MetricDescriptor_GAUGE ,
@@ -330,7 +348,7 @@ func (s *Sink) report(ctx context.Context) {
330
348
}
331
349
ts = append (ts , & monitoringpb.TimeSeries {
332
350
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 ),
334
352
Labels : labels ,
335
353
},
336
354
MetricKind : metric .MetricDescriptor_GAUGE ,
@@ -370,7 +388,7 @@ func (s *Sink) report(ctx context.Context) {
370
388
371
389
ts = append (ts , & monitoringpb.TimeSeries {
372
390
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 ),
374
392
Labels : labels ,
375
393
},
376
394
MetricKind : metric .MetricDescriptor_CUMULATIVE ,
0 commit comments