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

[chore] [exporter/splunkhec] fix flaky test expecting all requests to be gzipped. #17178

Merged
merged 6 commits into from
Dec 21, 2022
Merged
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
34 changes: 20 additions & 14 deletions exporter/splunkhecexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -158,7 +159,7 @@ func TestConsumeMetricsData(t *testing.T) {
md pmetric.Metrics
reqTestFunc func(t *testing.T, r *http.Request)
httpResponseCode int
maxContentLength int
maxContentLength uint
wantErr bool
}{
{
Expand Down Expand Up @@ -201,23 +202,28 @@ func TestConsumeMetricsData(t *testing.T) {
md: generateLargeBatch(),
reqTestFunc: func(t *testing.T, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
assert.Equal(t, "keep-alive", r.Header.Get("Connection"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, "OpenTelemetry-Collector Splunk Exporter/v0.0.1", r.Header.Get("User-Agent"))
assert.Equal(t, "Splunk 1234", r.Header.Get("Authorization"))
assert.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
zipReader, err := gzip.NewReader(bytes.NewReader(body))
assert.NoError(t, err)
bodyBytes, _ := io.ReadAll(zipReader)
firstPayload := strings.Split(string(bodyBytes), "}{")[0]
var metric splunk.Event
err = json.Unmarshal([]byte(firstPayload+"}"), &metric)
if err != nil {
t.Fatal(err)
bodyBytes := body
// the last batch might not be zipped.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this created flakiness. Sometimes the last request sent by the batch was not gzipped. We account for it.

if "gzip" == r.Header.Get("Content-Encoding") {
zipReader, err2 := gzip.NewReader(bytes.NewReader(body))
require.NoError(t, err2)
bodyBytes, _ = io.ReadAll(zipReader)
}

events := strings.Split(string(bodyBytes), "}{")
firstPayload := events[0]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also sometimes the last batch had just one event. We fix it here by doing a better job separating HEC events.

if len(events) > 1 {
firstPayload += "}"
}

var metric splunk.Event
err = json.Unmarshal([]byte(firstPayload), &metric)
assert.NoError(t, err, fmt.Sprintf("could not read: %s", firstPayload))
assert.Equal(t, "test_splunk", metric.Source)
assert.Equal(t, "test_type", metric.SourceType)
assert.Equal(t, "test_index", metric.Index)
Expand Down Expand Up @@ -252,7 +258,7 @@ func TestConsumeMetricsData(t *testing.T) {
config.Index = "test_index"
config.SplunkAppName = "OpenTelemetry-Collector Splunk Exporter"
config.SplunkAppVersion = "v0.0.1"
config.MaxContentLengthMetrics = 1800
config.MaxContentLengthMetrics = tt.maxContentLength

sender, err := buildClient(options, config, zap.NewNop())
assert.NoError(t, err)
Expand Down