traefik/pkg/middlewares/tracing/forwarder_test.go

137 lines
4.9 KiB
Go
Raw Normal View History

package tracing
import (
2018-11-14 09:18:03 +00:00
"context"
"net/http"
"net/http/httptest"
"testing"
2018-11-14 09:18: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"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/tracing"
)
2018-11-14 09:18:03 +00:00
func TestNewForwarder(t *testing.T) {
type expected struct {
Tags map[string]interface{}
OperationName string
}
testCases := []struct {
2018-11-14 09:18:03 +00:00
desc string
spanNameLimit int
tracing *trackingBackenMock
service string
router string
expected expected
}{
{
2018-11-14 09:18:03 +00:00
desc: "Simple Forward Tracer without truncation and hashing",
spanNameLimit: 101,
tracing: &trackingBackenMock{
tracer: &MockTracer{Span: &MockSpan{Tags: make(map[string]interface{})}},
},
2018-11-14 09:18:03 +00:00
service: "some-service.domain.tld",
router: "some-service.domain.tld",
expected: expected{
Tags: map[string]interface{}{
2022-02-10 15:00:09 +00:00
"http.host": "www.test.com",
"http.method": "GET",
"http.url": "http://www.test.com/toto",
"traefik.service.name": "some-service.domain.tld",
"traefik.router.name": "some-service.domain.tld",
"span.kind": ext.SpanKindRPCClientEnum,
},
2018-11-14 09:18:03 +00:00
OperationName: "forward some-service.domain.tld/some-service.domain.tld",
},
2020-07-07 12:42:03 +00:00
},
{
2018-11-14 09:18:03 +00:00
desc: "Simple Forward Tracer with truncation and hashing",
spanNameLimit: 101,
tracing: &trackingBackenMock{
tracer: &MockTracer{Span: &MockSpan{Tags: make(map[string]interface{})}},
},
2018-11-14 09:18:03 +00:00
service: "some-service-100.slug.namespace.environment.domain.tld",
router: "some-service-100.slug.namespace.environment.domain.tld",
expected: expected{
Tags: map[string]interface{}{
2022-02-10 15:00:09 +00:00
"http.host": "www.test.com",
"http.method": "GET",
"http.url": "http://www.test.com/toto",
"traefik.service.name": "some-service-100.slug.namespace.environment.domain.tld",
"traefik.router.name": "some-service-100.slug.namespace.environment.domain.tld",
"span.kind": ext.SpanKindRPCClientEnum,
},
2018-11-14 09:18:03 +00:00
OperationName: "forward some-service-100.slug.namespace.enviro.../some-service-100.slug.namespace.enviro.../bc4a0d48",
},
},
{
2018-11-14 09:18:03 +00:00
desc: "Exactly 101 chars",
spanNameLimit: 101,
tracing: &trackingBackenMock{
tracer: &MockTracer{Span: &MockSpan{Tags: make(map[string]interface{})}},
},
2018-11-14 09:18:03 +00:00
service: "some-service1.namespace.environment.domain.tld",
router: "some-service1.namespace.environment.domain.tld",
expected: expected{
Tags: map[string]interface{}{
2022-02-10 15:00:09 +00:00
"http.host": "www.test.com",
"http.method": "GET",
"http.url": "http://www.test.com/toto",
"traefik.service.name": "some-service1.namespace.environment.domain.tld",
"traefik.router.name": "some-service1.namespace.environment.domain.tld",
"span.kind": ext.SpanKindRPCClientEnum,
},
2018-11-14 09:18:03 +00:00
OperationName: "forward some-service1.namespace.environment.domain.tld/some-service1.namespace.environment.domain.tld",
},
},
{
2018-11-14 09:18:03 +00:00
desc: "More than 101 chars",
spanNameLimit: 101,
tracing: &trackingBackenMock{
tracer: &MockTracer{Span: &MockSpan{Tags: make(map[string]interface{})}},
},
2018-11-14 09:18:03 +00:00
service: "some-service1.frontend.namespace.environment.domain.tld",
router: "some-service1.backend.namespace.environment.domain.tld",
expected: expected{
Tags: map[string]interface{}{
2022-02-10 15:00:09 +00:00
"http.host": "www.test.com",
"http.method": "GET",
"http.url": "http://www.test.com/toto",
"traefik.service.name": "some-service1.frontend.namespace.environment.domain.tld",
"traefik.router.name": "some-service1.backend.namespace.environment.domain.tld",
"span.kind": ext.SpanKindRPCClientEnum,
},
2018-11-14 09:18:03 +00:00
OperationName: "forward some-service1.frontend.namespace.envir.../some-service1.backend.namespace.enviro.../fa49dd23",
},
},
}
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/toto", nil)
req = req.WithContext(tracing.WithTracing(req.Context(), newTracing))
rw := httptest.NewRecorder()
next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
span := test.tracing.tracer.(*MockTracer).Span
tags := span.Tags
assert.Equal(t, test.expected.Tags, tags)
2023-11-17 00:50:06 +00:00
assert.LessOrEqual(t, len(test.expected.OperationName), test.spanNameLimit,
2018-11-14 09:18:03 +00:00
"the len of the operation name %q [len: %d] doesn't respect limit %d",
test.expected.OperationName, len(test.expected.OperationName), test.spanNameLimit)
assert.Equal(t, test.expected.OperationName, span.OpName)
})
2018-11-14 09:18:03 +00:00
handler := NewForwarder(context.Background(), test.router, test.service, next)
handler.ServeHTTP(rw, req)
})
}
}