Skip to content

Commit 219ff84

Browse files
committed
feat: make batch_size_ranges configuration optional by providing hardcoded defaults
Signed-off-by: OneZero-Y <[email protected]>
1 parent ec93fff commit 219ff84

File tree

4 files changed

+117
-28
lines changed

4 files changed

+117
-28
lines changed

config/config.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,6 @@ api:
253253
high_resolution_timing: false # Use nanosecond precision timing
254254
sample_rate: 1.0 # Collect metrics for all requests (1.0 = 100%, 0.5 = 50%)
255255

256-
# Batch size range labels for metrics (used in duration metrics labels)
257-
batch_size_ranges:
258-
- {min: 1, max: 1, label: "1"}
259-
- {min: 2, max: 5, label: "2-5"}
260-
- {min: 6, max: 10, label: "6-10"}
261-
- {min: 11, max: 20, label: "11-20"}
262-
- {min: 21, max: 50, label: "21-50"}
263-
- {min: 51, max: -1, label: "50+"} # -1 means no upper limit
264-
265256
# Histogram buckets for metrics (directly configure what you need)
266257
duration_buckets: [0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30]
267258
size_buckets: [1, 2, 5, 10, 20, 50, 100, 200]

src/semantic-router/pkg/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ type BatchClassificationMetricsConfig struct {
105105
// Sample rate for metrics collection (0.0-1.0, 1.0 means collect all metrics)
106106
SampleRate float64 `yaml:"sample_rate,omitempty"`
107107

108-
// Batch size range labels for metrics
108+
// Batch size range labels for metrics (optional - uses sensible defaults if not specified)
109+
// Default ranges: "1", "2-5", "6-10", "11-20", "21-50", "50+"
109110
BatchSizeRanges []BatchSizeRangeConfig `yaml:"batch_size_ranges,omitempty"`
110111

111112
// Histogram buckets for metrics (directly configured)

src/semantic-router/pkg/metrics/metrics_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,92 @@ func TestMetricsIntegration(t *testing.T) {
166166
ConcurrentGoroutines.WithLabelValues(batchID).Dec()
167167
}
168168
}
169+
170+
// TestDefaultBatchSizeRanges tests that default batch size ranges work when configuration is empty
171+
func TestDefaultBatchSizeRanges(t *testing.T) {
172+
// Save current config
173+
originalConfig := GetBatchMetricsConfig()
174+
175+
// Set config with empty BatchSizeRanges to test defaults
176+
emptyConfig := BatchMetricsConfig{
177+
Enabled: true,
178+
DetailedGoroutineTracking: true,
179+
HighResolutionTiming: false,
180+
SampleRate: 1.0,
181+
DurationBuckets: FallbackDurationBuckets,
182+
SizeBuckets: FallbackSizeBuckets,
183+
BatchSizeRanges: []config.BatchSizeRangeConfig{}, // Empty - should use defaults
184+
}
185+
SetBatchMetricsConfig(emptyConfig)
186+
187+
// Test that default ranges are used
188+
tests := []struct {
189+
name string
190+
size int
191+
expected string
192+
}{
193+
{"Single text (default)", 1, "1"},
194+
{"Small batch (default)", 3, "2-5"},
195+
{"Medium batch (default)", 8, "6-10"},
196+
{"Large batch (default)", 15, "11-20"},
197+
{"Very large batch (default)", 35, "21-50"},
198+
{"Maximum batch (default)", 100, "50+"},
199+
}
200+
201+
for _, tt := range tests {
202+
t.Run(tt.name, func(t *testing.T) {
203+
result := GetBatchSizeRange(tt.size)
204+
if result != tt.expected {
205+
t.Errorf("GetBatchSizeRange(%d) with empty config = %s, want %s", tt.size, result, tt.expected)
206+
}
207+
})
208+
}
209+
210+
// Restore original config
211+
SetBatchMetricsConfig(originalConfig)
212+
}
213+
214+
// TestCustomBatchSizeRanges tests that custom batch size ranges override defaults
215+
func TestCustomBatchSizeRanges(t *testing.T) {
216+
// Save current config
217+
originalConfig := GetBatchMetricsConfig()
218+
219+
// Set config with custom BatchSizeRanges
220+
customConfig := BatchMetricsConfig{
221+
Enabled: true,
222+
DetailedGoroutineTracking: true,
223+
HighResolutionTiming: false,
224+
SampleRate: 1.0,
225+
DurationBuckets: FallbackDurationBuckets,
226+
SizeBuckets: FallbackSizeBuckets,
227+
BatchSizeRanges: []config.BatchSizeRangeConfig{
228+
{Min: 1, Max: 10, Label: "small"},
229+
{Min: 11, Max: 100, Label: "medium"},
230+
{Min: 101, Max: -1, Label: "large"},
231+
},
232+
}
233+
SetBatchMetricsConfig(customConfig)
234+
235+
// Test that custom ranges are used
236+
tests := []struct {
237+
name string
238+
size int
239+
expected string
240+
}{
241+
{"Custom small range", 5, "small"},
242+
{"Custom medium range", 50, "medium"},
243+
{"Custom large range", 200, "large"},
244+
}
245+
246+
for _, tt := range tests {
247+
t.Run(tt.name, func(t *testing.T) {
248+
result := GetBatchSizeRange(tt.size)
249+
if result != tt.expected {
250+
t.Errorf("GetBatchSizeRange(%d) with custom config = %s, want %s", tt.size, result, tt.expected)
251+
}
252+
})
253+
}
254+
255+
// Restore original config
256+
SetBatchMetricsConfig(originalConfig)
257+
}

website/docs/getting-started/configuration.md

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,16 @@ api:
233233
high_resolution_timing: false # Use nanosecond precision timing
234234
sample_rate: 1.0 # Collect metrics for all requests (1.0 = 100%)
235235

236-
# Batch size range labels for metrics
237-
batch_size_ranges:
238-
- {min: 1, max: 1, label: "1"}
239-
- {min: 2, max: 5, label: "2-5"}
240-
- {min: 6, max: 10, label: "6-10"}
241-
- {min: 11, max: 20, label: "11-20"}
242-
- {min: 21, max: 50, label: "21-50"}
243-
- {min: 51, max: -1, label: "50+"} # -1 means no upper limit
236+
# Batch size range labels for metrics (OPTIONAL - uses sensible defaults)
237+
# Default ranges: "1", "2-5", "6-10", "11-20", "21-50", "50+"
238+
# Only specify if you need custom ranges:
239+
# batch_size_ranges:
240+
# - {min: 1, max: 1, label: "1"}
241+
# - {min: 2, max: 5, label: "2-5"}
242+
# - {min: 6, max: 10, label: "6-10"}
243+
# - {min: 11, max: 20, label: "11-20"}
244+
# - {min: 21, max: 50, label: "21-50"}
245+
# - {min: 51, max: -1, label: "50+"} # -1 means no upper limit
244246

245247
# Histogram buckets - choose from presets below or customize
246248
duration_buckets: [0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30]
@@ -282,37 +284,43 @@ pkill -f "router"
282284
make run-router
283285
```
284286

287+
### Default Batch Size Ranges
288+
289+
The system provides sensible default batch size ranges that work well for most use cases:
290+
291+
- **"1"** - Single text requests
292+
- **"2-5"** - Small batch requests
293+
- **"6-10"** - Medium batch requests
294+
- **"11-20"** - Large batch requests
295+
- **"21-50"** - Very large batch requests
296+
- **"50+"** - Maximum batch requests
297+
298+
**You don't need to configure `batch_size_ranges` unless you have specific requirements.** The defaults are automatically used when the configuration is omitted.
299+
285300
### Configuration Examples by Use Case
286301

287302
**Real-time Chat API (fast preset)**
288303
```yaml
289304
# Copy these values to your config for sub-millisecond monitoring
290305
duration_buckets: [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1]
291306
size_buckets: [1, 2, 3, 5, 8, 10]
292-
batch_size_ranges:
293-
- {min: 1, max: 1, label: "1"}
294-
- {min: 2, max: 5, label: "2-5"}
295-
- {min: 6, max: -1, label: "6+"}
307+
# batch_size_ranges: uses defaults (no configuration needed)
296308
```
297309

298310
**E-commerce API (standard preset)**
299311
```yaml
300312
# Copy these values for typical web API response times
301313
duration_buckets: [0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]
302314
size_buckets: [1, 2, 5, 10, 20, 50, 100]
303-
batch_size_ranges:
304-
- {min: 1, max: 1, label: "1"}
305-
- {min: 2, max: 5, label: "2-5"}
306-
- {min: 6, max: 10, label: "6-10"}
307-
- {min: 11, max: 20, label: "11-20"}
308-
- {min: 21, max: -1, label: "21+"}
315+
# batch_size_ranges: uses defaults (no configuration needed)
309316
```
310317

311318
**Data Processing Pipeline (slow preset)**
312319
```yaml
313320
# Copy these values for heavy computation workloads
314321
duration_buckets: [0.1, 0.5, 1, 5, 10, 30, 60, 120]
315322
size_buckets: [10, 50, 100, 500, 1000, 5000]
323+
# Custom batch size ranges for large-scale processing (overrides defaults)
316324
batch_size_ranges:
317325
- {min: 1, max: 50, label: "1-50"}
318326
- {min: 51, max: 200, label: "51-200"}

0 commit comments

Comments
 (0)