diff --git a/plugins/outputs/bigquery/bigquery.go b/plugins/outputs/bigquery/bigquery.go index 0c2a245ee38c6..06037dbd98907 100644 --- a/plugins/outputs/bigquery/bigquery.go +++ b/plugins/outputs/bigquery/bigquery.go @@ -135,13 +135,12 @@ func (s *BigQuery) Write(metrics []telegraf.Metric) error { } func (s *BigQuery) writeCompact(metrics []telegraf.Metric) error { - - inserter := s.client.DatasetInProject(s.Project, s.Dataset).Table(s.CompactTableName).Inserter() - ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, time.Duration(s.Timeout)) defer cancel() + inserter := s.client.DatasetInProject(s.Project, s.Dataset).Table(s.CompactTableName).Inserter() + compactValues := make([]*bigquery.ValuesSaver, len(metrics)) for i, m := range metrics { compactValues[i] = s.newCompactValuesSaver(m) @@ -176,18 +175,17 @@ func newValuesSaver(m telegraf.Metric) *bigquery.ValuesSaver { } } -func (b *BigQuery) newCompactValuesSaver(m telegraf.Metric) *bigquery.ValuesSaver { - - b.serializer.Transformation = "tags" - tags, err := b.serializer.Serialize(m) +func (s *BigQuery) newCompactValuesSaver(m telegraf.Metric) *bigquery.ValuesSaver { + s.serializer.Transformation = "tags" + tags, err := s.serializer.Serialize(m) if err != nil { - b.Log.Warnf("serializing tags: %v", err) + s.Log.Warnf("serializing tags: %v", err) } - b.serializer.Transformation = "fields" - fields, err := b.serializer.Serialize(m) + s.serializer.Transformation = "fields" + fields, err := s.serializer.Serialize(m) if err != nil { - b.Log.Warnf("serializing fields: %v", err) + s.Log.Warnf("serializing fields: %v", err) } return &bigquery.ValuesSaver{ diff --git a/plugins/outputs/bigquery/bigquery_test.go b/plugins/outputs/bigquery/bigquery_test.go index d51e94e00c525..2633febab828a 100644 --- a/plugins/outputs/bigquery/bigquery_test.go +++ b/plugins/outputs/bigquery/bigquery_test.go @@ -188,6 +188,8 @@ func TestWriteCompact(t *testing.T) { "tags": "{\"tag1\":\"value1\"}\n", "fields": "{\"value\":1}\n", }, row) + + require.NoError(t, b.Close()) } func (b *BigQuery) setUpTestClient(endpointURL string) error { @@ -221,16 +223,19 @@ func localBigQueryServer(t *testing.T) *httptest.Server { require.NoError(t, err) case "/projects/test-project/datasets/test-dataset/tables/test-metrics": w.WriteHeader(http.StatusOK) - w.Write([]byte("{}")) + _, err := w.Write([]byte("{}")) + require.NoError(t, err) case "/projects/test-project/datasets/test-dataset/tables/test-metrics/insertAll": decoder := json.NewDecoder(r.Body) require.NoError(t, decoder.Decode(&receivedBody)) w.WriteHeader(http.StatusOK) - w.Write([]byte(successfulResponse)) + _, err := w.Write([]byte(successfulResponse)) + require.NoError(t, err) default: w.WriteHeader(http.StatusNotFound) - w.Write([]byte(r.URL.String())) + _, err := w.Write([]byte(r.URL.String())) + require.NoError(t, err) } })