Skip to content

Commit

Permalink
Merge pull request #32 from emilien-puget/fix-reader
Browse files Browse the repository at this point in the history
fix a reader panic
  • Loading branch information
dhontecillas authored Jul 10, 2024
2 parents d24a596 + e9f6d66 commit efc5ab6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions io/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
nooptrace "go.opentelemetry.io/otel/trace/noop"
)

var _ io.ReadCloser = (*instrumentedReader)(nil)
Expand Down Expand Up @@ -39,6 +40,7 @@ func NewInstrumentedReaderFactory(prefix string, attrT []attribute.KeyValue, att
reader: rc,
track: ioTracking{
instr: instr,
span: nooptrace.Span{},
ctx: ctx,
},
}
Expand Down
57 changes: 57 additions & 0 deletions io/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,60 @@ func TestReaderTimeout(t *testing.T) {
return
}
}

func TestReader_noread(t *testing.T) {
var err error

spanRecorder := sdktracetest.NewSpanRecorder()
tracerProvider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
tracer := tracerProvider.Tracer("test-tracer")
ctx, _ := tracer.Start(context.Background(), "test-body-tracker")

metricReader := sdkmetric.NewManualReader()
metricProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(metricReader))
meter := metricProvider.Meter("test-meter")

// this is the buffer where we want to write something:
var bw bytes.Buffer
bw.Grow(64 * 1024)

payload := "foo bar"
bw.WriteString(payload)

attrsT := []attribute.KeyValue{
attribute.Int("http.status_code", 201),
attribute.String("url.path.pattern", "/this/{matched}/path"),
attribute.String("url.path", "/this/some_value/path"),
}
attrsM := []attribute.KeyValue{
attribute.Int("http.status_code", 201),
attribute.String("url.path", "/this/{matched}/path"),
}
reader := NewInstrumentedReader("", &bw, ctx, attrsT, attrsM, tracer, meter)
reader.Close()

endedSpans := spanRecorder.Ended()
if len(endedSpans) != 0 {
t.Errorf("num ended spans, want: 0, got: %d", len(endedSpans))
for idx, s := range endedSpans {
t.Errorf("%d -> %#v", idx, s)
}
return
}

// check the reported metrics
rm := metricdata.ResourceMetrics{}
err = metricReader.Collect(context.Background(), &rm)
if err != nil {
t.Errorf("collecting metrics err: %s", err.Error())
return
}

if len(rm.ScopeMetrics) != 1 {
t.Errorf("wrong amount of metrics, want: 1, got: %d", len(rm.ScopeMetrics))
for idx, sm := range rm.ScopeMetrics {
t.Errorf("%d -> %#v", idx, sm)
}
return
}
}

0 comments on commit efc5ab6

Please sign in to comment.