Skip to content

Commit e407ddc

Browse files
committed
fix(analytics): guard sendToProfound with try-catch and align check with isProfoundEnabled
1 parent a902d60 commit e407ddc

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

apps/sim/lib/analytics/profound.ts

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,36 @@ function ensureFlushTimer(): void {
8585
* Queues a request log entry for the next batch flush to Profound.
8686
*/
8787
export function sendToProfound(request: Request, statusCode: number): void {
88-
if (!isHosted || !env.PROFOUND_API_KEY) return
89-
90-
const url = new URL(request.url)
91-
const queryParams: Record<string, string> = {}
92-
url.searchParams.forEach((value, key) => {
93-
queryParams[key] = value
94-
})
95-
96-
buffer.push({
97-
timestamp: new Date().toISOString(),
98-
method: request.method,
99-
host: url.hostname,
100-
path: url.pathname,
101-
status_code: statusCode,
102-
ip:
103-
request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() ||
104-
request.headers.get('x-real-ip') ||
105-
'0.0.0.0',
106-
user_agent: request.headers.get('user-agent') || '',
107-
...(Object.keys(queryParams).length > 0 && { query_params: queryParams }),
108-
...(request.headers.get('referer') && { referer: request.headers.get('referer')! }),
109-
})
110-
111-
ensureFlushTimer()
112-
113-
if (buffer.length >= MAX_BATCH_SIZE) {
114-
flush().catch(() => {})
88+
if (!isProfoundEnabled()) return
89+
90+
try {
91+
const url = new URL(request.url)
92+
const queryParams: Record<string, string> = {}
93+
url.searchParams.forEach((value, key) => {
94+
queryParams[key] = value
95+
})
96+
97+
buffer.push({
98+
timestamp: new Date().toISOString(),
99+
method: request.method,
100+
host: url.hostname,
101+
path: url.pathname,
102+
status_code: statusCode,
103+
ip:
104+
request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() ||
105+
request.headers.get('x-real-ip') ||
106+
'0.0.0.0',
107+
user_agent: request.headers.get('user-agent') || '',
108+
...(Object.keys(queryParams).length > 0 && { query_params: queryParams }),
109+
...(request.headers.get('referer') && { referer: request.headers.get('referer')! }),
110+
})
111+
112+
ensureFlushTimer()
113+
114+
if (buffer.length >= MAX_BATCH_SIZE) {
115+
flush().catch(() => {})
116+
}
117+
} catch (error) {
118+
logger.error('Failed to enqueue log entry', error)
115119
}
116120
}

0 commit comments

Comments
 (0)