collector_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package servertiming
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "sync"
  7. "testing"
  8. "time"
  9. )
  10. func TestCollectorHeaderValueAggregatesIntervals(t *testing.T) {
  11. startedAt := time.Unix(100, 0)
  12. collector := New(startedAt)
  13. collector.Record(MetricDatabase, startedAt.Add(10*time.Millisecond), startedAt.Add(40*time.Millisecond), 2)
  14. collector.Record(MetricRedis, startedAt.Add(30*time.Millisecond), startedAt.Add(50*time.Millisecond), 3)
  15. collector.Record(dependencyMetricName("openai"), startedAt.Add(70*time.Millisecond), startedAt.Add(100*time.Millisecond), 1)
  16. collector.Record(dependencyMetricName("github"), startedAt.Add(60*time.Millisecond), startedAt.Add(90*time.Millisecond), 1)
  17. got := collector.HeaderValue(startedAt.Add(120*time.Millisecond), "miss")
  18. want := `total;dur=120.0, app;dur=40.0, db;dur=30.0;desc="queries=2", redis;dur=20.0;desc="commands=3", cache;desc="miss", deps;dur=40.0;desc="calls=2", dep_github;dur=30.0;desc="calls=1", dep_openai;dur=30.0;desc="calls=1"`
  19. if got != want {
  20. t.Fatalf("HeaderValue() = %q, want %q", got, want)
  21. }
  22. }
  23. func TestRecordIntervalDoesNotIncrementCount(t *testing.T) {
  24. startedAt := time.Unix(200, 0)
  25. collector := New(startedAt)
  26. ctx := WithCollector(context.Background(), collector)
  27. Record(ctx, MetricDatabase, startedAt.Add(10*time.Millisecond), startedAt.Add(20*time.Millisecond), 1)
  28. RecordInterval(ctx, MetricDatabase, startedAt.Add(30*time.Millisecond), startedAt.Add(40*time.Millisecond))
  29. header := HeaderValue(ctx, startedAt.Add(100*time.Millisecond), "hit")
  30. if !strings.Contains(header, `db;dur=20.0;desc="queries=1"`) {
  31. t.Fatalf("header %q does not contain one query with both blocking intervals", header)
  32. }
  33. if !strings.Contains(header, "app;dur=80.0") {
  34. t.Fatalf("header %q does not subtract the interval union from app time", header)
  35. }
  36. }
  37. func TestCollectorCacheStatusFallback(t *testing.T) {
  38. startedAt := time.Unix(300, 0)
  39. collector := New(startedAt)
  40. ctx := WithCollector(context.Background(), collector)
  41. SetCacheStatus(ctx, " HIT ")
  42. if got := HeaderValue(ctx, startedAt.Add(time.Millisecond), "invalid"); !strings.Contains(got, `cache;desc="hit"`) {
  43. t.Fatalf("HeaderValue() = %q, want stored cache hit", got)
  44. }
  45. other := New(startedAt)
  46. if got := other.HeaderValue(startedAt.Add(time.Millisecond), "invalid"); !strings.Contains(got, `cache;desc="bypass"`) {
  47. t.Fatalf("HeaderValue() = %q, want cache bypass", got)
  48. }
  49. }
  50. func TestCollectorSanitizesDependencyMetric(t *testing.T) {
  51. startedAt := time.Unix(400, 0)
  52. collector := New(startedAt)
  53. ctx := WithCollector(context.Background(), collector)
  54. RecordDependency(ctx, "GitHub API\r\nInjected;dur=999", startedAt, startedAt.Add(time.Millisecond))
  55. header := HeaderValue(ctx, startedAt.Add(2*time.Millisecond), "bypass")
  56. if strings.ContainsAny(header, "\r\n") || strings.Contains(header, ";dur=999") {
  57. t.Fatalf("unsafe metric content reached header: %q", header)
  58. }
  59. if !strings.Contains(header, "dep_githubapiinjecteddur999;dur=1.0") {
  60. t.Fatalf("sanitized dependency metric missing from header: %q", header)
  61. }
  62. }
  63. func TestCollectorBoundsHeaderLength(t *testing.T) {
  64. startedAt := time.Unix(500, 0)
  65. collector := New(startedAt)
  66. for i := 0; i < 300; i++ {
  67. collector.Record(
  68. dependencyMetricName(fmt.Sprintf("module_%03d_with_a_deliberately_long_name", i)),
  69. startedAt,
  70. startedAt.Add(time.Millisecond),
  71. 1,
  72. )
  73. }
  74. header := collector.HeaderValue(startedAt.Add(2*time.Millisecond), "bypass")
  75. if len(header) > maxHeaderLength {
  76. t.Fatalf("header length = %d, want <= %d", len(header), maxHeaderLength)
  77. }
  78. if !strings.Contains(header, "total;dur=2.0") || !strings.Contains(header, "deps;dur=1.0") {
  79. t.Fatalf("bounded header lost fixed metrics: %q", header)
  80. }
  81. }
  82. func TestCollectorConcurrentRecording(t *testing.T) {
  83. startedAt := time.Now()
  84. collector := New(startedAt)
  85. ctx := WithCollector(context.Background(), collector)
  86. const workers = 25
  87. const recordsPerWorker = 100
  88. var wg sync.WaitGroup
  89. wg.Add(workers)
  90. for i := 0; i < workers; i++ {
  91. go func() {
  92. defer wg.Done()
  93. for j := 0; j < recordsPerWorker; j++ {
  94. Record(ctx, MetricDatabase, startedAt, startedAt.Add(time.Microsecond), 1)
  95. }
  96. }()
  97. }
  98. wg.Wait()
  99. header := HeaderValue(ctx, startedAt.Add(time.Millisecond), "bypass")
  100. want := fmt.Sprintf(`queries=%d`, workers*recordsPerWorker)
  101. if !strings.Contains(header, want) {
  102. t.Fatalf("header %q does not contain %q", header, want)
  103. }
  104. }
  105. func TestContextHelpersHandleMissingCollector(t *testing.T) {
  106. if Active(context.Background()) {
  107. t.Fatal("context without collector reported active")
  108. }
  109. if got := HeaderValue(context.Background(), time.Now(), "hit"); got != "" {
  110. t.Fatalf("HeaderValue() = %q without collector, want empty", got)
  111. }
  112. }