2022-04-06 23:20:29 +00:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
2022-05-18 10:11:48 +00:00
|
|
|
"context"
|
2022-04-06 23:20:29 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2022-05-18 10:11:48 +00:00
|
|
|
"github.com/rs/zerolog"
|
2022-04-06 23:20:29 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
tracesdk "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-05-18 10:11:48 +00:00
|
|
|
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
|
2022-04-06 23:20:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewCfTracer(t *testing.T) {
|
2022-07-26 21:00:53 +00:00
|
|
|
log := zerolog.Nop()
|
2022-04-06 23:20:29 +00:00
|
|
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
2022-04-11 23:02:13 +00:00
|
|
|
req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
2023-02-22 14:52:44 +00:00
|
|
|
tr := NewTracedHTTPRequest(req, 0, &log)
|
2022-04-06 23:20:29 +00:00
|
|
|
assert.NotNil(t, tr)
|
|
|
|
assert.IsType(t, tracesdk.NewTracerProvider(), tr.TracerProvider)
|
|
|
|
assert.IsType(t, &InMemoryOtlpClient{}, tr.exporter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewCfTracerMultiple(t *testing.T) {
|
2022-07-26 21:00:53 +00:00
|
|
|
log := zerolog.Nop()
|
2022-04-06 23:20:29 +00:00
|
|
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
2022-04-11 23:02:13 +00:00
|
|
|
req.Header.Add(TracerContextName, "1241ce3ecdefc68854e8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
|
|
|
req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
2023-02-22 14:52:44 +00:00
|
|
|
tr := NewTracedHTTPRequest(req, 0, &log)
|
2022-04-06 23:20:29 +00:00
|
|
|
assert.NotNil(t, tr)
|
|
|
|
assert.IsType(t, tracesdk.NewTracerProvider(), tr.TracerProvider)
|
|
|
|
assert.IsType(t, &InMemoryOtlpClient{}, tr.exporter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewCfTracerNilHeader(t *testing.T) {
|
2022-07-26 21:00:53 +00:00
|
|
|
log := zerolog.Nop()
|
2022-04-06 23:20:29 +00:00
|
|
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
2022-04-11 23:02:13 +00:00
|
|
|
req.Header[http.CanonicalHeaderKey(TracerContextName)] = nil
|
2023-02-22 14:52:44 +00:00
|
|
|
tr := NewTracedHTTPRequest(req, 0, &log)
|
2022-04-06 23:20:29 +00:00
|
|
|
assert.NotNil(t, tr)
|
|
|
|
assert.IsType(t, trace.NewNoopTracerProvider(), tr.TracerProvider)
|
|
|
|
assert.IsType(t, &NoopOtlpClient{}, tr.exporter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewCfTracerInvalidHeaders(t *testing.T) {
|
2022-07-26 21:00:53 +00:00
|
|
|
log := zerolog.Nop()
|
2022-04-06 23:20:29 +00:00
|
|
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
|
|
|
for _, test := range [][]string{nil, {""}} {
|
2022-04-11 23:02:13 +00:00
|
|
|
req.Header[http.CanonicalHeaderKey(TracerContextName)] = test
|
2023-02-22 14:52:44 +00:00
|
|
|
tr := NewTracedHTTPRequest(req, 0, &log)
|
2022-04-06 23:20:29 +00:00
|
|
|
assert.NotNil(t, tr)
|
|
|
|
assert.IsType(t, trace.NewNoopTracerProvider(), tr.TracerProvider)
|
|
|
|
assert.IsType(t, &NoopOtlpClient{}, tr.exporter)
|
|
|
|
}
|
|
|
|
}
|
2022-05-18 10:11:48 +00:00
|
|
|
|
|
|
|
func TestAddingSpansWithNilMap(t *testing.T) {
|
2022-07-26 21:00:53 +00:00
|
|
|
log := zerolog.Nop()
|
2022-05-18 10:11:48 +00:00
|
|
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
|
|
|
req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
2023-02-22 14:52:44 +00:00
|
|
|
tr := NewTracedHTTPRequest(req, 0, &log)
|
2022-05-18 10:11:48 +00:00
|
|
|
|
|
|
|
exporter := tr.exporter.(*InMemoryOtlpClient)
|
|
|
|
|
|
|
|
// add fake spans
|
|
|
|
spans := createResourceSpans([]*tracepb.Span{createOtlpSpan(traceId)})
|
|
|
|
err := exporter.UploadTraces(context.Background(), spans)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// a panic shouldn't occur
|
2022-07-26 21:00:53 +00:00
|
|
|
tr.AddSpans(nil)
|
2022-05-18 10:11:48 +00:00
|
|
|
}
|
2024-11-11 14:56:12 +00:00
|
|
|
|
|
|
|
func FuzzNewIdentity(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, trace string) {
|
|
|
|
_, _ = NewIdentity(trace)
|
|
|
|
})
|
|
|
|
}
|