tracing_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package tracing
  2. import (
  3. "context"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/rs/zerolog"
  8. "github.com/stretchr/testify/assert"
  9. tracesdk "go.opentelemetry.io/otel/sdk/trace"
  10. "go.opentelemetry.io/otel/trace"
  11. tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
  12. )
  13. func TestNewCfTracer(t *testing.T) {
  14. log := zerolog.Nop()
  15. req := httptest.NewRequest("GET", "http://localhost", nil)
  16. req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
  17. tr := NewTracedHTTPRequest(req, 0, &log)
  18. assert.NotNil(t, tr)
  19. assert.IsType(t, tracesdk.NewTracerProvider(), tr.TracerProvider)
  20. assert.IsType(t, &InMemoryOtlpClient{}, tr.exporter)
  21. }
  22. func TestNewCfTracerMultiple(t *testing.T) {
  23. log := zerolog.Nop()
  24. req := httptest.NewRequest("GET", "http://localhost", nil)
  25. req.Header.Add(TracerContextName, "1241ce3ecdefc68854e8514e69ba42ca:b38f1bf5eae406f3:0:1")
  26. req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
  27. tr := NewTracedHTTPRequest(req, 0, &log)
  28. assert.NotNil(t, tr)
  29. assert.IsType(t, tracesdk.NewTracerProvider(), tr.TracerProvider)
  30. assert.IsType(t, &InMemoryOtlpClient{}, tr.exporter)
  31. }
  32. func TestNewCfTracerNilHeader(t *testing.T) {
  33. log := zerolog.Nop()
  34. req := httptest.NewRequest("GET", "http://localhost", nil)
  35. req.Header[http.CanonicalHeaderKey(TracerContextName)] = nil
  36. tr := NewTracedHTTPRequest(req, 0, &log)
  37. assert.NotNil(t, tr)
  38. assert.IsType(t, trace.NewNoopTracerProvider(), tr.TracerProvider)
  39. assert.IsType(t, &NoopOtlpClient{}, tr.exporter)
  40. }
  41. func TestNewCfTracerInvalidHeaders(t *testing.T) {
  42. log := zerolog.Nop()
  43. req := httptest.NewRequest("GET", "http://localhost", nil)
  44. for _, test := range [][]string{nil, {""}} {
  45. req.Header[http.CanonicalHeaderKey(TracerContextName)] = test
  46. tr := NewTracedHTTPRequest(req, 0, &log)
  47. assert.NotNil(t, tr)
  48. assert.IsType(t, trace.NewNoopTracerProvider(), tr.TracerProvider)
  49. assert.IsType(t, &NoopOtlpClient{}, tr.exporter)
  50. }
  51. }
  52. func TestAddingSpansWithNilMap(t *testing.T) {
  53. log := zerolog.Nop()
  54. req := httptest.NewRequest("GET", "http://localhost", nil)
  55. req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
  56. tr := NewTracedHTTPRequest(req, 0, &log)
  57. exporter := tr.exporter.(*InMemoryOtlpClient)
  58. // add fake spans
  59. spans := createResourceSpans([]*tracepb.Span{createOtlpSpan(traceId)})
  60. err := exporter.UploadTraces(context.Background(), spans)
  61. assert.NoError(t, err)
  62. // a panic shouldn't occur
  63. tr.AddSpans(nil)
  64. }
  65. func FuzzNewIdentity(f *testing.F) {
  66. f.Fuzz(func(t *testing.T, trace string) {
  67. _, _ = NewIdentity(trace)
  68. })
  69. }