2018-07-31 22:16:03 +00:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
2018-11-14 09:18:03 +00:00
|
|
|
"context"
|
2018-07-31 22:16:03 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/tracing"
|
2018-07-31 22:16:03 +00:00
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-07-31 22:16:03 +00:00
|
|
|
)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func TestEntryPointMiddleware(t *testing.T) {
|
|
|
|
type expected struct {
|
|
|
|
Tags map[string]interface{}
|
|
|
|
OperationName string
|
2018-07-31 22:16:03 +00:00
|
|
|
}
|
2018-08-01 11:36:03 +00:00
|
|
|
|
2018-07-31 22:16:03 +00:00
|
|
|
testCases := []struct {
|
2018-11-14 09:18:03 +00:00
|
|
|
desc string
|
|
|
|
entryPoint string
|
|
|
|
spanNameLimit int
|
|
|
|
tracing *trackingBackenMock
|
|
|
|
expected expected
|
2018-07-31 22:16:03 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "no truncation test",
|
|
|
|
entryPoint: "test",
|
|
|
|
spanNameLimit: 0,
|
|
|
|
tracing: &trackingBackenMock{
|
|
|
|
tracer: &MockTracer{Span: &MockSpan{Tags: make(map[string]interface{})}},
|
2018-07-31 22:16:03 +00:00
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
expected: expected{
|
|
|
|
Tags: map[string]interface{}{
|
|
|
|
"span.kind": ext.SpanKindRPCServerEnum,
|
|
|
|
"http.method": http.MethodGet,
|
|
|
|
"component": "",
|
|
|
|
"http.url": "http://www.test.com",
|
|
|
|
"http.host": "www.test.com",
|
|
|
|
},
|
|
|
|
OperationName: "EntryPoint test www.test.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "basic test",
|
|
|
|
entryPoint: "test",
|
|
|
|
spanNameLimit: 25,
|
|
|
|
tracing: &trackingBackenMock{
|
|
|
|
tracer: &MockTracer{Span: &MockSpan{Tags: make(map[string]interface{})}},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
Tags: map[string]interface{}{
|
|
|
|
"span.kind": ext.SpanKindRPCServerEnum,
|
|
|
|
"http.method": http.MethodGet,
|
|
|
|
"component": "",
|
|
|
|
"http.url": "http://www.test.com",
|
|
|
|
"http.host": "www.test.com",
|
|
|
|
},
|
|
|
|
OperationName: "EntryPoint te... ww... 0c15301b",
|
2018-07-31 22:16:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-11-14 09:18:03 +00:00
|
|
|
newTracing, err := tracing.NewTracing("", test.spanNameLimit, test.tracing)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "http://www.test.com", nil)
|
|
|
|
rw := httptest.NewRecorder()
|
2018-07-31 22:16:03 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
2018-07-31 22:16:03 +00:00
|
|
|
span := test.tracing.tracer.(*MockTracer).Span
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
tags := span.Tags
|
|
|
|
assert.Equal(t, test.expected.Tags, tags)
|
|
|
|
assert.Equal(t, test.expected.OperationName, span.OpName)
|
|
|
|
})
|
2018-07-31 22:16:03 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
handler := NewEntryPoint(context.Background(), newTracing, test.entryPoint, next)
|
|
|
|
handler.ServeHTTP(rw, req)
|
2018-07-31 22:16:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|