From 9fa18f387de0aff4bbd61d51f880299480c3e476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Edstr=C3=B6m?= Date: Thu, 7 Feb 2019 15:35:27 +0100 Subject: [PATCH] Add config field for Stackdriver output resource_type --- plugins/outputs/stackdriver/README.md | 2 ++ plugins/outputs/stackdriver/stackdriver.go | 11 +++++-- .../outputs/stackdriver/stackdriver_test.go | 30 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/plugins/outputs/stackdriver/README.md b/plugins/outputs/stackdriver/README.md index a2d13e6e163b5..37fa1e828892c 100644 --- a/plugins/outputs/stackdriver/README.md +++ b/plugins/outputs/stackdriver/README.md @@ -7,6 +7,8 @@ Requires `project` to specify where Stackdriver metrics will be delivered to. Metrics are grouped by the `namespace` variable and metric key - eg: `custom.googleapis.com/telegraf/system/load5` +[Resource type](https://cloud.google.com/monitoring/api/resources) is configured by the `resource_type` variable (default `global`). + ### Configuration ```toml diff --git a/plugins/outputs/stackdriver/stackdriver.go b/plugins/outputs/stackdriver/stackdriver.go index a1c9e20dad56a..ce0b70bef53b8 100644 --- a/plugins/outputs/stackdriver/stackdriver.go +++ b/plugins/outputs/stackdriver/stackdriver.go @@ -19,8 +19,9 @@ import ( // Stackdriver is the Google Stackdriver config info. type Stackdriver struct { - Project string - Namespace string + Project string + Namespace string + ResourceType string `toml:"resource_type"` client *monitoring.MetricClient } @@ -64,6 +65,10 @@ func (s *Stackdriver) Connect() error { return fmt.Errorf("Namespace is a required field for stackdriver output") } + if s.ResourceType == "" { + s.ResourceType = "global" + } + if s.client == nil { ctx := context.Background() client, err := monitoring.NewMetricClient(ctx) @@ -137,7 +142,7 @@ func (s *Stackdriver) Write(metrics []telegraf.Metric) error { }, MetricKind: metricKind, Resource: &monitoredrespb.MonitoredResource{ - Type: "global", + Type: s.ResourceType, Labels: map[string]string{ "project_id": s.Project, }, diff --git a/plugins/outputs/stackdriver/stackdriver_test.go b/plugins/outputs/stackdriver/stackdriver_test.go index d9aab38fdf8ba..55558e8426f3d 100644 --- a/plugins/outputs/stackdriver/stackdriver_test.go +++ b/plugins/outputs/stackdriver/stackdriver_test.go @@ -97,6 +97,36 @@ func TestWrite(t *testing.T) { require.NoError(t, err) err = s.Write(testutil.MockMetrics()) require.NoError(t, err) + + request := mockMetric.reqs[0].(*monitoringpb.CreateTimeSeriesRequest) + require.Equal(t, request.TimeSeries[0].Resource.Type, "global") +} + +func TestWriteResourceType(t *testing.T) { + expectedResponse := &emptypb.Empty{} + mockMetric.err = nil + mockMetric.reqs = nil + mockMetric.resps = append(mockMetric.resps[:0], expectedResponse) + + c, err := monitoring.NewMetricClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + + s := &Stackdriver{ + Project: fmt.Sprintf("projects/%s", "[PROJECT]"), + Namespace: "test", + ResourceType: "foo", + client: c, + } + + err = s.Connect() + require.NoError(t, err) + err = s.Write(testutil.MockMetrics()) + require.NoError(t, err) + + request := mockMetric.reqs[0].(*monitoringpb.CreateTimeSeriesRequest) + require.Equal(t, request.TimeSeries[0].Resource.Type, "foo") } func TestWriteAscendingTime(t *testing.T) {