@@ -7,7 +7,7 @@ package metrics
77import (
88 "context"
99 "fmt"
10- "path "
10+ "strings "
1111 "time"
1212
1313 monitoring "cloud.google.com/go/monitoring/apiv3"
@@ -53,24 +53,44 @@ type MetricSet struct {
5353//metricsConfig holds a configuration specific for metrics metricset.
5454type metricsConfig struct {
5555 ServiceName string `config:"service" validate:"required"`
56- // MetricPrefix allows to specify the prefix string for MetricTypes
56+ // ServiceMetricPrefix allows to specify the prefix string for MetricTypes
5757 // Stackdriver requires metrics to be prefixed with a common prefix.
5858 // This prefix changes based on the services the metrics belongs to.
5959 ServiceMetricPrefix string `config:"service_metric_prefix"`
6060 MetricTypes []string `config:"metric_types" validate:"required"`
6161 Aligner string `config:"aligner"`
6262}
6363
64- func (mc metricsConfig ) AddPrefixTo (metric string ) string {
64+ // prefix returns the service metric prefix, falling back to the Google Cloud
65+ // monitoring service prefix when not specified.
66+ // The prefix is normalized to always end with '/'.
67+ func (mc metricsConfig ) prefix () string {
6568 prefix := mc .ServiceMetricPrefix
69+
6670 // NOTE: fallback to Google Cloud prefix for backward compatibility
6771 // Prefix <service>.googleapis.com/ works only for Google Cloud metrics
6872 // List: https://cloud.google.com/monitoring/api/metrics_gcp
6973 if prefix == "" {
70- prefix = mc .ServiceName + ".googleapis.com"
74+ prefix = mc .ServiceName + ".googleapis.com/ "
7175 }
7276
73- return path .Join (prefix , metric )
77+ // Final slash is part of prefix. Creating a prefix with final slash
78+ // normalize the prefix for other use cases
79+ if ! strings .HasSuffix (prefix , "/" ) {
80+ prefix = prefix + "/"
81+ }
82+
83+ return prefix
84+ }
85+
86+ // AddPrefixTo adds the required service metric prefix to the given metric
87+ func (mc metricsConfig ) AddPrefixTo (metric string ) string {
88+ return mc .prefix () + metric
89+ }
90+
91+ // RemovePrefixFrom removes service metric prefix from the given metric
92+ func (mc metricsConfig ) RemovePrefixFrom (metric string ) string {
93+ return strings .TrimPrefix (metric , mc .prefix ())
7494}
7595
7696type metricMeta struct {
@@ -152,7 +172,7 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) (err erro
152172 return err
153173 }
154174
155- events , err := m .eventMapping (ctx , responses , sdc . ServiceName )
175+ events , err := m .eventMapping (ctx , responses , sdc )
156176 if err != nil {
157177 err = errors .Wrap (err , "eventMapping failed" )
158178 m .Logger ().Error (err )
@@ -167,14 +187,14 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) (err erro
167187 return nil
168188}
169189
170- func (m * MetricSet ) eventMapping (ctx context.Context , tss []timeSeriesWithAligner , serviceName string ) ([]mb.Event , error ) {
171- e := newIncomingFieldExtractor (m .Logger ())
190+ func (m * MetricSet ) eventMapping (ctx context.Context , tss []timeSeriesWithAligner , sdc metricsConfig ) ([]mb.Event , error ) {
191+ e := newIncomingFieldExtractor (m .Logger (), sdc )
172192
173193 var gcpService = gcp .NewStackdriverMetadataServiceForTimeSeries (nil )
174194 var err error
175195
176196 if ! m .config .ExcludeLabels {
177- if gcpService , err = NewMetadataServiceForConfig (m .config , serviceName ); err != nil {
197+ if gcpService , err = NewMetadataServiceForConfig (m .config , sdc . ServiceName ); err != nil {
178198 return nil , errors .Wrap (err , "error trying to create metadata service" )
179199 }
180200 }
@@ -199,7 +219,7 @@ func (m *MetricSet) eventMapping(ctx context.Context, tss []timeSeriesWithAligne
199219 event .MetricSetFields .Put (singleEvent .Key , singleEvent .Value )
200220 }
201221
202- if serviceName == "compute" {
222+ if sdc . ServiceName == "compute" {
203223 event .RootFields = addHostFields (groupedEvents )
204224 } else {
205225 event .RootFields = groupedEvents [0 ].ECS
0 commit comments