Skip to content

Commit 47c6a86

Browse files
committed
Fix an issue when the error is nil but is different than nil
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 69ff46b commit 47c6a86

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

receiver/receiverhelper/obsreport.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package receiverhelper // import "go.opentelemetry.io/collector/receiver/receive
77

88
import (
99
"context"
10+
"reflect"
1011

1112
"go.opentelemetry.io/otel/attribute"
1213
"go.opentelemetry.io/otel/codes"
@@ -186,7 +187,7 @@ func (rec *ObsReport) endOp(
186187
attribute.Int64(acceptedItemsKey, int64(numAccepted)),
187188
attribute.Int64(refusedItemsKey, int64(numRefused)),
188189
)
189-
if err != nil {
190+
if !isErrorNil(err) {
190191
span.SetStatus(codes.Error, err.Error())
191192
}
192193
}
@@ -210,3 +211,7 @@ func (rec *ObsReport) recordMetrics(receiverCtx context.Context, signal pipeline
210211
acceptedMeasure.Add(receiverCtx, int64(numAccepted), metric.WithAttributes(rec.otelAttrs...))
211212
refusedMeasure.Add(receiverCtx, int64(numRefused), metric.WithAttributes(rec.otelAttrs...))
212213
}
214+
215+
func isErrorNil(err error) bool {
216+
return err == nil || reflect.ValueOf(err).IsNil()
217+
}

receiver/receiverhelper/obsreport_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,28 @@ func TestCheckReceiverLogsViews(t *testing.T) {
296296
assert.Error(t, tt.CheckReceiverLogs(transport, 0, 7))
297297
}
298298

299+
type myError struct {
300+
error
301+
}
302+
303+
func returnsError(b bool) error {
304+
var p *myError = nil
305+
if b {
306+
p = &myError{error: errors.New("")}
307+
}
308+
return p // Will always return a non-nil error.
309+
}
310+
311+
func TestIsErrorNil(t *testing.T) {
312+
assert.True(t, isErrorNil(nil))
313+
assert.False(t, isErrorNil(returnsError(true)))
314+
assert.True(t, returnsError(true) != nil)
315+
assert.True(t, isErrorNil(returnsError(false)))
316+
}
317+
299318
func testTelemetry(t *testing.T, id component.ID, testFunc func(t *testing.T, tt componenttest.TestTelemetry)) {
300319
tt, err := componenttest.SetupTelemetry(id)
301320
require.NoError(t, err)
302321
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })
303-
304322
testFunc(t, tt)
305323
}

0 commit comments

Comments
 (0)