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

Added HEC error messages #36871

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions .chloggen/hec_errors_msg_improvements.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# 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: Added messages returned by Splunk to error displayed in logs when there is some issue with exporting events with HEC

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35712]

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
20 changes: 20 additions & 0 deletions internal/splunk/httprequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package splunk // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"strconv"
Expand All @@ -29,6 +32,23 @@ func HandleHTTPCode(resp *http.Response) error {
resp.StatusCode,
http.StatusText(resp.StatusCode))

// Check if there is any error text returned by Splunk that we can append to the error message
if resp.ContentLength > 0 {
var jsonResponse map[string]any
bodyString, _ := io.ReadAll(resp.Body)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bodyString, _ := io.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)

https://pkg.go.dev/io#ReadAll returns a byte array, bodyString is misleading, this is not a string.

resp.Body = io.NopCloser(bytes.NewBuffer(bodyString))
unmarshalError := json.Unmarshal(bodyString, &jsonResponse)

if unmarshalError == nil {
responseErrorValue, ok := jsonResponse["text"]
if ok {
err = multierr.Append(err, fmt.Errorf("reason: %v", responseErrorValue))
} else {
err = multierr.Append(err, fmt.Errorf("no error message from Splunk found"))
}
}
}

switch resp.StatusCode {
// Check for responses that may include "Retry-After" header.
case http.StatusTooManyRequests, http.StatusServiceUnavailable:
Expand Down
46 changes: 46 additions & 0 deletions internal/splunk/httprequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@ package splunk

import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/multierr"
)

func TestConsumeMetrics(t *testing.T) {
tests := []struct {
name string
httpResponseCode int
retryAfter int
respBody string
wantErr bool
wantPermanentErr bool
wantThrottleErr bool
wantErrMessage bool
noErrMessage bool
responseOk bool
}{
{
name: "response_forbidden",
Expand All @@ -49,6 +56,24 @@ func TestConsumeMetrics(t *testing.T) {
name: "large_batch",
httpResponseCode: http.StatusAccepted,
},
{
name: "response_disabled_token",
httpResponseCode: http.StatusForbidden,
respBody: "{\"text\":\"Token disabled\",\"code\":1}",
wantErrMessage: true,
},
{
name: "response_no_text",
httpResponseCode: http.StatusForbidden,
respBody: "{\"code\":1}",
noErrMessage: true,
},
{
name: "response_ok",
httpResponseCode: http.StatusOK,
respBody: "{\"text\":\"ok\"}",
responseOk: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -61,6 +86,11 @@ func TestConsumeMetrics(t *testing.T) {
}
}

if tt.respBody != "" {
resp.Body = io.NopCloser(strings.NewReader(tt.respBody))
resp.ContentLength = int64(len(tt.respBody))
}

err := HandleHTTPCode(resp)
if tt.wantErr {
assert.Error(t, err)
Expand All @@ -80,6 +110,22 @@ func TestConsumeMetrics(t *testing.T) {
return
}

if tt.wantErrMessage {
assert.Error(t, err)
expected := fmt.Errorf("HTTP %d %q", tt.httpResponseCode, http.StatusText(tt.httpResponseCode))
expected = multierr.Append(expected, fmt.Errorf("reason: %v", "Token disabled"))
assert.EqualValues(t, expected, err)
return
}

if tt.noErrMessage {
assert.Error(t, err)
expected := fmt.Errorf("HTTP %d %q", tt.httpResponseCode, http.StatusText(tt.httpResponseCode))
expected = multierr.Append(expected, fmt.Errorf("no error message from Splunk found"))
assert.EqualValues(t, expected, err)
return
}

assert.NoError(t, err)
})
}
Expand Down
Loading