Skip to content

Commit

Permalink
Fix linter findings
Browse files Browse the repository at this point in the history
  • Loading branch information
Hipska committed Nov 24, 2023
1 parent d04cb40 commit 7f45222
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
20 changes: 9 additions & 11 deletions plugins/outputs/bigquery/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down
11 changes: 8 additions & 3 deletions plugins/outputs/bigquery/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
})

Expand Down

0 comments on commit 7f45222

Please sign in to comment.