Skip to content

Commit 2d66573

Browse files
committed
merge categories
1 parent de5b70a commit 2d66573

File tree

6 files changed

+128
-264
lines changed

6 files changed

+128
-264
lines changed

internal/ratelimit/category.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,47 @@ func (c Category) String() string {
5757
return rv
5858
}
5959
}
60+
61+
// Priority represents the importance level of a category for buffer management.
62+
type Priority int
63+
64+
const (
65+
PriorityCritical Priority = iota + 1
66+
PriorityHigh
67+
PriorityMedium
68+
PriorityLow
69+
PriorityLowest
70+
)
71+
72+
func (p Priority) String() string {
73+
switch p {
74+
case PriorityCritical:
75+
return "critical"
76+
case PriorityHigh:
77+
return "high"
78+
case PriorityMedium:
79+
return "medium"
80+
case PriorityLow:
81+
return "low"
82+
case PriorityLowest:
83+
return "lowest"
84+
default:
85+
return "unknown"
86+
}
87+
}
88+
89+
// GetPriority returns the priority level for this category.
90+
func (c Category) GetPriority() Priority {
91+
switch c {
92+
case CategoryError:
93+
return PriorityCritical
94+
case CategoryMonitor:
95+
return PriorityHigh
96+
case CategoryLog:
97+
return PriorityMedium
98+
case CategoryTransaction:
99+
return PriorityLow
100+
default:
101+
return PriorityMedium
102+
}
103+
}

internal/ratelimit/category_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,42 @@ func TestKnownCategories(t *testing.T) {
6060
})
6161
}
6262
}
63+
64+
func TestPriority_String(t *testing.T) {
65+
tests := []struct {
66+
priority Priority
67+
expected string
68+
}{
69+
{PriorityCritical, "critical"},
70+
{PriorityHigh, "high"},
71+
{PriorityMedium, "medium"},
72+
{PriorityLow, "low"},
73+
{PriorityLowest, "lowest"},
74+
{Priority(999), "unknown"},
75+
}
76+
77+
for _, tt := range tests {
78+
if got := tt.priority.String(); got != tt.expected {
79+
t.Errorf("Priority(%d).String() = %q, want %q", tt.priority, got, tt.expected)
80+
}
81+
}
82+
}
83+
84+
func TestCategory_GetPriority(t *testing.T) {
85+
tests := []struct {
86+
category Category
87+
expected Priority
88+
}{
89+
{CategoryError, PriorityCritical},
90+
{CategoryMonitor, PriorityHigh},
91+
{CategoryLog, PriorityMedium},
92+
{CategoryTransaction, PriorityLow},
93+
{Category("unknown"), PriorityMedium},
94+
}
95+
96+
for _, tt := range tests {
97+
if got := tt.category.GetPriority(); got != tt.expected {
98+
t.Errorf("Category(%q).GetPriority() = %s, want %s", tt.category, got, tt.expected)
99+
}
100+
}
101+
}

internal/telemetry/buffer.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"sync"
55
"sync/atomic"
66
"time"
7+
8+
"github.com/getsentry/sentry-go/internal/ratelimit"
79
)
810

911
const defaultCapacity = 100
@@ -17,16 +19,16 @@ type Buffer[T any] struct {
1719
size int
1820
capacity int
1921

20-
category DataCategory
21-
priority Priority
22+
category ratelimit.Category
23+
priority ratelimit.Priority
2224
overflowPolicy OverflowPolicy
2325

2426
offered int64
2527
dropped int64
2628
onDropped func(item T, reason string)
2729
}
2830

29-
func NewBuffer[T any](category DataCategory, capacity int, overflowPolicy OverflowPolicy) *Buffer[T] {
31+
func NewBuffer[T any](category ratelimit.Category, capacity int, overflowPolicy OverflowPolicy) *Buffer[T] {
3032
if capacity <= 0 {
3133
capacity = defaultCapacity
3234
}
@@ -188,11 +190,11 @@ func (b *Buffer[T]) Capacity() int {
188190
return b.capacity
189191
}
190192

191-
func (b *Buffer[T]) Category() DataCategory {
193+
func (b *Buffer[T]) Category() ratelimit.Category {
192194
return b.category
193195
}
194196

195-
func (b *Buffer[T]) Priority() Priority {
197+
func (b *Buffer[T]) Priority() ratelimit.Priority {
196198
return b.priority
197199
}
198200

@@ -269,14 +271,14 @@ func (b *Buffer[T]) GetMetrics() BufferMetrics {
269271
}
270272

271273
type BufferMetrics struct {
272-
Category DataCategory `json:"category"`
273-
Priority Priority `json:"priority"`
274-
Capacity int `json:"capacity"`
275-
Size int `json:"size"`
276-
Utilization float64 `json:"utilization"`
277-
OfferedCount int64 `json:"offered_count"`
278-
DroppedCount int64 `json:"dropped_count"`
279-
AcceptedCount int64 `json:"accepted_count"`
280-
DropRate float64 `json:"drop_rate"`
281-
LastUpdated time.Time `json:"last_updated"`
274+
Category ratelimit.Category `json:"category"`
275+
Priority ratelimit.Priority `json:"priority"`
276+
Capacity int `json:"capacity"`
277+
Size int `json:"size"`
278+
Utilization float64 `json:"utilization"`
279+
OfferedCount int64 `json:"offered_count"`
280+
DroppedCount int64 `json:"dropped_count"`
281+
AcceptedCount int64 `json:"accepted_count"`
282+
DropRate float64 `json:"drop_rate"`
283+
LastUpdated time.Time `json:"last_updated"`
282284
}

internal/telemetry/buffer_test.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"sync/atomic"
66
"testing"
77
"time"
8+
9+
"github.com/getsentry/sentry-go/internal/ratelimit"
810
)
911

1012
type testItem struct {
@@ -14,35 +16,35 @@ type testItem struct {
1416

1517
func TestNewBuffer(t *testing.T) {
1618
t.Run("with valid capacity", func(t *testing.T) {
17-
buffer := NewBuffer[*testItem](DataCategoryError, 50, OverflowPolicyDropOldest)
19+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 50, OverflowPolicyDropOldest)
1820
if buffer.Capacity() != 50 {
1921
t.Errorf("Expected capacity 50, got %d", buffer.Capacity())
2022
}
21-
if buffer.Category() != DataCategoryError {
23+
if buffer.Category() != ratelimit.CategoryError {
2224
t.Errorf("Expected category error, got %s", buffer.Category())
2325
}
24-
if buffer.Priority() != PriorityCritical {
26+
if buffer.Priority() != ratelimit.PriorityCritical {
2527
t.Errorf("Expected priority critical, got %s", buffer.Priority())
2628
}
2729
})
2830

2931
t.Run("with zero capacity", func(t *testing.T) {
30-
buffer := NewBuffer[*testItem](DataCategoryLog, 0, OverflowPolicyDropOldest)
32+
buffer := NewBuffer[*testItem](ratelimit.CategoryLog, 0, OverflowPolicyDropOldest)
3133
if buffer.Capacity() != 100 {
3234
t.Errorf("Expected default capacity 100, got %d", buffer.Capacity())
3335
}
3436
})
3537

3638
t.Run("with negative capacity", func(t *testing.T) {
37-
buffer := NewBuffer[*testItem](DataCategoryLog, -10, OverflowPolicyDropOldest)
39+
buffer := NewBuffer[*testItem](ratelimit.CategoryLog, -10, OverflowPolicyDropOldest)
3840
if buffer.Capacity() != 100 {
3941
t.Errorf("Expected default capacity 100, got %d", buffer.Capacity())
4042
}
4143
})
4244
}
4345

4446
func TestBufferBasicOperations(t *testing.T) {
45-
buffer := NewBuffer[*testItem](DataCategoryError, 3, OverflowPolicyDropOldest)
47+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 3, OverflowPolicyDropOldest)
4648

4749
// Test empty buffer
4850
if !buffer.IsEmpty() {
@@ -81,7 +83,7 @@ func TestBufferBasicOperations(t *testing.T) {
8183
}
8284

8385
func TestBufferPollOperation(t *testing.T) {
84-
buffer := NewBuffer[*testItem](DataCategoryError, 3, OverflowPolicyDropOldest)
86+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 3, OverflowPolicyDropOldest)
8587

8688
// Test polling from empty buffer
8789
item, ok := buffer.Poll()
@@ -124,7 +126,7 @@ func TestBufferPollOperation(t *testing.T) {
124126
}
125127

126128
func TestBufferOverflow(t *testing.T) {
127-
buffer := NewBuffer[*testItem](DataCategoryError, 2, OverflowPolicyDropOldest)
129+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 2, OverflowPolicyDropOldest)
128130

129131
// Fill buffer to capacity
130132
item1 := &testItem{id: 1, data: "first"}
@@ -168,7 +170,7 @@ func TestBufferOverflow(t *testing.T) {
168170
}
169171

170172
func TestBufferDrain(t *testing.T) {
171-
buffer := NewBuffer[*testItem](DataCategoryError, 5, OverflowPolicyDropOldest)
173+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 5, OverflowPolicyDropOldest)
172174

173175
// Drain empty buffer
174176
items := buffer.Drain()
@@ -204,7 +206,7 @@ func TestBufferDrain(t *testing.T) {
204206
}
205207

206208
func TestBufferMetrics(t *testing.T) {
207-
buffer := NewBuffer[*testItem](DataCategoryError, 2, OverflowPolicyDropOldest)
209+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 2, OverflowPolicyDropOldest)
208210

209211
// Initial metrics
210212
if buffer.OfferedCount() != 0 {
@@ -228,7 +230,7 @@ func TestBufferMetrics(t *testing.T) {
228230
}
229231

230232
func TestBufferConcurrency(t *testing.T) {
231-
buffer := NewBuffer[*testItem](DataCategoryError, 100, OverflowPolicyDropOldest)
233+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 100, OverflowPolicyDropOldest)
232234

233235
const numGoroutines = 10
234236
const itemsPerGoroutine = 50
@@ -288,13 +290,13 @@ func TestBufferConcurrency(t *testing.T) {
288290

289291
func TestBufferDifferentCategories(t *testing.T) {
290292
testCases := []struct {
291-
category DataCategory
292-
expectedPriority Priority
293+
category ratelimit.Category
294+
expectedPriority ratelimit.Priority
293295
}{
294-
{DataCategoryError, PriorityCritical},
295-
{DataCategoryCheckIn, PriorityHigh},
296-
{DataCategoryLog, PriorityMedium},
297-
{DataCategoryTransaction, PriorityLow},
296+
{ratelimit.CategoryError, ratelimit.PriorityCritical},
297+
{ratelimit.CategoryMonitor, ratelimit.PriorityHigh},
298+
{ratelimit.CategoryLog, ratelimit.PriorityMedium},
299+
{ratelimit.CategoryTransaction, ratelimit.PriorityLow},
298300
}
299301

300302
for _, tc := range testCases {
@@ -315,7 +317,7 @@ func TestBufferStressTest(t *testing.T) {
315317
t.Skip("Skipping stress test in short mode")
316318
}
317319

318-
buffer := NewBuffer[*testItem](DataCategoryError, 1000, OverflowPolicyDropOldest)
320+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 1000, OverflowPolicyDropOldest)
319321

320322
const duration = 100 * time.Millisecond
321323
const numProducers = 5
@@ -392,7 +394,7 @@ func TestBufferStressTest(t *testing.T) {
392394
}
393395

394396
func TestOverflowPolicyDropOldest(t *testing.T) {
395-
buffer := NewBuffer[*testItem](DataCategoryError, 2, OverflowPolicyDropOldest)
397+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 2, OverflowPolicyDropOldest)
396398

397399
// Fill buffer to capacity
398400
item1 := &testItem{id: 1, data: "first"}
@@ -432,7 +434,7 @@ func TestOverflowPolicyDropOldest(t *testing.T) {
432434
}
433435

434436
func TestOverflowPolicyDropNewest(t *testing.T) {
435-
buffer := NewBuffer[*testItem](DataCategoryError, 2, OverflowPolicyDropNewest)
437+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 2, OverflowPolicyDropNewest)
436438

437439
// Fill buffer to capacity
438440
item1 := &testItem{id: 1, data: "first"}
@@ -472,7 +474,7 @@ func TestOverflowPolicyDropNewest(t *testing.T) {
472474
}
473475

474476
func TestBufferDroppedCallback(t *testing.T) {
475-
buffer := NewBuffer[*testItem](DataCategoryError, 2, OverflowPolicyDropOldest)
477+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 2, OverflowPolicyDropOldest)
476478

477479
var droppedItems []*testItem
478480
var dropReasons []string
@@ -510,7 +512,7 @@ func TestBufferDroppedCallback(t *testing.T) {
510512
}
511513

512514
func TestBufferPollBatch(t *testing.T) {
513-
buffer := NewBuffer[*testItem](DataCategoryError, 5, OverflowPolicyDropOldest)
515+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 5, OverflowPolicyDropOldest)
514516

515517
// Add some items
516518
for i := 1; i <= 5; i++ {
@@ -538,7 +540,7 @@ func TestBufferPollBatch(t *testing.T) {
538540
}
539541

540542
func TestBufferPeek(t *testing.T) {
541-
buffer := NewBuffer[*testItem](DataCategoryError, 3, OverflowPolicyDropOldest)
543+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 3, OverflowPolicyDropOldest)
542544

543545
// Test peek on empty buffer
544546
_, ok := buffer.Peek()
@@ -565,11 +567,11 @@ func TestBufferPeek(t *testing.T) {
565567
}
566568

567569
func TestBufferAdvancedMetrics(t *testing.T) {
568-
buffer := NewBuffer[*testItem](DataCategoryError, 2, OverflowPolicyDropOldest)
570+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 2, OverflowPolicyDropOldest)
569571

570572
// Test initial metrics
571573
metrics := buffer.GetMetrics()
572-
if metrics.Category != DataCategoryError {
574+
if metrics.Category != ratelimit.CategoryError {
573575
t.Errorf("Expected category error, got %s", metrics.Category)
574576
}
575577
if metrics.Capacity != 2 {
@@ -609,7 +611,7 @@ func TestBufferAdvancedMetrics(t *testing.T) {
609611
}
610612

611613
func TestBufferClear(t *testing.T) {
612-
buffer := NewBuffer[*testItem](DataCategoryError, 3, OverflowPolicyDropOldest)
614+
buffer := NewBuffer[*testItem](ratelimit.CategoryError, 3, OverflowPolicyDropOldest)
613615

614616
// Add some items
615617
buffer.Offer(&testItem{id: 1, data: "test"})

0 commit comments

Comments
 (0)