Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { vi } from 'vitest'

const SAMPLE_RATE_ENV_KEYS = [
'MULTI_TENANT',
'IS_MULTITENANT',
'TENANT_POOL_CACHE_HIT_LOG_SAMPLE_RATE',
'TENANT_POOL_CACHE_MISS_LOG_SAMPLE_RATE',
] as const

type SampleRateEnvKey = (typeof SAMPLE_RATE_ENV_KEYS)[number]

const originalEnv = new Map<SampleRateEnvKey, string | undefined>()

function setSampleRateEnv(env: Partial<Record<SampleRateEnvKey, string>>) {
for (const key of SAMPLE_RATE_ENV_KEYS) {
delete process.env[key]
}

process.env.MULTI_TENANT = 'true'

for (const [key, value] of Object.entries(env)) {
process.env[key] = value
}
}

describe('config sample rate parsing', () => {
beforeAll(() => {
for (const key of SAMPLE_RATE_ENV_KEYS) {
originalEnv.set(key, process.env[key])
}
})

afterEach(() => {
for (const key of SAMPLE_RATE_ENV_KEYS) {
const value = originalEnv.get(key)

if (value === undefined) {
delete process.env[key]
} else {
process.env[key] = value
}
}

vi.resetModules()
})

test('defaults tenant pool cache log sample rates to zero', async () => {
setSampleRateEnv({})

const { getConfig } = await import('./config')
const config = getConfig({ reload: true })

expect(config.tenantPoolCacheHitLogSampleRate).toBe(0)
expect(config.tenantPoolCacheMissLogSampleRate).toBe(0)
})

test('parses fractional tenant pool cache log sample rates', async () => {
setSampleRateEnv({
TENANT_POOL_CACHE_HIT_LOG_SAMPLE_RATE: '0.25',
TENANT_POOL_CACHE_MISS_LOG_SAMPLE_RATE: '0.75',
})

const { getConfig } = await import('./config')
const config = getConfig({ reload: true })

expect(config.tenantPoolCacheHitLogSampleRate).toBe(0.25)
expect(config.tenantPoolCacheMissLogSampleRate).toBe(0.75)
})

test('clamps tenant pool cache log sample rates to zero and one', async () => {
setSampleRateEnv({
TENANT_POOL_CACHE_HIT_LOG_SAMPLE_RATE: '-0.5',
TENANT_POOL_CACHE_MISS_LOG_SAMPLE_RATE: '1.5',
})

const { getConfig } = await import('./config')
const config = getConfig({ reload: true })

expect(config.tenantPoolCacheHitLogSampleRate).toBe(0)
expect(config.tenantPoolCacheMissLogSampleRate).toBe(1)
})

test('falls back to default tenant pool cache log sample rates for invalid values', async () => {
setSampleRateEnv({
TENANT_POOL_CACHE_HIT_LOG_SAMPLE_RATE: 'nope',
TENANT_POOL_CACHE_MISS_LOG_SAMPLE_RATE: 'Infinity',
})

const { getConfig } = await import('./config')
const config = getConfig({ reload: true })

expect(config.tenantPoolCacheHitLogSampleRate).toBe(0)
expect(config.tenantPoolCacheMissLogSampleRate).toBe(0)
})
})
23 changes: 23 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ type StorageConfigType = {
}
prometheusMetricsEnabled: boolean
prometheusMetricsIncludeTenantId: boolean
tenantPoolCacheHitLogSampleRate: number
tenantPoolCacheMissLogSampleRate: number
otelMetricsEnabled: boolean
otelMetricsTemporality: 'DELTA' | 'CUMULATIVE'
otelMetricsExportIntervalMs: number
Expand Down Expand Up @@ -456,6 +458,14 @@ export function getConfig(options?: { reload?: boolean }): StorageConfigType {
logflareApiKey: getOptionalConfigFromEnv('LOGFLARE_API_KEY'),
logflareSourceToken: getOptionalConfigFromEnv('LOGFLARE_SOURCE_TOKEN'),
logflareBatchSize: parseInt(getOptionalConfigFromEnv('LOGFLARE_BATCH_SIZE') || '200', 10),
tenantPoolCacheHitLogSampleRate: envSampleRate(
getOptionalConfigFromEnv('TENANT_POOL_CACHE_HIT_LOG_SAMPLE_RATE'),
0
),
tenantPoolCacheMissLogSampleRate: envSampleRate(
getOptionalConfigFromEnv('TENANT_POOL_CACHE_MISS_LOG_SAMPLE_RATE'),
0
),
Comment thread
ferhatelmas marked this conversation as resolved.
tracingEnabled: getOptionalConfigFromEnv('TRACING_ENABLED') === 'true',
tracingMode: getOptionalConfigFromEnv('TRACING_MODE') ?? 'basic',
tracingTimeMinDuration: parseFloat(
Expand Down Expand Up @@ -646,3 +656,16 @@ function envNumber(value: string | undefined, defaultValue?: number): number | u
}
return parsed
}

function envSampleRate(value: string | undefined, defaultValue: number): number {
if (!value) {
return defaultValue
}

const parsed = Number(value)
if (!Number.isFinite(parsed)) {
return defaultValue
}

return Math.min(Math.max(parsed, 0), 1)
}
Loading