2024-01-08 08:10:06 +00:00
|
|
|
package opentelemetry_test
|
2022-11-29 14:34:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2023-01-06 08:10:05 +00:00
|
|
|
"time"
|
2022-11-29 14:34:05 +00:00
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
"github.com/containous/alice"
|
2022-11-29 14:34:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-01-08 08:10:06 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/static"
|
|
|
|
tracingMiddle "github.com/traefik/traefik/v3/pkg/middlewares/tracing"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/tracing"
|
2024-01-08 08:10:06 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/tracing/opentelemetry"
|
2024-02-06 09:04:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/types"
|
2022-11-29 14:34:05 +00:00
|
|
|
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"
|
|
|
|
)
|
|
|
|
|
2023-01-06 08:10:05 +00:00
|
|
|
func TestTracing(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
headers map[string]string
|
|
|
|
assertFn func(*testing.T, string)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "service name and version",
|
|
|
|
assertFn: func(t *testing.T, trace string) {
|
|
|
|
t.Helper()
|
2022-11-29 14:34:05 +00:00
|
|
|
|
2023-01-06 08:10:05 +00:00
|
|
|
assert.Regexp(t, `({"key":"service.name","value":{"stringValue":"traefik"}})`, trace)
|
|
|
|
assert.Regexp(t, `({"key":"service.version","value":{"stringValue":"dev"}})`, trace)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "context propagation",
|
|
|
|
headers: map[string]string{
|
|
|
|
"traceparent": "00-00000000000000000000000000000001-0000000000000001-01",
|
|
|
|
"tracestate": "foo=bar",
|
|
|
|
},
|
|
|
|
assertFn: func(t *testing.T, trace string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Regexp(t, `("traceId":"00000000000000000000000000000001")`, trace)
|
|
|
|
assert.Regexp(t, `("parentSpanId":"0000000000000001")`, trace)
|
|
|
|
assert.Regexp(t, `("traceState":"foo=bar")`, trace)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
traceCh := make(chan string)
|
|
|
|
collector := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-11-29 14:34:05 +00:00
|
|
|
gzr, err := gzip.NewReader(r.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
body, err := io.ReadAll(gzr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req := ptraceotlp.NewExportRequest()
|
|
|
|
err = req.UnmarshalProto(body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
marshalledReq, err := json.Marshal(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-01-06 08:10:05 +00:00
|
|
|
traceCh <- string(marshalledReq)
|
2022-11-29 14:34:05 +00:00
|
|
|
}))
|
2023-01-06 08:10:05 +00:00
|
|
|
t.Cleanup(collector.Close)
|
2022-11-29 14:34:05 +00:00
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
tracingConfig := &static.Tracing{
|
|
|
|
ServiceName: "traefik",
|
|
|
|
SampleRate: 1.0,
|
|
|
|
OTLP: &opentelemetry.Config{
|
2024-02-06 09:04:05 +00:00
|
|
|
HTTP: &types.OtelHTTP{
|
2024-01-08 08:10:06 +00:00
|
|
|
Endpoint: collector.URL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
newTracing, closer, err := tracing.NewTracing(tracingConfig)
|
2022-11-29 14:34:05 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-08 08:10:06 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = closer.Close()
|
|
|
|
})
|
2022-11-29 14:34:05 +00:00
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
chain := alice.New(tracingMiddle.WrapEntryPointHandler(context.Background(), newTracing, "test"))
|
|
|
|
epHandler, err := chain.Then(http.NotFoundHandler())
|
|
|
|
require.NoError(t, err)
|
2022-11-29 14:34:05 +00:00
|
|
|
|
2023-01-06 08:10:05 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "http://www.test.com", nil)
|
|
|
|
for k, v := range test.headers {
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
2022-11-29 14:34:05 +00:00
|
|
|
|
2023-01-06 08:10:05 +00:00
|
|
|
epHandler.ServeHTTP(rw, req)
|
2022-11-29 14:34:05 +00:00
|
|
|
|
2023-01-06 08:10:05 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Error("Trace not exported")
|
|
|
|
|
|
|
|
case trace := <-traceCh:
|
|
|
|
assert.Equal(t, http.StatusNotFound, rw.Code)
|
|
|
|
test.assertFn(t, trace)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-11-29 14:34:05 +00:00
|
|
|
}
|