Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config field for Stackdriver output resource_type #5389

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/outputs/stackdriver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions plugins/outputs/stackdriver/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
},
Expand Down
30 changes: 30 additions & 0 deletions plugins/outputs/stackdriver/stackdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down