From 79ebdd721d3fe02458f23fe80274d561ac741af7 Mon Sep 17 00:00:00 2001 From: Adam <13007539+MrgSub@users.noreply.github.com> Date: Sun, 10 Aug 2025 10:24:46 -0700 Subject: [PATCH] Disable OpenTelemetry instrumentation and reduce state cache invalidations (#1975) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # READ CAREFULLY THEN REMOVE Remove bullet points that are not relevant. PLEASE REFRAIN FROM USING AI TO WRITE YOUR CODE AND PR DESCRIPTION. IF YOU DO USE AI TO WRITE YOUR CODE PLEASE PROVIDE A DESCRIPTION AND REVIEW IT CAREFULLY. MAKE SURE YOU UNDERSTAND THE CODE YOU ARE SUBMITTING USING AI. - Pull requests that do not follow these guidelines will be closed without review or comment. - If you use AI to write your PR description your pr will be close without review or comment. - If you are unsure about anything, feel free to ask for clarification. ## Description Please provide a clear description of your changes. --- ## Type of Change Please delete options that are not relevant. - [ ] 🐛 Bug fix (non-breaking change which fixes an issue) - [ ] ✨ New feature (non-breaking change which adds functionality) - [ ] 💥 Breaking change (fix or feature with breaking changes) - [ ] 📝 Documentation update - [ ] 🎨 UI/UX improvement - [ ] 🔒 Security enhancement - [ ] ⚡ Performance improvement ## Areas Affected Please check all that apply: - [ ] Email Integration (Gmail, IMAP, etc.) - [ ] User Interface/Experience - [ ] Authentication/Authorization - [ ] Data Storage/Management - [ ] API Endpoints - [ ] Documentation - [ ] Testing Infrastructure - [ ] Development Workflow - [ ] Deployment/Infrastructure ## Testing Done Describe the tests you've done: - [ ] Unit tests added/updated - [ ] Integration tests added/updated - [ ] Manual testing performed - [ ] Cross-browser testing (if UI changes) - [ ] Mobile responsiveness verified (if UI changes) ## Security Considerations For changes involving data or authentication: - [ ] No sensitive data is exposed - [ ] Authentication checks are in place - [ ] Input validation is implemented - [ ] Rate limiting is considered (if applicable) ## Checklist - [ ] I have read the [CONTRIBUTING](https://github.com/Mail-0/Zero/blob/staging/.github/CONTRIBUTING.md) document - [ ] My code follows the project's style guidelines - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in complex areas - [ ] I have updated the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix/feature works - [ ] All tests pass locally - [ ] Any dependent changes are merged and published ## Additional Notes Add any other context about the pull request here. ## Screenshots/Recordings Add screenshots or recordings here if applicable. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the project's license._ --- ## Summary by cubic Disabled OpenTelemetry instrumentation and reduced unnecessary state cache invalidations to improve server performance. - **Refactors** - Commented out OpenTelemetry setup and usage in the server entrypoint. - Limited calls to invalidate and send state cache in thread and workflow logic. --- apps/server/src/lib/server-utils.ts | 8 ++-- apps/server/src/main.ts | 44 +++++++++---------- .../src/workflows/sync-threads-workflow.ts | 6 +-- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/apps/server/src/lib/server-utils.ts b/apps/server/src/lib/server-utils.ts index e028e075d..b5ee8d7b1 100644 --- a/apps/server/src/lib/server-utils.ts +++ b/apps/server/src/lib/server-utils.ts @@ -305,10 +305,10 @@ export const modifyThreadLabelsInDB = async ( const threadResult = await getThread(connectionId, threadId); const shard = await getShardClient(connectionId, threadResult.shardId); await shard.stub.modifyThreadLabelsInDB(threadId, addLabels, removeLabels); - + const agent = await getZeroSocketAgent(connectionId); await agent.invalidateDoStateCache(); - + await sendDoState(connectionId); }; @@ -402,8 +402,8 @@ export const getThreadsFromDB = async ( }, ): Promise => { // Fire and forget - don't block the thread query on state updates - const agent = await getZeroSocketAgent(connectionId); - await agent.invalidateDoStateCache(); + // const agent = await getZeroSocketAgent(connectionId); + // await agent.invalidateDoStateCache(); void sendDoState(connectionId); const maxResults = params.maxResults ?? defaultPageSize; diff --git a/apps/server/src/main.ts b/apps/server/src/main.ts index 076bb4e64..a9bb9f51d 100644 --- a/apps/server/src/main.ts +++ b/apps/server/src/main.ts @@ -22,7 +22,7 @@ import { } from './lib/attachments'; import { SyncThreadsCoordinatorWorkflow } from './workflows/sync-threads-coordinator-workflow'; import { WorkerEntrypoint, DurableObject, RpcTarget } from 'cloudflare:workers'; -import { instrument, type ResolveConfigFn } from '@microlabs/otel-cf-workers'; +// import { instrument, type ResolveConfigFn } from '@microlabs/otel-cf-workers'; import { getZeroAgent, getZeroDB, verifyToken } from './lib/server-utils'; import { SyncThreadsWorkflow } from './workflows/sync-threads-workflow'; import { ShardRegistry, ZeroAgent, ZeroDriver } from './routes/agent'; @@ -824,32 +824,28 @@ const handler = { }, }; -const config: ResolveConfigFn = (env: ZeroEnv) => { - return { - exporter: { - url: env.OTEL_EXPORTER_OTLP_ENDPOINT || 'https://api.axiom.co/v1/traces', - headers: env.OTEL_EXPORTER_OTLP_HEADERS - ? Object.fromEntries( - env.OTEL_EXPORTER_OTLP_HEADERS.split(',').map((header: string) => { - const [key, value] = header.split('='); - return [key.trim(), value.trim()]; - }), - ) - : {}, - }, - service: { - name: env.OTEL_SERVICE_NAME || 'zero-email-server', - version: '1.0.0', - }, - }; -}; +// const config: ResolveConfigFn = (env: ZeroEnv) => { +// return { +// exporter: { +// url: env.OTEL_EXPORTER_OTLP_ENDPOINT || 'https://api.axiom.co/v1/traces', +// headers: env.OTEL_EXPORTER_OTLP_HEADERS +// ? Object.fromEntries( +// env.OTEL_EXPORTER_OTLP_HEADERS.split(',').map((header: string) => { +// const [key, value] = header.split('='); +// return [key.trim(), value.trim()]; +// }), +// ) +// : {}, +// }, +// service: { +// name: env.OTEL_SERVICE_NAME || 'zero-email-server', +// version: '1.0.0', +// }, +// }; +// }; export default class Entry extends WorkerEntrypoint { async fetch(request: Request): Promise { - const instrumentedHandler = instrument(handler, config); - if (instrumentedHandler && instrumentedHandler.fetch) { - return instrumentedHandler.fetch(request as any, this.env, this.ctx); - } return handler.fetch(request, this.env, this.ctx); } async queue( diff --git a/apps/server/src/workflows/sync-threads-workflow.ts b/apps/server/src/workflows/sync-threads-workflow.ts index 4dd5068db..75f5bd620 100644 --- a/apps/server/src/workflows/sync-threads-workflow.ts +++ b/apps/server/src/workflows/sync-threads-workflow.ts @@ -13,7 +13,7 @@ * * Reuse or distribution of this file requires a license from Zero Email Inc. */ -import { getZeroAgent, connectionToDriver, sendDoState } from '../lib/server-utils'; +import { getZeroAgent, connectionToDriver } from '../lib/server-utils'; import { WorkflowEntrypoint, WorkflowStep } from 'cloudflare:workers'; import type { WorkflowEvent } from 'cloudflare:workers'; import { connection } from '../db/schema'; @@ -179,8 +179,8 @@ export class SyncThreadsWorkflow extends WorkflowEntrypoint