fix(analytics): filter cookies in Amplitude proxy to prevent request rejections#2476
fix(analytics): filter cookies in Amplitude proxy to prevent request rejections#2476ygrishajev merged 1 commit intomainfrom
Conversation
📝 WalkthroughWalkthroughReplaces Next.js rewrite-based Amplitude proxy with a new API route at Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant NextAPI as Next.js /api/collect
participant Proxy as Memoized Proxy
participant Amplitude
Client->>NextAPI: POST /api/collect (Amplitude payload)
NextAPI->>NextAPI: strip cookies, set req.url = "/2/httpapi"
NextAPI->>Proxy: forward request (changeOrigin, target=Amplitude)
Proxy->>Amplitude: HTTP request to Amplitude /2/httpapi
Amplitude-->>Proxy: response
Proxy-->>NextAPI: proxyRes event
NextAPI-->>Client: proxy response (resolve)
alt Proxy error
Proxy-->>NextAPI: error
NextAPI-->>Client: 502 (if headers not sent)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
Comment |
86a5b21 to
2c75e34
Compare
Codecov Report❌ Patch coverage is
❌ Your patch status has failed because the patch coverage (0.00%) is below the target coverage (50.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #2476 +/- ##
==========================================
- Coverage 50.96% 50.61% -0.36%
==========================================
Files 1062 1053 -9
Lines 29185 28869 -316
Branches 6450 6398 -52
==========================================
- Hits 14875 14612 -263
+ Misses 14079 13963 -116
- Partials 231 294 +63
*This pull request uses carry forward flags. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In @apps/deploy-web/src/pages/api/collect/index.ts:
- Line 37: The current call to services.logger.error({ error, event:
"AMPLITUDE_PROXY_ERROR" }) won't let the logger extract stack/metadata because
it expects an Error instance (or specific key like err); update the call to pass
the Error directly or use the logger's err key: call
services.logger.error(error, { event: "AMPLITUDE_PROXY_ERROR" }) or
services.logger.error({ err: error, event: "AMPLITUDE_PROXY_ERROR" }) (or, if
neither is supported, log a descriptive message and then log the error
separately) so the stack and error details are captured; target the
services.logger.error invocation and the local error variable in the
AMPLITUDE_PROXY_ERROR handling.
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
apps/deploy-web/next.config.jsapps/deploy-web/src/pages/api/collect/index.ts
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}
📄 CodeRabbit inference engine (.cursor/rules/general.mdc)
**/*.{ts,tsx,js}: Never use typeanyor cast to typeany. Always define the proper TypeScript types.
Never use deprecated methods from libraries.
Don't add unnecessary comments to the code.
Files:
apps/deploy-web/src/pages/api/collect/index.tsapps/deploy-web/next.config.js
🧠 Learnings (1)
📚 Learning: 2025-08-12T13:52:38.708Z
Learnt from: stalniy
Repo: akash-network/console PR: 1800
File: apps/deploy-web/next.config.js:163-165
Timestamp: 2025-08-12T13:52:38.708Z
Learning: In the Akash Console project, akashnetwork/env-loader is used at the top of next.config.js files to automatically load environment variables from env/.env files into process.env. SENTRY_ORG and SENTRY_PROJECT are stored as public configuration values in apps/deploy-web/env/.env and are loaded this way, while only SENTRY_AUTH_TOKEN is handled as a GitHub secret in workflows.
Applied to files:
apps/deploy-web/next.config.js
🧬 Code graph analysis (1)
apps/deploy-web/src/pages/api/collect/index.ts (3)
apps/deploy-web/src/services/app-di-container/server-di-container.service.ts (1)
AppServices(63-63)apps/deploy-web/src/lib/nextjs/defineApiHandler/defineApiHandler.ts (1)
defineApiHandler(13-57)packages/logging/src/services/logger/logger.service.ts (1)
error(146-152)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: validate / validate-app
- GitHub Check: test-build
🔇 Additional comments (3)
apps/deploy-web/next.config.js (1)
161-163: LGTM!Clean removal of the Amplitude proxy rewrites configuration. The functionality is now properly handled by the new API route at
/api/collect, which allows for cookie filtering that wasn't possible with Next.js rewrites.apps/deploy-web/src/pages/api/collect/index.ts (2)
4-17: LGTM on proxy initialization.Good use of
ReturnTypefor type inference avoiding explicitany. Memoizing the proxy server is appropriate for performance.
48-53: LGTM!Correct configuration for a proxy route —
externalResolver: trueprevents Next.js warnings about unresolved requests, andbodyParser: falseis necessary to stream the raw request body to the upstream.
…rejections Create custom API route handler for /api/collect that removes all cookies before proxying to Amplitude's API. This prevents Amplitude from rejecting requests due to excessive or irrelevant cookies while maintaining analytics functionality. - Add /api/collect API route that proxies to Amplitude with cookie filtering - Rewrite URL path from /api/collect to /2/httpapi to match Amplitude endpoint - Initialize proxy server once and reuse for better performance - Remove Next.js rewrite rule as API route takes precedence
2c75e34 to
4716602
Compare
Create custom API route handler for /api/collect that removes all cookies before proxying to Amplitude's API. This prevents Amplitude from rejecting requests due to excessive or irrelevant cookies while maintaining analytics functionality.
Summary by CodeRabbit
New Features
Chores
✏️ Tip: You can customize this high-level summary in your review settings.