diff --git a/plugins/outputs/stackdriver/README.md b/plugins/outputs/stackdriver/README.md index af0684c61..6495c6f64 100644 --- a/plugins/outputs/stackdriver/README.md +++ b/plugins/outputs/stackdriver/README.md @@ -60,6 +60,12 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. ## * double: preferred datatype to allow queries by PromQL. # metric_data_type = "source" + ## Tags as resource labels + ## Tags defined in this option, when they exist, are added as a resource + ## label and not included as a metric label. The values from tags override + ## the values defined under the resource_labels config options. + # tags_as_resource_label = [] + ## Custom resource type # resource_type = "generic_node" diff --git a/plugins/outputs/stackdriver/sample.conf b/plugins/outputs/stackdriver/sample.conf index 4b9cbbdd2..824be6924 100644 --- a/plugins/outputs/stackdriver/sample.conf +++ b/plugins/outputs/stackdriver/sample.conf @@ -25,6 +25,12 @@ ## * double: preferred datatype to allow queries by PromQL. # metric_data_type = "source" + ## Tags as resource labels + ## Tags defined in this option, when they exist, are added as a resource + ## label and not included as a metric label. The values from tags override + ## the values defined under the resource_labels config options. + # tags_as_resource_label = [] + ## Custom resource type # resource_type = "generic_node" diff --git a/plugins/outputs/stackdriver/stackdriver.go b/plugins/outputs/stackdriver/stackdriver.go index 619f56902..aee56ccb3 100644 --- a/plugins/outputs/stackdriver/stackdriver.go +++ b/plugins/outputs/stackdriver/stackdriver.go @@ -27,14 +27,15 @@ var sampleConfig string // Stackdriver is the Google Stackdriver config info. type Stackdriver struct { - Project string `toml:"project"` - Namespace string `toml:"namespace"` - ResourceType string `toml:"resource_type"` - ResourceLabels map[string]string `toml:"resource_labels"` - MetricTypePrefix string `toml:"metric_type_prefix"` - MetricNameFormat string `toml:"metric_name_format"` - MetricDataType string `toml:"metric_data_type"` - Log telegraf.Logger `toml:"-"` + Project string `toml:"project"` + Namespace string `toml:"namespace"` + ResourceType string `toml:"resource_type"` + ResourceLabels map[string]string `toml:"resource_labels"` + MetricTypePrefix string `toml:"metric_type_prefix"` + MetricNameFormat string `toml:"metric_name_format"` + MetricDataType string `toml:"metric_data_type"` + TagsAsResourceLabels []string `toml:"tags_as_resource_label"` + Log telegraf.Logger `toml:"-"` client *monitoring.MetricClient counterCache *counterCache @@ -223,6 +224,16 @@ func (s *Stackdriver) sendBatch(batch []telegraf.Metric) error { Value: value, } + // Convert any declared tag to a resource label and remove it from + // the metric + resourceLabels := s.ResourceLabels + for _, tag := range s.TagsAsResourceLabels { + if val, ok := m.GetTag(tag); ok { + resourceLabels[tag] = val + m.RemoveTag(tag) + } + } + // Prepare time series. timeSeries := &monitoringpb.TimeSeries{ Metric: &metricpb.Metric{ @@ -232,7 +243,7 @@ func (s *Stackdriver) sendBatch(batch []telegraf.Metric) error { MetricKind: metricKind, Resource: &monitoredrespb.MonitoredResource{ Type: s.ResourceType, - Labels: s.ResourceLabels, + Labels: resourceLabels, }, Points: []*monitoringpb.Point{ dataPoint, @@ -254,7 +265,7 @@ func (s *Stackdriver) sendBatch(batch []telegraf.Metric) error { MetricKind: metricpb.MetricDescriptor_CUMULATIVE, Resource: &monitoredrespb.MonitoredResource{ Type: s.ResourceType, - Labels: s.ResourceLabels, + Labels: resourceLabels, }, Points: []*monitoringpb.Point{ dataPoint,