* Log a 1st-party event for internal analytics (async version). * Events are batched and exported to /api/event_logging/batch * * This enriches the event with core metadata (model, session, env context, etc.) * at log time, similar to logEventToStatsig. * * @param eventName - Name of the event
(
firstPartyEventLogger: Logger,
eventName: string,
metadata: Record<string, number | boolean | undefined> = {},
)
| 155 | * @param metadata - Additional metadata for the event (intentionally no strings, to avoid accidentally logging code/filepaths) |
| 156 | */ |
| 157 | async function logEventTo1PAsync( |
| 158 | firstPartyEventLogger: Logger, |
| 159 | eventName: string, |
| 160 | metadata: Record<string, number | boolean | undefined> = {}, |
| 161 | ): Promise<void> { |
| 162 | try { |
| 163 | // Enrich with core metadata at log time (similar to Statsig pattern) |
| 164 | const coreMetadata = await getEventMetadata({ |
| 165 | model: metadata.model, |
| 166 | betas: metadata.betas, |
| 167 | }) |
| 168 | |
| 169 | // Build attributes - OTel supports nested objects natively via AnyValueMap |
| 170 | // Cast through unknown since our nested objects are structurally compatible |
| 171 | // with AnyValue but TS doesn't recognize it due to missing index signatures |
| 172 | const attributes = { |
| 173 | event_name: eventName, |
| 174 | event_id: randomUUID(), |
| 175 | // Pass objects directly - no JSON serialization needed |
| 176 | core_metadata: coreMetadata, |
| 177 | user_metadata: getCoreUserData(true), |
| 178 | event_metadata: metadata, |
| 179 | } as unknown as AnyValueMap |
| 180 | |
| 181 | // Add user_id if available |
| 182 | const userId = getOrCreateUserID() |
| 183 | if (userId) { |
| 184 | attributes.user_id = userId |
| 185 | } |
| 186 | |
| 187 | // Debug logging when debug mode is enabled |
| 188 | if (isInternalBuild()) { |
| 189 | logForDebugging( |
| 190 | `[NOUMENA-ONLY] 1P event: ${eventName} ${jsonStringify(metadata, null, 0)}`, |
| 191 | ) |
| 192 | } |
| 193 | |
| 194 | // Emit log record |
| 195 | firstPartyEventLogger.emit({ |
| 196 | body: eventName, |
| 197 | attributes, |
| 198 | }) |
| 199 | } catch (e) { |
| 200 | if (process.env.NODE_ENV === 'development') { |
| 201 | throw e |
| 202 | } |
| 203 | if (isInternalBuild()) { |
| 204 | logError(e as Error) |
| 205 | } |
| 206 | // swallow |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Log a 1st-party event for internal analytics. |
no test coverage detected