mirror of
https://github.com/hanzoai/krakend-otel.git
synced 2026-09-30 22:49:38 +00:00
fix a reader panic
fix a reader panic when the close function of the reader is called when no reader has been done
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/metric"
|
"go.opentelemetry.io/otel/metric"
|
||||||
"go.opentelemetry.io/otel/trace"
|
"go.opentelemetry.io/otel/trace"
|
||||||
|
nooptrace "go.opentelemetry.io/otel/trace/noop"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ io.ReadCloser = (*instrumentedReader)(nil)
|
var _ io.ReadCloser = (*instrumentedReader)(nil)
|
||||||
@@ -39,6 +40,7 @@ func NewInstrumentedReaderFactory(prefix string, attrT []attribute.KeyValue, att
|
|||||||
reader: rc,
|
reader: rc,
|
||||||
track: ioTracking{
|
track: ioTracking{
|
||||||
instr: instr,
|
instr: instr,
|
||||||
|
span: nooptrace.Span{},
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -255,3 +255,60 @@ func TestReaderTimeout(t *testing.T) {
|
|||||||
return
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user