Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(persons): batch group calls (#61291)

Zach Brown02312429e7b4
diff --git a/nodejs/src/ingestion/personhog/client.test.ts b/nodejs/src/ingestion/personhog/client.test.ts
index ef692521e279..297071c81ef1 100644
--- a/nodejs/src/ingestion/personhog/client.test.ts
+++ b/nodejs/src/ingestion/personhog/client.test.ts
@@ -390,6 +390,37 @@ describe('PersonHogClient', () => {
expect(result).toEqual([])
expect(handler).not.toHaveBeenCalled()
})
+
+ it('chunks large batches into multiple gRPC calls', async () => {
+ let callCount = 0
+ const batchSizes: number[] = []
+
+ const client = createMockClient({
+ getGroupsBatch: (req) => {
+ callCount++
+ batchSizes.push(req.keys.length)
+ return {
+ results: req.keys.map((key) => ({
+ key,
+ group: makeProtoGroup({ groupProperties: jsonBytes({ chunked: true }) }),
+ })),
+ }
+ },
+ })
+
+ // 300 items should produce 2 chunks: 250 + 50
+ const count = 300
+ const teamIds = Array.from({ length: count }, (_, i) => i + 1)
+ const groupTypeIndexes = Array.from({ length: count }, () => 0)
+ const groupKeys = Array.from({ length: count }, (_, i) => `key-${i}`)
+
+ const result = await client.groups.fetchGroupsByKeys(teamIds, groupTypeIndexes, groupKeys)
+
+ expect(callCount).toBe(2)
+ expect(batchSizes).toEqual([250, 50])
+ expect(result).toHaveLength(300)
+ expect(result.every((r) => r.group_properties.chunked === true)).toBe(true)
+ })
})
describe('fetchGroupTypesByTeamIds', () => {
diff --git a/nodejs/src/ingestion/personhog/groups.ts b/nodejs/src/ingestion/personhog/groups.ts
index f34a346ec1d4..16df36ff4e3f 100644
--- a/nodejs/src/ingestion/personhog/groups.ts
+++ b/nodejs/src/ingestion/personhog/groups.ts
@@ -13,6 +13,7 @@ import type { Group as ProtoGroup } from '../../generated/personhog/personhog/ty
import { Group as DomainGroup, GroupTypeIndex } from '../../types'
import { epochMsToDateTime, eventualReadOptions, parseJsonBytes } from './client'
+const PERSONHOG_BATCH_SIZE = 250
const VALID_GROUP_TYPE_INDEXES = new Set<number>([0, 1, 2, 3, 4])
function toGroupTypeIndex(value: number): GroupTypeIndex {
@@ -74,20 +75,6 @@ export class PersonHogGroupOperations {
return []
}
- const response = await this.client.getGroupsBatch(
- create(GetGroupsBatchRequestSchema, {
- keys: teamIds.map((teamId, i) =>
- create(GroupKeySchema, {
- teamId: BigInt(teamId),
- groupTypeIndex: groupTypeIndexes[i],
- groupKey: groupKeys[i],
- })
- ),
- readOptions: eventualReadOptions(),
- }),
- callerTag ? { headers: { 'x-caller-tag': callerTag } } : undefined
- )
-
const results: {
team_id: number
group_type_index: GroupTypeIndex
@@ -95,14 +82,34 @@ export class PersonHogGroupOperations {
group_properties: Record<string, any>
}[] = []
- for (const result of response.results) {
- if (result.group && result.key) {
- results.push({
- team_id: Number(result.key.teamId),
- group_type_index: toGroupTypeIndex(result.key.groupTypeIndex),
- group_key: result.key.groupKey,
- group_properties: parseJsonBytes(result.group.groupProperties) ?? {},
- })
+ for (let i = 0; i < teamIds.length; i += PERSONHOG_BATCH_SIZE) {
+ const batchTeamIds = teamIds.slice(i, i + PERSONHOG_BATCH_SIZE)
+ const batchGroupTypeIndexes = groupTypeIndexes.slice(i, i + PERSONHOG_BATCH_SIZE)
+ const batchGroupKeys = groupKeys.slice(i, i + PERSONHOG_BATCH_SIZE)
+
+ const response = await this.client.getGroupsBatch(
+ create(GetGroupsBatchRequestSchema, {
+ keys: batchTeamIds.map((teamId, j) =>
+ create(GroupKeySchema, {
+ teamId: BigInt(teamId),
+ groupTypeIndex: batchGroupTypeIndexes[j],
+ groupKey: batchGroupKeys[j],
+ })
+ ),
+ readOptions: eventualReadOptions(),
+ }),
+ callerTag ? { headers: { 'x-caller-tag': callerTag } } : undefined
+ )
+
+ for (const result of response.results) {
+ if (result.group && result.key) {
+ results.push({
+ team_id: Number(result.key.teamId),
+ group_type_index: toGroupTypeIndex(result.key.groupTypeIndex),
+ group_key: result.key.groupKey,
+ group_properties: parseJsonBytes(result.group.groupProperties) ?? {},
+ })
+ }
}
}