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

Fix tests #12

Merged
merged 9 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
50 changes: 31 additions & 19 deletions http/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"

// "go.opentelemetry.io/otel/codes"
"github.com/krakend/krakend-otel/state"

"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
Expand All @@ -21,12 +22,14 @@ import (

type fakeService struct{}

func (s *fakeService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (*fakeService) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("foo bar"))
}

type testOTEL struct {
tracer trace.Tracer
tracerProvider trace.TracerProvider
meter metric.Meter
metricProvider metric.MeterProvider

metricReader *sdkmetric.ManualReader
Expand All @@ -40,8 +43,11 @@ func newTestOTEL() *testOTEL {

metricReader := sdkmetric.NewManualReader()
metricProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(metricReader))

return &testOTEL{
tracer: tracer,
tracerProvider: tracerProvider,
meter: metricProvider.Meter("io.krakend.krakend-otel"),
metricProvider: metricProvider,
metricReader: metricReader,
spanRecorder: spanRecorder,
Expand All @@ -52,15 +58,23 @@ func (o *testOTEL) Tracer() trace.Tracer {
return o.tracer
}

func (o *testOTEL) TracerProvider() trace.TracerProvider {
return o.tracerProvider
}

func (o *testOTEL) MeterProvider() metric.MeterProvider {
return o.metricProvider
}

func (o *testOTEL) Propagator() propagation.TextMapPropagator {
func (o *testOTEL) Meter() metric.Meter {
return o.meter
}

func (*testOTEL) Propagator() propagation.TextMapPropagator {
return nil
}

func (o *testOTEL) Shutdown(ctx context.Context) {
func (*testOTEL) Shutdown(_ context.Context) {
}

func TestInstrumentedHTTPClient(t *testing.T) {
Expand All @@ -70,9 +84,7 @@ func TestInstrumentedHTTPClient(t *testing.T) {
innerClient := &http.Client{}
otelInstance := newTestOTEL()
transportOptions := &TransportOptions{
OTELInstance: func() state.OTEL {
return otelInstance
},
OTELInstance: otelInstance,
TracesOpts: TransportTracesOptions{
RoundTrip: true,
ReadPayload: true,
Expand All @@ -89,9 +101,9 @@ func TestInstrumentedHTTPClient(t *testing.T) {
},
}

c, err := InstrumentedHTTPClient(innerClient, transportOptions, "test-http-client")
if err != nil {
t.Errorf("unexpected error %s", err.Error())
c := InstrumentedHTTPClient(innerClient, transportOptions, "test-http-client")
if c == nil {
t.Error("unable to create client")
return
}

Expand Down Expand Up @@ -159,14 +171,14 @@ func TestInstrumentedHTTPClient(t *testing.T) {
// "requests-failed-count": false // <- we do not have requests that failed
// "requests-canceled-count": false // <- we do not have requests cancelled
// "requests-timedout-count": false // <- we do not have requests timed out
"http.client.request.started.count": false,
"http.client.request.size": false,
"http.client.duration": false,
"http.client.response.size": false,
"http.client.response.read.size": false,
"http.client.response.read.size.hist": false,
"http.client.response.read.duration": false,
"http.client.response.read.duration.hist": false,
"http.client.request.started.count": false,
"http.client.request.size": false,
"http.client.duration": false,
"http.client.response.size": false,
"http.client.response.read.size": false,
"http.client.response.read.size-hist": false,
"http.client.response.read.time": false,
"http.client.response.read.time-hist": false,
// "reader-errors": false,
}
numWantedMetrics := len(wantedMetrics)
Expand All @@ -189,7 +201,7 @@ func TestInstrumentedHTTPClient(t *testing.T) {
}

// --> check that the metrics have the expected attributes set
readSize := gotMetrics["read-size"]
readSize := gotMetrics["http.client.response.read.size"]
readSizeSum, ok := readSize.Data.(metricdata.Sum[int64])
if !ok {
t.Errorf("cannot access read size aggregation: %#v", readSize.Data)
Expand Down
24 changes: 12 additions & 12 deletions io/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestReaderHappyPath(t *testing.T) {
attribute.Int("http.status_code", 201),
attribute.String("url.path", "/this/{matched}/path"),
}
reader := NewInstrumentedReader(&bw, ctx, attrsT, attrsM, tracer, meter)
reader := NewInstrumentedReader("", &bw, ctx, attrsT, attrsM, tracer, meter)

_, err = io.ReadAll(reader)
if err != nil {
Expand Down Expand Up @@ -88,10 +88,10 @@ func TestReaderHappyPath(t *testing.T) {
// --> check that we have all the metrics we want to report
sm := rm.ScopeMetrics[0]
wantedMetrics := map[string]bool{
"read-size": false,
"read-size-hist": false,
"read-time": false,
"read-time-hist": false,
"read.size": false,
"read.size-hist": false,
"read.time": false,
"read.time-hist": false,
// "reader-errors": false,
}
numWantedMetrics := len(wantedMetrics)
Expand All @@ -107,7 +107,7 @@ func TestReaderHappyPath(t *testing.T) {
}

// --> check that the metrics have the expected attributes set
readSize := gotMetrics["read-size"]
readSize := gotMetrics["read.size"]
readSizeSum, ok := readSize.Data.(metricdata.Sum[int64])
if !ok {
t.Errorf("cannot access read size aggregation: %#v", readSize.Data)
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestReaderTimeout(t *testing.T) {
attribute.Int("http.status_code", 201),
attribute.String("url.path", "/this/{matched}/path"),
}
reader := NewInstrumentedReader(innerReader, ctx, attrsT, attrsM, tracer, meter)
reader := NewInstrumentedReader("", innerReader, ctx, attrsT, attrsM, tracer, meter)

fourBytes := make([]byte, 4)
_, err = reader.Read(fourBytes)
Expand Down Expand Up @@ -214,10 +214,10 @@ func TestReaderTimeout(t *testing.T) {
// --> check that we have all the metrics we want to report
sm := rm.ScopeMetrics[0]
wantedMetrics := map[string]bool{
"read-size": false,
"read-size-hist": false,
"read-time": false,
"read-time-hist": false,
"read.size": false,
"read.size-hist": false,
"read.time": false,
"read.time-hist": false,
// "reader-errors": false,
}
numWantedMetrics := len(wantedMetrics)
Expand All @@ -233,7 +233,7 @@ func TestReaderTimeout(t *testing.T) {
}

// --> check that the metrics have the expected attributes set
readSize := gotMetrics["read-size"]
readSize := gotMetrics["read.size"]
readSizeSum, ok := readSize.Data.(metricdata.Sum[int64])
if !ok {
t.Errorf("cannot access read size aggregation: %#v", readSize.Data)
Expand Down
12 changes: 6 additions & 6 deletions io/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestWriterHappyPath(t *testing.T) {
attribute.Int("http.status_code", 201),
attribute.String("url.path", "/this/{matched}/path"),
}
writer := NewInstrumentedWriter(&bw, ctx, attrsT, attrsM, tracer, meter)
writer := NewInstrumentedWriter("", &bw, ctx, attrsT, attrsM, tracer, meter)

startedSpans := spanRecorder.Started()
if len(startedSpans) > initialStartedSpansLen {
Expand Down Expand Up @@ -103,10 +103,10 @@ func TestWriterHappyPath(t *testing.T) {
// --> check that we have all the metrics we want to report
sm := rm.ScopeMetrics[0]
wantedMetrics := map[string]bool{
"written-size": false,
"written-size-hist": false,
"written-time": false,
"written-time-hist": false,
"written.size": false,
"written.size-hist": false,
"written.time": false,
"written.time-hist": false,
// "written-errors": false,
}
numWantedMetrics := len(wantedMetrics)
Expand All @@ -122,7 +122,7 @@ func TestWriterHappyPath(t *testing.T) {
}

// --> check that the metrics have the expected attributes set
writtenSize := gotMetrics["written-size"]
writtenSize := gotMetrics["written.size"]
writtenSizeSum, ok := writtenSize.Data.(metricdata.Sum[int64])
if !ok {
t.Errorf("cannot access written size aggregation: %#v", writtenSize.Data)
Expand Down
4 changes: 3 additions & 1 deletion otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

lconfig "github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
)

func TestGlobalConfig(t *testing.T) {
Expand All @@ -23,10 +24,11 @@ func TestGlobalConfig(t *testing.T) {
},
}

err := Register(context.Background(), cfg)
shuftdownFn, err := Register(context.Background(), logging.NoOp, cfg)
if err != nil {
t.Errorf("unexpected error %s", err.Error())
}
shuftdownFn()
}

/*
Expand Down