chore: import upstream snapshot with attribution
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Profound Analytics - Custom log integration
|
||||
*
|
||||
* Buffers HTTP request logs in memory and flushes them in batches to Profound's API.
|
||||
* Runs in Node.js (proxy.ts on ECS), so module-level state persists across requests.
|
||||
* @see https://docs.tryprofound.com/agent-analytics/custom
|
||||
*/
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { env } from '@/lib/core/config/env'
|
||||
import { isHosted } from '@/lib/core/config/env-flags'
|
||||
import { getClientIp } from '@/lib/core/utils/request'
|
||||
import { getBaseDomain } from '@/lib/core/utils/urls'
|
||||
|
||||
const logger = createLogger('ProfoundAnalytics')
|
||||
|
||||
const FLUSH_INTERVAL_MS = 10_000
|
||||
const MAX_BATCH_SIZE = 500
|
||||
|
||||
interface ProfoundLogEntry {
|
||||
timestamp: string
|
||||
method: string
|
||||
host: string
|
||||
path: string
|
||||
status_code: number
|
||||
ip: string
|
||||
user_agent: string
|
||||
query_params?: Record<string, string>
|
||||
referer?: string
|
||||
}
|
||||
|
||||
let buffer: ProfoundLogEntry[] = []
|
||||
let flushTimer: NodeJS.Timeout | null = null
|
||||
|
||||
/**
|
||||
* Returns true if Profound analytics is configured.
|
||||
*/
|
||||
export function isProfoundEnabled(): boolean {
|
||||
return isHosted && Boolean(env.PROFOUND_API_KEY) && Boolean(env.PROFOUND_ENDPOINT)
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes buffered log entries to Profound's API.
|
||||
*/
|
||||
async function flush(): Promise<void> {
|
||||
if (buffer.length === 0) return
|
||||
|
||||
const apiKey = env.PROFOUND_API_KEY
|
||||
if (!apiKey) {
|
||||
buffer = []
|
||||
return
|
||||
}
|
||||
|
||||
const endpoint = env.PROFOUND_ENDPOINT
|
||||
if (!endpoint) {
|
||||
buffer = []
|
||||
return
|
||||
}
|
||||
const entries = buffer.splice(0, MAX_BATCH_SIZE)
|
||||
|
||||
try {
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-api-key': apiKey,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(entries),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error(`Profound API returned ${response.status}`)
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Failed to flush logs to Profound', error)
|
||||
}
|
||||
}
|
||||
|
||||
function ensureFlushTimer(): void {
|
||||
if (flushTimer) return
|
||||
flushTimer = setInterval(() => {
|
||||
flush().catch(() => {})
|
||||
}, FLUSH_INTERVAL_MS)
|
||||
flushTimer.unref()
|
||||
}
|
||||
|
||||
/**
|
||||
* Queues a request log entry for the next batch flush to Profound.
|
||||
*/
|
||||
export function sendToProfound(request: Request, statusCode: number): void {
|
||||
if (!isProfoundEnabled()) return
|
||||
|
||||
try {
|
||||
const url = new URL(request.url)
|
||||
const queryParams: Record<string, string> = {}
|
||||
url.searchParams.forEach((value, key) => {
|
||||
queryParams[key] = value
|
||||
})
|
||||
|
||||
buffer.push({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: request.method,
|
||||
host: getBaseDomain(),
|
||||
path: url.pathname,
|
||||
status_code: statusCode,
|
||||
ip: (() => {
|
||||
const resolved = getClientIp(request)
|
||||
return resolved === 'unknown' ? '0.0.0.0' : resolved
|
||||
})(),
|
||||
user_agent: request.headers.get('user-agent') || '',
|
||||
...(Object.keys(queryParams).length > 0 && { query_params: queryParams }),
|
||||
...(request.headers.get('referer') && { referer: request.headers.get('referer')! }),
|
||||
})
|
||||
|
||||
ensureFlushTimer()
|
||||
|
||||
if (buffer.length >= MAX_BATCH_SIZE) {
|
||||
flush().catch(() => {})
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Failed to enqueue log entry', error)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user