(
eventName: string,
properties: { [key: string]: boolean | number | undefined },
)
| 158 | |
| 159 | // NOTE: use via src/services/analytics/index.ts > logEvent |
| 160 | export async function trackDatadogEvent( |
| 161 | eventName: string, |
| 162 | properties: { [key: string]: boolean | number | undefined }, |
| 163 | ): Promise<void> { |
| 164 | if (process.env.NODE_ENV !== 'production') { |
| 165 | return |
| 166 | } |
| 167 | |
| 168 | // Don't send events for 3P providers (Bedrock, Vertex, Foundry) |
| 169 | if (getAPIProvider() !== 'firstParty') { |
| 170 | return |
| 171 | } |
| 172 | |
| 173 | // Fast path: use cached result if available to avoid await overhead |
| 174 | let initialized = datadogInitialized |
| 175 | if (initialized === null) { |
| 176 | initialized = await initializeDatadog() |
| 177 | } |
| 178 | if (!initialized || !DATADOG_ALLOWED_EVENTS.has(eventName)) { |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | try { |
| 183 | const metadata = await getEventMetadata({ |
| 184 | model: properties.model, |
| 185 | betas: properties.betas, |
| 186 | }) |
| 187 | // Destructure to avoid duplicate envContext (once nested, once flattened) |
| 188 | const { envContext, ...restMetadata } = metadata |
| 189 | const allData: Record<string, unknown> = { |
| 190 | ...restMetadata, |
| 191 | ...envContext, |
| 192 | ...properties, |
| 193 | userBucket: getUserBucket(), |
| 194 | } |
| 195 | |
| 196 | // Normalize MCP tool names to "mcp" for cardinality reduction |
| 197 | if ( |
| 198 | typeof allData.toolName === 'string' && |
| 199 | allData.toolName.startsWith('mcp__') |
| 200 | ) { |
| 201 | allData.toolName = 'mcp' |
| 202 | } |
| 203 | |
| 204 | // Normalize model names for cardinality reduction (external users only) |
| 205 | if (process.env.USER_TYPE !== 'ant' && typeof allData.model === 'string') { |
| 206 | const shortName = getCanonicalName(allData.model.replace(/\[1m]$/i, '')) |
| 207 | allData.model = shortName in MODEL_COSTS ? shortName : 'other' |
| 208 | } |
| 209 | |
| 210 | // Truncate dev version to base + date (remove timestamp and sha for cardinality reduction) |
| 211 | // e.g. "2.0.53-dev.20251124.t173302.sha526cc6a" -> "2.0.53-dev.20251124" |
| 212 | if (typeof allData.version === 'string') { |
| 213 | allData.version = allData.version.replace( |
| 214 | /^(\d+\.\d+\.\d+-dev\.\d{8})\.t\d+\.sha[a-f0-9]+$/, |
| 215 | '$1', |
| 216 | ) |
| 217 | } |
no test coverage detected