Skip to content

Commit 4cdc2c4

Browse files
committed
feat(cdp): shorten vercel log drain hashes to 22-char base64
1 parent e3fb5d3 commit 4cdc2c4

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

nodejs/src/cdp/templates/_sources/vercel/__snapshots__/vercel_log_drain.template.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`vercel log drain template should capture all Vercel log properties with snake_case naming 1`] = `
44
[
55
{
6-
"distinct_id": "http_log_a2c746dc51d4d25aa95cd6a2ebb31af1f46ce475194e213adfcf9e2f7be982d0",
6+
"distinct_id": "http_log_osdG3FHU0lqpXNai67Ma8f",
77
"event": "$http_log",
88
"properties": {
99
"$current_url": "https://my-app.vercel.app/api/users?page=1",

nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('vercel log drain template', () => {
2525
expect(response.finished).toEqual(true)
2626
expect(response.capturedPostHogEvents).toHaveLength(1)
2727
expect(response.capturedPostHogEvents[0].event).toEqual('$http_log')
28-
expect(response.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[a-f0-9]{64}$/)
28+
expect(response.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[A-Za-z0-9+/]{22}$/)
2929
expect(response.capturedPostHogEvents[0].properties).toMatchObject({
3030
source: 'lambda',
3131
level: 'info',
@@ -218,7 +218,7 @@ describe('vercel log drain template', () => {
218218
)
219219

220220
expect(response.error).toBeUndefined()
221-
expect(response.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[a-f0-9]{64}$/)
221+
expect(response.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[A-Za-z0-9+/]{22}$/)
222222
})
223223

224224
it('should capture all Vercel log properties with snake_case naming', async () => {
@@ -306,7 +306,7 @@ describe('vercel log drain template', () => {
306306

307307
expect(response.error).toBeUndefined()
308308
expect(response.capturedPostHogEvents).toHaveLength(1)
309-
expect(response.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[a-f0-9]{64}$/)
309+
expect(response.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[A-Za-z0-9+/]{22}$/)
310310
// Proxy fields should be null when proxy is missing
311311
expect(response.capturedPostHogEvents[0].properties.proxy_method).toBeNull()
312312
})
@@ -549,7 +549,7 @@ describe('vercel log drain template', () => {
549549
const id1Repeat = day1Repeat.capturedPostHogEvents[0].distinct_id
550550
const id2 = day2.capturedPostHogEvents[0].distinct_id
551551

552-
expect(id1).toMatch(/^http_log_[a-f0-9]{64}$/)
552+
expect(id1).toMatch(/^http_log_[A-Za-z0-9+/]{22}$/)
553553
expect(id1Repeat).toBe(id1)
554554
expect(id2).not.toBe(id1)
555555
expect(day1.capturedPostHogEvents[0].properties.$distinct_id_strategy).toBe('rotating_salt')
@@ -585,7 +585,7 @@ describe('vercel log drain template', () => {
585585
{ request: createVercelRequest(vercelLogDrain) }
586586
)
587587

588-
expect(day1.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[a-f0-9]{64}$/)
588+
expect(day1.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[A-Za-z0-9+/]{22}$/)
589589
expect(day2.capturedPostHogEvents[0].distinct_id).toBe(day1.capturedPostHogEvents[0].distinct_id)
590590
expect(rotated.capturedPostHogEvents[0].distinct_id).not.toBe(day1.capturedPostHogEvents[0].distinct_id)
591591
expect(day1.capturedPostHogEvents[0].properties.$distinct_id_strategy).toBe('fixed_salt')
@@ -641,7 +641,7 @@ describe('vercel log drain template', () => {
641641
)
642642

643643
expect(response.error).toBeUndefined()
644-
expect(response.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[a-f0-9]{64}$/)
644+
expect(response.capturedPostHogEvents[0].distinct_id).toMatch(/^http_log_[A-Za-z0-9+/]{22}$/)
645645
expect(response.capturedPostHogEvents[0].properties.$distinct_id_strategy).toBe('rotating_salt_fallback')
646646
expect(response.logs.map((l) => l.message)).toContainEqual(
647647
expect.stringContaining('custom_template empty, falling back')

nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,24 @@ let strategy := inputs.distinct_id_strategy ?? 'rotating_salt'
170170
let activeStrategy := strategy
171171
let distinctId := ''
172172
173+
// Hashed strategies emit a 22-char unpadded base64 prefix of sha256, matching
174+
// the visual format of PostHog cookieless distinct IDs (132 bits of entropy —
175+
// far more than needed for collision resistance, and short enough to be readable).
176+
fun shortHash(input) {
177+
return substring(sha256(input, 'base64'), 1, 22)
178+
}
179+
173180
if (strategy == 'rotating_salt') {
174-
distinctId := f'http_log_{sha256Hex(f'{salt}:{day}:{clientIp}:{host}:{userAgent}')}'
181+
distinctId := f'http_log_{shortHash(f'{salt}:{day}:{clientIp}:{host}:{userAgent}')}'
175182
} else if (strategy == 'fixed_salt') {
176-
distinctId := f'http_log_{sha256Hex(f'{salt}:{clientIp}:{host}:{userAgent}')}'
183+
distinctId := f'http_log_{shortHash(f'{salt}:{clientIp}:{host}:{userAgent}')}'
177184
} else if (strategy == 'ip') {
178185
distinctId := f'http_log_{clientIp}'
179186
} else if (strategy == 'custom') {
180187
let customTemplate := inputs.custom_template ?? ''
181188
if (empty(customTemplate)) {
182189
print('vercel log drain: custom_template empty, falling back to rotating_salt')
183-
distinctId := f'http_log_{sha256Hex(f'{salt}:{day}:{clientIp}:{host}:{userAgent}')}'
190+
distinctId := f'http_log_{shortHash(f'{salt}:{day}:{clientIp}:{host}:{userAgent}')}'
184191
activeStrategy := 'rotating_salt_fallback'
185192
} else {
186193
let result := customTemplate
@@ -195,7 +202,7 @@ if (strategy == 'rotating_salt') {
195202
}
196203
} else {
197204
// Unknown strategy value — treat as rotating_salt
198-
distinctId := f'http_log_{sha256Hex(f'{salt}:{day}:{clientIp}:{host}:{userAgent}')}'
205+
distinctId := f'http_log_{shortHash(f'{salt}:{day}:{clientIp}:{host}:{userAgent}')}'
199206
activeStrategy := 'rotating_salt'
200207
}
201208

0 commit comments

Comments
 (0)