forked from PostHog/bigquery-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
279 lines (251 loc) · 9.92 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { createBuffer } from '@posthog/plugin-contrib'
import { Plugin, PluginMeta, PluginEvent, RetryError } from '@posthog/plugin-scaffold'
import { BigQuery, Table, TableField, TableMetadata } from '@google-cloud/bigquery'
type BigQueryPlugin = Plugin<{
global: {
bigQueryClient: BigQuery
bigQueryTable: Table
bigQueryTableFields: TableField[]
exportEventsBuffer: ReturnType<typeof createBuffer>
exportEventsToIgnore: Set<string>
exportEventsWithRetry: (payload: UploadJobPayload, meta: PluginMeta<BigQueryPlugin>) => Promise<void>
}
config: {
datasetId: string
tableId: string
exportEventsBufferBytes: string
exportEventsBufferSeconds: string
exportEventsToIgnore: string
exportElementsOnAnyEvent: 'Yes' | 'No'
}
jobs: {
exportEventsWithRetry: UploadJobPayload
}
}>
interface UploadJobPayload {
batch: PluginEvent[]
batchId: number
retriesPerformedSoFar: number
}
export const setupPlugin: BigQueryPlugin['setupPlugin'] = async (meta) => {
const { global, attachments, config } = meta
if (!attachments.googleCloudKeyJson) {
throw new Error('JSON config not provided!')
}
if (!config.datasetId) {
throw new Error('Dataset ID not provided!')
}
if (!config.tableId) {
throw new Error('Table ID not provided!')
}
const credentials = JSON.parse(attachments.googleCloudKeyJson.contents.toString())
global.bigQueryClient = new BigQuery({
projectId: credentials['project_id'],
credentials,
autoRetry: false,
})
global.bigQueryTable = global.bigQueryClient.dataset(config.datasetId).table(config.tableId)
global.bigQueryTableFields = [
{ name: 'uuid', type: 'STRING' },
{ name: 'event', type: 'STRING' },
{ name: 'properties', type: 'STRING' },
{ name: 'elements', type: 'STRING' },
{ name: 'set', type: 'STRING' },
{ name: 'set_once', type: 'STRING' },
{ name: 'distinct_id', type: 'STRING' },
{ name: 'team_id', type: 'INT64' },
{ name: 'ip', type: 'STRING' },
{ name: 'site_url', type: 'STRING' },
{ name: 'timestamp', type: 'TIMESTAMP' },
{ name: 'bq_ingested_timestamp', type: 'TIMESTAMP' },
]
try {
const [metadata]: TableMetadata[] = await global.bigQueryTable.getMetadata()
if (!metadata.schema || !metadata.schema.fields) {
throw new Error('Can not get metadata for table. Please check if the table schema is defined.')
}
const existingFields = metadata.schema.fields
const fieldsToAdd = global.bigQueryTableFields.filter(
({ name }) => !existingFields.find((f: any) => f.name === name)
)
if (fieldsToAdd.length > 0) {
console.info(
`Incomplete schema on BigQuery table! Adding the following fields to reach parity: ${JSON.stringify(
fieldsToAdd
)}`
)
let result: TableMetadata
try {
metadata.schema.fields = metadata.schema.fields.concat(fieldsToAdd)
;[result] = await global.bigQueryTable.setMetadata(metadata)
} catch (error) {
const fieldsToStillAdd = global.bigQueryTableFields.filter(
({ name }) => !result.schema?.fields?.find((f: any) => f.name === name)
)
if (fieldsToStillAdd.length > 0) {
throw new Error(
`Tried adding fields ${JSON.stringify(fieldsToAdd)}, but ${JSON.stringify(
fieldsToStillAdd
)} still to add. Can not start plugin.`
)
}
}
}
} catch (error) {
// some other error? abort!
if (!error.message.includes('Not found')) {
throw new Error(error)
}
console.log(`Creating BigQuery Table - ${config.datasetId}:${config.tableId}`)
try {
await global.bigQueryClient
.dataset(config.datasetId)
.createTable(config.tableId, { schema: global.bigQueryTableFields })
} catch (error) {
// a different worker already created the table
if (!error.message.includes('Already Exists')) {
throw error
}
}
}
setupBufferExportCode(meta, exportEventsToBigQuery)
}
export async function exportEventsToBigQuery(events: PluginEvent[], { global, config }: PluginMeta<BigQueryPlugin>) {
const insertOptions = {
createInsertId: false,
partialRetries: 0,
raw: true,
}
if (!global.bigQueryTable) {
throw new Error('No BigQuery client initialized!')
}
try {
const rows = events.map((event) => {
const {
event: eventName,
properties,
$set,
$set_once,
distinct_id,
team_id,
site_url,
now,
sent_at,
uuid,
..._discard
} = event
const ip = properties?.['$ip'] || event.ip
const timestamp = event.timestamp || properties?.timestamp || now || sent_at
let ingestedProperties = properties
let elements = []
const shouldExportElementsForEvent =
eventName === '$autocapture' || config.exportElementsOnAnyEvent === 'Yes'
if (
shouldExportElementsForEvent &&
properties &&
'$elements' in properties &&
Array.isArray(properties['$elements'])
) {
const { $elements, ...props } = properties
ingestedProperties = props
elements = $elements
}
const object: { json: Record<string, any>; insertId?: string } = {
json: {
uuid,
event: eventName,
properties: JSON.stringify(ingestedProperties || {}),
elements: JSON.stringify(elements || {}),
set: JSON.stringify($set || {}),
set_once: JSON.stringify($set_once || {}),
distinct_id,
team_id,
ip,
site_url,
timestamp: timestamp,
bq_ingested_timestamp: new Date().toISOString(),
},
}
return object
})
const start = Date.now()
await global.bigQueryTable.insert(rows, insertOptions)
const end = Date.now() - start
console.log(
`Inserted ${events.length} ${events.length > 1 ? 'events' : 'event'} to BigQuery. Took ${
end / 1000
} seconds.`
)
} catch (error) {
console.error(
`Error inserting ${events.length} ${events.length > 1 ? 'events' : 'event'} into BigQuery: `,
error
)
throw new RetryError(`Error inserting into BigQuery! ${JSON.stringify(error.errors)}`)
}
}
// What follows is code that should be abstracted away into the plugin server itself.
const setupBufferExportCode = (
meta: PluginMeta<BigQueryPlugin>,
exportEvents: (events: PluginEvent[], meta: PluginMeta<BigQueryPlugin>) => Promise<void>
) => {
const uploadBytes = Math.max(
1024 * 1024,
Math.min(parseInt(meta.config.exportEventsBufferBytes) || 1024 * 1024, 1024 * 1024 * 10)
)
const uploadSeconds = Math.max(1, Math.min(parseInt(meta.config.exportEventsBufferSeconds) || 30, 600))
meta.global.exportEventsToIgnore = new Set(
meta.config.exportEventsToIgnore
? meta.config.exportEventsToIgnore.split(',').map((event) => event.trim())
: null
)
meta.global.exportEventsBuffer = createBuffer({
limit: uploadBytes,
timeoutSeconds: uploadSeconds,
onFlush: async (batch) => {
const jobPayload = {
batch,
batchId: Math.floor(Math.random() * 1000000),
retriesPerformedSoFar: 0,
}
const firstThroughQueue = false // TODO: might make sense sometimes? e.g. when we are processing too many tasks already?
if (firstThroughQueue) {
await meta.jobs.exportEventsWithRetry(jobPayload).runNow()
} else {
await meta.global.exportEventsWithRetry(jobPayload, meta)
}
},
})
meta.global.exportEventsWithRetry = async (payload: UploadJobPayload, meta: PluginMeta<BigQueryPlugin>) => {
const { jobs } = meta
try {
await exportEvents(payload.batch, meta)
} catch (err) {
if (err instanceof RetryError) {
if (payload.retriesPerformedSoFar < 15) {
const nextRetrySeconds = 2 ** payload.retriesPerformedSoFar * 3
console.log(`Enqueued batch ${payload.batchId} for retry in ${Math.round(nextRetrySeconds)}s`)
await jobs
.exportEventsWithRetry({ ...payload, retriesPerformedSoFar: payload.retriesPerformedSoFar + 1 })
.runIn(nextRetrySeconds, 'seconds')
} else {
console.log(
`Dropped batch ${payload.batchId} after retrying ${payload.retriesPerformedSoFar} times`
)
}
} else {
throw err
}
}
}
}
export const jobs: BigQueryPlugin['jobs'] = {
exportEventsWithRetry: async (payload, meta) => {
await meta.global.exportEventsWithRetry(payload, meta)
},
}
export const onEvent: BigQueryPlugin['onEvent'] = (event, { global }) => {
if (!global.exportEventsToIgnore.has(event.event)) {
global.exportEventsBuffer.add(event, JSON.stringify(event).length)
}
}