|
1 | | -import { describe, expect, it } from 'vitest' |
| 1 | +import { describe, expect, it, vi } from 'vitest' |
2 | 2 |
|
3 | 3 | const DEFAULT_FILTER_CONFIG = { |
4 | 4 | ignoredRoutes: ['/health', '/health/liveness', '/metrics'], |
@@ -230,4 +230,75 @@ describe('Observability Filtering', () => { |
230 | 230 | ).toBe(false) |
231 | 231 | }) |
232 | 232 | }) |
| 233 | + |
| 234 | + describe('custom tracesSampler hook', () => { |
| 235 | + type SamplerContext = { |
| 236 | + name?: string |
| 237 | + attributes?: Record<string, unknown> |
| 238 | + } |
| 239 | + |
| 240 | + // Replicates the composition logic from provider.ts initializeSentry |
| 241 | + const makeSampler = ( |
| 242 | + ignoredRoutes: string[], |
| 243 | + customSampler?: (ctx: SamplerContext) => number | undefined, |
| 244 | + defaultRate = 1.0, |
| 245 | + ) => { |
| 246 | + return (ctx: SamplerContext): number => { |
| 247 | + const httpTarget = ctx.attributes?.['http.target'] as string | undefined |
| 248 | + const rawRoute = httpTarget || ctx.name || '' |
| 249 | + const route = rawRoute.replace(/\/+$/, '') |
| 250 | + |
| 251 | + for (const ignored of ignoredRoutes) { |
| 252 | + if (route === ignored || route.startsWith(`${ignored}?`)) { |
| 253 | + return 0 |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + if (customSampler) { |
| 258 | + const result = customSampler(ctx) |
| 259 | + if (result !== undefined) return result |
| 260 | + } |
| 261 | + |
| 262 | + return defaultRate |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + it('falls back to sampleRate when no custom sampler provided', () => { |
| 267 | + const sampler = makeSampler(['/health'], undefined, 0.5) |
| 268 | + expect(sampler({ name: '/api/data' })).toBe(0.5) |
| 269 | + }) |
| 270 | + |
| 271 | + it('custom sampler return value overrides sampleRate', () => { |
| 272 | + const custom = (ctx: SamplerContext) => { |
| 273 | + if (ctx.attributes?.['customer.id'] === '84') return 0 |
| 274 | + return undefined |
| 275 | + } |
| 276 | + const sampler = makeSampler([], custom, 0.5) |
| 277 | + expect(sampler({ attributes: { 'customer.id': '84' } })).toBe(0) |
| 278 | + expect(sampler({ attributes: { 'customer.id': '1' } })).toBe(0.5) |
| 279 | + }) |
| 280 | + |
| 281 | + it('custom sampler returning undefined falls back to sampleRate', () => { |
| 282 | + const custom = (_ctx: SamplerContext) => undefined |
| 283 | + const sampler = makeSampler([], custom, 0.75) |
| 284 | + expect(sampler({ name: '/api/data' })).toBe(0.75) |
| 285 | + }) |
| 286 | + |
| 287 | + it('ignoredRoutes check short-circuits before custom sampler is called', () => { |
| 288 | + const custom = vi.fn(() => 1.0 as number | undefined) |
| 289 | + const sampler = makeSampler(['/health'], custom, 0.5) |
| 290 | + expect(sampler({ name: '/health' })).toBe(0) |
| 291 | + expect(custom).not.toHaveBeenCalled() |
| 292 | + }) |
| 293 | + |
| 294 | + it('custom sampler can return fractional sample rates', () => { |
| 295 | + const custom = (ctx: SamplerContext) => { |
| 296 | + if (ctx.attributes?.['tier'] === 'premium') return 1.0 |
| 297 | + return 0.1 |
| 298 | + } |
| 299 | + const sampler = makeSampler([], custom, 0.5) |
| 300 | + expect(sampler({ attributes: { tier: 'premium' } })).toBe(1.0) |
| 301 | + expect(sampler({ attributes: { tier: 'free' } })).toBe(0.1) |
| 302 | + }) |
| 303 | + }) |
233 | 304 | }) |
0 commit comments