-
Notifications
You must be signed in to change notification settings - Fork 306
Add Profound Agent Analytics Cloudflare Worker #2787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jalafgani
wants to merge
3
commits into
main
Choose a base branch
from
add-profound-cloudflare-worker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| node_modules | ||
| .wrangler | ||
| .dev.vars |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "name": "log-collector", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "dev": "wrangler dev", | ||
| "deploy": "wrangler deploy", | ||
| "cf-typegen": "wrangler types" | ||
| }, | ||
| "devDependencies": { | ||
| "@cloudflare/workers-types": "^4.20250124.0", | ||
| "typescript": "^5.5.2", | ||
| "wrangler": "^3.101.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| export interface Env { | ||
| PROFOUND_API_URL: string; | ||
| PROFOUND_API_KEY: string; | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { | ||
| const response = await fetch(request); | ||
| const responseClone = response.clone(); | ||
|
|
||
| ctx.waitUntil(handleRequest(request, responseClone, env)); | ||
| return response; | ||
| } | ||
| } satisfies ExportedHandler<Env>; | ||
|
|
||
| async function handleRequest(request: Request, response: Response, env: Env) { | ||
| const requestUrl = new URL(request.url); | ||
|
|
||
| const headerSize = Array.from(response.headers.entries()).reduce( | ||
| (total, [key, value]) => total + key.length + value.length + 4, | ||
| 0 | ||
| ); | ||
|
|
||
| const responseBody = await response.blob(); | ||
| const bodySize = responseBody.size; | ||
| const totalBytesSent = headerSize + bodySize; | ||
|
|
||
| const logData = { | ||
| timestamp: Date.now(), | ||
| host: requestUrl.hostname, | ||
| method: request.method, | ||
| pathname: requestUrl.pathname, | ||
| query_params: Object.fromEntries(requestUrl.searchParams), | ||
| ip: request.headers.get('cf-connecting-ip'), | ||
| userAgent: request.headers.get('user-agent'), | ||
| referer: request.headers.get('referer'), | ||
| bytes: totalBytesSent, | ||
| status: response.status | ||
| }; | ||
|
|
||
| await fetch(env.PROFOUND_API_URL, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-API-Key': env.PROFOUND_API_KEY | ||
| }, | ||
| body: JSON.stringify([logData]) | ||
| }).catch((error) => console.error('Failed to send logs:', error)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ESNext", | ||
| "module": "ESNext", | ||
| "moduleResolution": "Bundler", | ||
| "lib": ["ESNext"], | ||
| "types": ["@cloudflare/workers-types/2023-07-01"], | ||
| "noEmit": true, | ||
| "skipLibCheck": true, | ||
| "strict": true, | ||
| "forceConsistentCasingInFileNames": true | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "$schema": "node_modules/wrangler/config-schema.json", | ||
| "name": "log-collector", | ||
| "main": "src/index.ts", | ||
| "compatibility_date": "2025-01-29", | ||
| "observability": { | ||
| "enabled": true | ||
| }, | ||
| "route": { | ||
| "pattern": "appwrite.io/*", | ||
| "zone_name": "appwrite.io" | ||
| }, | ||
| "vars": { | ||
| "PROFOUND_API_URL": "https://artemis.api.tryprofound.com/v1/logs/cloudflare_worker" | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verify compliance with privacy regulations before logging client IP addresses.
Logging
cf-connecting-ip(client IP address) to a third-party analytics service constitutes processing of personal data under GDPR/CCPA. Ensure:🤖 Prompt for AI Agents