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
Show file tree
Hide file tree
Changes from 4 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
16 changes: 16 additions & 0 deletions .chloggen/fix_close_zip_twice.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: splunkhecexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Don't attempt to close the request body explicitly as go does this when sending data.

# One or more tracking issues related to the change
issues: [17177]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
6 changes: 6 additions & 0 deletions exporter/splunkhecexporter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ type bufferState struct {
bufFront *index
resource int
library int
closed bool
}

func (b *bufferState) reset() {
b.closed = false
b.buf.Reset()
b.compressionEnabled = false
b.writer = &cancellableBytesWriter{innerWriter: b.buf, maxCapacity: b.bufferMaxLen}
Expand All @@ -78,6 +80,10 @@ func (b *bufferState) Read(p []byte) (n int, err error) {
}

func (b *bufferState) Close() error {
if b.closed {
return nil
}
b.closed = true
atoulme marked this conversation as resolved.
Show resolved Hide resolved
if _, ok := b.writer.(*cancellableGzipWriter); ok {
return b.writer.(*cancellableGzipWriter).close()
}
Expand Down
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, err := gzip.NewReader(bytes.NewReader(body))
require.NoError(t, err)
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