Skip to content

Commit b9eafef

Browse files
committed
Add a unit test for component status reporting
1 parent a041a17 commit b9eafef

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

exporter/elasticsearchexporter/esclient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (cl *clientLogger) LogRoundTrip(requ *http.Request, resp *http.Response, cl
8181
zap.NamedError("reason", clientErr),
8282
)
8383
zl.Debug("Request failed.", fields...)
84-
err := fmt.Errorf("Elasticsearch request failed: %v", clientErr.Error())
84+
err := fmt.Errorf("Elasticsearch request failed: %w", clientErr)
8585
componentstatus.ReportStatus(
8686
cl.componentHost, componentstatus.NewRecoverableErrorEvent(err))
8787

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package elasticsearchexporter
5+
6+
import (
7+
"errors"
8+
"io"
9+
"net/http"
10+
"net/url"
11+
"testing"
12+
"time"
13+
14+
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
16+
"go.opentelemetry.io/collector/component"
17+
"go.opentelemetry.io/collector/component/componentstatus"
18+
"go.uber.org/zap"
19+
)
20+
21+
func TestComponentStatus(t *testing.T) {
22+
statusChan := make(chan *componentstatus.Event, 1)
23+
reporter := &testStatusReporter{statusChan}
24+
esLogger := clientLogger{
25+
Logger: zap.New(nil),
26+
logRequestBody: false,
27+
logResponseBody: false,
28+
componentHost: reporter,
29+
}
30+
31+
// Pass in an error and make sure it's sent to the component status reporter
32+
esLogger.LogRoundTrip(nil, nil, io.EOF, time.Now(), 0)
33+
select {
34+
case event := <-statusChan:
35+
assert.True(t, errors.Is(event.Err(), io.EOF), "LogRoundTrip should report a component status error wrapping its error parameter")
36+
assert.Equal(t, componentstatus.StatusRecoverableError, event.Status(), "LogRoundTrip on an error parameter should report a recoverable error")
37+
default:
38+
require.Fail(t, "LogRoundTrip with an error should report a recoverable error status")
39+
}
40+
41+
// Pass in an http error status and make sure it's sent to the component status reporter
42+
esLogger.LogRoundTrip(&http.Request{URL: &url.URL{}}, &http.Response{StatusCode: 401, Status: "401 Unauthorized"}, nil, time.Now(), 0)
43+
select {
44+
case event := <-statusChan:
45+
err := event.Err()
46+
require.NotNil(t, err, "LogRoundTrip with an http error status should report a component status error")
47+
assert.Contains(t, err.Error(), "401 Unauthorized", "LogRoundTrip with an http error status should include the status in its error state")
48+
assert.Equal(t, componentstatus.StatusRecoverableError, event.Status(), "LogRoundTrip with an http error status should report a recoverable error")
49+
default:
50+
require.Fail(t, "LogRoundTrip with an http error code should report a recoverable error status")
51+
}
52+
53+
// Pass in an http success status and make sure the component status returns to OK
54+
esLogger.LogRoundTrip(&http.Request{URL: &url.URL{}}, &http.Response{StatusCode: 200}, nil, time.Now(), 0)
55+
select {
56+
case event := <-statusChan:
57+
assert.Nil(t, event.Err(), "LogRoundTrip with a success status shouldn't report a component status error")
58+
assert.Equal(t, componentstatus.StatusOK, event.Status(), "LogRoundTrip with a success status should report component status OK")
59+
default:
60+
require.Fail(t, "LogRoundTrip with an http success should report component status OK")
61+
}
62+
}
63+
64+
type testStatusReporter struct {
65+
statusChan chan *componentstatus.Event
66+
}
67+
68+
func (tsr *testStatusReporter) Report(event *componentstatus.Event) {
69+
tsr.statusChan <- event
70+
}
71+
72+
func (tsr *testStatusReporter) GetExtensions() map[component.ID]component.Component {
73+
return make(map[component.ID]component.Component)
74+
}

0 commit comments

Comments
 (0)