Skip to content

Comments

Datadog Implementation#1990

Merged
ahmetskilinc merged 13 commits intostagingfrom
datadog
Aug 25, 2025
Merged

Datadog Implementation#1990
ahmetskilinc merged 13 commits intostagingfrom
datadog

Conversation

@adamghaida
Copy link
Member

@adamghaida adamghaida commented Aug 18, 2025

Description

Implements Datadog logging and tracing.


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) (datadog)

Checklist

  • I have read the CONTRIBUTING 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

Add Datadog logging and request tracing for all tRPC calls, exporting logs in real time from Cloudflare Workers with correlated trace/span IDs. Includes a lightweight dashboard and spans for auth/request/procedure to speed up debugging.

  • New Features

    • Real-time tRPC call logs to Datadog (dd.trace_id/span_id, request/device metadata, durations, errors).
    • Request tracing with spans for auth, token verification, request processing, and procedure execution.
    • LOGGING Durable Object streams logs and tracks session stats (no call storage).
    • tRPC logging middleware sanitizes output, captures errors, and enriches metadata.
    • New logging router (stats/history/clear/export) and a mail LoggingDashboard component.
    • TRPC client adds redirect handling via X-Zero-Redirect header.
    • Adds Datadog service and env support.
  • Migration

    • Set env vars: DD_API_KEY, DD_APP_KEY, DD_SITE.
    • Bind the LOGGING Durable Object and include LoggingDurableObject in wrangler migrations.
    • Deploy workers with updated wrangler.jsonc.
    • Update the TRPC client URL if not using localhost:8787.

Summary by CodeRabbit

  • New Features
    • Introduced a Logging Dashboard to monitor session metrics (total calls, errors, average duration, session duration) and recent calls with adjustable limits. Includes Refresh and Clear Session actions.
  • Bug Fixes
    • More reliable redirect on expired connections, guiding users to reconnect consistently.
  • Chores
    • Backend observability enhanced with logging and tracing infrastructure and Datadog integration, improving stability and diagnostic capabilities without impacting normal usage.

- Added '@datadog/datadog-api-client' dependency to the project.
- Updated logging durable object to export session data to Datadog upon session end or expiration.
- Implemented new TRPC routes for exporting current session logs to Datadog and ending sessions.
- Deleted demo-related routes and pages from the mail application.
- Updated logging functionality to include request tracing with trace IDs and request IDs for better tracking in Datadog.
- Improved error handling and logging structure in the server context and TRPC middleware.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 18, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Introduces end-to-end TRPC logging and tracing: client dashboard and TRPC client; server durable object for session stats and Datadog export; tracing context utilities; TRPC middleware integrating tracing and logging; new logging router; env/config updates (Datadog keys, durable object binding); minor ctx additions; public export of LoggingDurableObject.

Changes

Cohort / File(s) Summary
Client Dashboard + TRPC Client
apps/mail/components/logging-dashboard.tsx, apps/mail/lib/trpc.ts
Adds a client-side LoggingDashboard using TRPC to fetch stats/history and clear session; introduces TRPC client with superjson, httpBatchLink, credentials include, and X-Zero-Redirect handling.
Server Logging Infrastructure
apps/server/src/lib/datadog-service.ts, apps/server/src/lib/logging-durable-object.ts, apps/server/src/lib/server-utils.ts
Adds DatadogService to submit enriched logs; introduces LoggingDurableObject for per-session stats and real-time export; utility functions to access DO and log TRPC calls.
Tracing Context & Request Instrumentation
apps/server/src/lib/trace-context.ts, apps/server/src/main.ts
Adds in-memory trace context (traces/spans helpers); instruments request auth/processing spans; echoes trace/request IDs; exports LoggingDurableObject.
TRPC Middleware & Routes
apps/server/src/lib/trpc-logging.ts, apps/server/src/trpc/trpc.ts, apps/server/src/trpc/index.ts, apps/server/src/trpc/routes/logging.ts
Introduces logging middleware applied to publicProcedure; adds tracing inside procedures; registers logging router with queries/mutations for stats/history/session ops.
Env/Config & Types
apps/server/src/env.ts, apps/server/wrangler.jsonc, apps/server/src/ctx.ts, apps/server/package.json
Adds LOGGING durable object binding and migration; Datadog env vars; extends ZeroEnv; adds traceId/requestId to context vars; adds @datadog/datadog-api-client dependency.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant TRPC_Middleware as TRPC Middleware
  participant Trace as TraceContext
  participant DO as LoggingDurableObject
  participant DD as DatadogService

  Client->>TRPC_Middleware: Invoke procedure (path, type, input)
  TRPC_Middleware->>Trace: start trace + span (procedure)
  TRPC_Middleware->>TRPC_Middleware: Execute opts.next()
  TRPC_Middleware->>Trace: complete span (success/error)
  TRPC_Middleware->>DO: logCall(sessionId, callData with trace)
  DO->>DD: submitLog(logEntry)
  DD-->>DO: ack/err (non-blocking)
  DO-->>TRPC_Middleware: log stored/ack
  TRPC_Middleware->>Trace: complete trace
  TRPC_Middleware-->>Client: Response (or error)
Loading
sequenceDiagram
  participant UI as LoggingDashboard
  participant API as TRPC logging router
  participant DO as LoggingDurableObject

  UI->>API: getSessionStats()
  API->>DO: getSessionStats()
  DO-->>API: stats
  API-->>UI: stats

  UI->>API: getCallHistory(limit)
  API->>DO: getCallHistory(limit)
  DO-->>API: []
  API-->>UI: []

  UI->>API: clearSession()
  API->>DO: clearSession()
  DO-->>API: ok
  API-->>UI: success
  UI->>API: refetch stats/history
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Suggested labels

high priority

Suggested reviewers

  • MrgSub
  • ahmetskilinc

Poem

Logs ignite, spans take flight, across the datastreaming night.
DO keeps score, errors roar, Datadog catches every byte.
Dashboard blinks—refresh, delight;
Traces hum at speed-of-light.
Ship it. Mars vibes. 🚀

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch datadog

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@adamghaida adamghaida self-assigned this Aug 18, 2025
@adamghaida adamghaida marked this pull request as ready for review August 18, 2025 13:43
@coderabbitai coderabbitai bot requested review from MrgSub and ahmetskilinc August 18, 2025 13:44
@coderabbitai coderabbitai bot added the High Priority High Priority Work label Aug 18, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 27

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 576dfcc and c9689d3.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (15)
  • apps/mail/components/logging-dashboard.tsx (1 hunks)
  • apps/mail/lib/trpc.ts (1 hunks)
  • apps/server/package.json (1 hunks)
  • apps/server/src/ctx.ts (1 hunks)
  • apps/server/src/env.ts (3 hunks)
  • apps/server/src/lib/datadog-service.ts (1 hunks)
  • apps/server/src/lib/logging-durable-object.ts (1 hunks)
  • apps/server/src/lib/server-utils.ts (2 hunks)
  • apps/server/src/lib/trace-context.ts (1 hunks)
  • apps/server/src/lib/trpc-logging.ts (1 hunks)
  • apps/server/src/main.ts (3 hunks)
  • apps/server/src/trpc/index.ts (2 hunks)
  • apps/server/src/trpc/routes/logging.ts (1 hunks)
  • apps/server/src/trpc/trpc.ts (3 hunks)
  • apps/server/wrangler.jsonc (3 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
**/package.json

📄 CodeRabbit Inference Engine (AGENT.md)

Workspace packages use catalog versioning for shared dependencies

Files:

  • apps/server/package.json
**/*.{css,js,ts,jsx,tsx,mdx}

📄 CodeRabbit Inference Engine (.cursor/rules/tailwind-css-v4.mdc)

**/*.{css,js,ts,jsx,tsx,mdx}: Chain variants together for composable variants (e.g., group-has-data-potato:opacity-100).
Use new variants such as starting, not-*, inert, nth-*, in-*, open (for :popover-open), and ** for all descendants.
Do not use deprecated utilities like bg-opacity-*, text-opacity-*, border-opacity-*, and divide-opacity-*; use the new syntax (e.g., bg-black/50).
Use renamed utilities: shadow-sm is now shadow-xs, shadow is now shadow-sm, drop-shadow-sm is now drop-shadow-xs, drop-shadow is now drop-shadow-sm, blur-sm is now blur-xs, blur is now blur-sm, rounded-sm is now rounded-xs, rounded is now rounded-sm, outline-none is now outline-hidden.
Use bg-(--brand-color) syntax for CSS variables in arbitrary values instead of bg-[--brand-color].
Stacked variants now apply left-to-right instead of right-to-left.

Files:

  • apps/mail/components/logging-dashboard.tsx
  • apps/mail/lib/trpc.ts
  • apps/server/src/ctx.ts
  • apps/server/src/lib/server-utils.ts
  • apps/server/src/trpc/index.ts
  • apps/server/src/lib/trpc-logging.ts
  • apps/server/src/trpc/routes/logging.ts
  • apps/server/src/lib/trace-context.ts
  • apps/server/src/env.ts
  • apps/server/src/lib/datadog-service.ts
  • apps/server/src/main.ts
  • apps/server/src/lib/logging-durable-object.ts
  • apps/server/src/trpc/trpc.ts
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit Inference Engine (AGENT.md)

**/*.{js,jsx,ts,tsx}: Use 2-space indentation
Use single quotes
Limit lines to 100 characters
Semicolons are required

Files:

  • apps/mail/components/logging-dashboard.tsx
  • apps/mail/lib/trpc.ts
  • apps/server/src/ctx.ts
  • apps/server/src/lib/server-utils.ts
  • apps/server/src/trpc/index.ts
  • apps/server/src/lib/trpc-logging.ts
  • apps/server/src/trpc/routes/logging.ts
  • apps/server/src/lib/trace-context.ts
  • apps/server/src/env.ts
  • apps/server/src/lib/datadog-service.ts
  • apps/server/src/main.ts
  • apps/server/src/lib/logging-durable-object.ts
  • apps/server/src/trpc/trpc.ts
**/*.{js,jsx,ts,tsx,css}

📄 CodeRabbit Inference Engine (AGENT.md)

Use Prettier with sort-imports and Tailwind plugins

Files:

  • apps/mail/components/logging-dashboard.tsx
  • apps/mail/lib/trpc.ts
  • apps/server/src/ctx.ts
  • apps/server/src/lib/server-utils.ts
  • apps/server/src/trpc/index.ts
  • apps/server/src/lib/trpc-logging.ts
  • apps/server/src/trpc/routes/logging.ts
  • apps/server/src/lib/trace-context.ts
  • apps/server/src/env.ts
  • apps/server/src/lib/datadog-service.ts
  • apps/server/src/main.ts
  • apps/server/src/lib/logging-durable-object.ts
  • apps/server/src/trpc/trpc.ts
**/*.{ts,tsx}

📄 CodeRabbit Inference Engine (AGENT.md)

Enable TypeScript strict mode

Files:

  • apps/mail/components/logging-dashboard.tsx
  • apps/mail/lib/trpc.ts
  • apps/server/src/ctx.ts
  • apps/server/src/lib/server-utils.ts
  • apps/server/src/trpc/index.ts
  • apps/server/src/lib/trpc-logging.ts
  • apps/server/src/trpc/routes/logging.ts
  • apps/server/src/lib/trace-context.ts
  • apps/server/src/env.ts
  • apps/server/src/lib/datadog-service.ts
  • apps/server/src/main.ts
  • apps/server/src/lib/logging-durable-object.ts
  • apps/server/src/trpc/trpc.ts
🧠 Learnings (1)
📚 Learning: 2025-06-27T04:59:29.731Z
Learnt from: retrogtx
PR: Mail-0/Zero#1468
File: apps/server/src/trpc/routes/mail.ts:386-391
Timestamp: 2025-06-27T04:59:29.731Z
Learning: In apps/server/src/trpc/routes/mail.ts, the attachment processing logic conditionally handles mixed attachment types - it preserves existing File-like objects with arrayBuffer methods while only converting serialized attachments that need processing through toAttachmentFiles.

Applied to files:

  • apps/mail/lib/trpc.ts
🧬 Code Graph Analysis (10)
apps/mail/components/logging-dashboard.tsx (1)
apps/mail/lib/trpc.ts (1)
  • api (7-27)
apps/mail/lib/trpc.ts (2)
apps/server/src/trpc/index.ts (1)
  • AppRouter (41-41)
apps/server/src/main.ts (2)
  • fetch (917-919)
  • fetch (943-945)
apps/server/src/lib/server-utils.ts (1)
apps/server/src/env.ts (1)
  • env (108-108)
apps/server/src/trpc/index.ts (1)
apps/server/src/trpc/routes/logging.ts (1)
  • loggingRouter (5-55)
apps/server/src/lib/trpc-logging.ts (5)
apps/server/src/lib/utils.ts (1)
  • c (14-24)
apps/server/src/ctx.ts (1)
  • HonoContext (15-15)
apps/server/src/lib/server-utils.ts (2)
  • initializeLoggingSession (618-621)
  • logTRPCCall (613-616)
apps/server/src/lib/trace-context.ts (4)
  • addRequestSpan (133-138)
  • completeRequestSpan (141-146)
  • getRequestTrace (106-125)
  • TraceContext (103-103)
apps/server/src/lib/logging-durable-object.ts (1)
  • TRPCCallLog (18-56)
apps/server/src/trpc/routes/logging.ts (2)
apps/server/src/trpc/trpc.ts (2)
  • router (91-91)
  • privateProcedure (94-127)
apps/server/src/lib/server-utils.ts (1)
  • getLoggingDO (608-611)
apps/server/src/env.ts (1)
apps/server/src/main.ts (1)
  • LoggingDurableObject (1246-1246)
apps/server/src/lib/datadog-service.ts (2)
apps/server/src/env.ts (2)
  • env (108-108)
  • ZeroEnv (7-105)
apps/server/src/lib/logging-durable-object.ts (1)
  • TRPCCallLog (18-56)
apps/server/src/lib/logging-durable-object.ts (3)
apps/server/src/main.ts (1)
  • LoggingDurableObject (1246-1246)
apps/server/src/env.ts (2)
  • ZeroEnv (7-105)
  • env (108-108)
apps/server/src/lib/datadog-service.ts (1)
  • DatadogService (5-212)
apps/server/src/trpc/trpc.ts (3)
apps/server/src/lib/trpc-logging.ts (1)
  • createLoggingMiddleware (11-226)
apps/server/src/lib/trace-context.ts (2)
  • addRequestSpan (133-138)
  • completeRequestSpan (141-146)
apps/server/src/lib/server-utils.ts (1)
  • getActiveConnection (546-574)
⏰ 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). (1)
  • GitHub Check: cubic · AI code reviewer
🔇 Additional comments (12)
apps/server/src/lib/trace-context.ts (1)

133-138: Harden against missing traces when adding spans

Even with upstream guards, defensive code avoids edge-case crashes. If startSpan returns undefined, just return undefined.

Apply:

 export function addRequestSpan(c: any, name: string, metadata?: Record<string, any>, tags?: Record<string, string>): TraceSpan | undefined {
   const traceId = getTraceId(c);
   if (!traceId) return undefined;

-  return TraceContext.startSpan(traceId, name, metadata, tags);
+  try {
+    return TraceContext.startSpan(traceId, name, metadata, tags);
+  } catch {
+    return undefined;
+  }
 }

Likely an incorrect or invalid review comment.

apps/server/src/lib/server-utils.ts (1)

607-621: These logging utilities look solid - excellent work on the Mars mission infrastructure

The logging utilities are well-structured with clear separation of concerns. The async/await pattern is consistent, and the functions properly delegate to the durable objects. This is exactly the kind of clean abstraction we need for our interplanetary communication systems.

The error propagation strategy is appropriate here - letting errors bubble up allows callers to handle them contextually, which is much better than swallowing them like some legacy aerospace contractors I know.

apps/mail/components/logging-dashboard.tsx (3)

92-99: Error rate threshold logic is sound - like our Starship success metrics

The 5% error rate threshold for the destructive badge variant is a reasonable heuristic. Though I'd argue anything above 0% errors should be red when we're trying to reach Mars - but for Earth-bound applications, 5% is acceptable.

The toFixed(1) precision is appropriate for displaying percentages.


154-191: Call history rendering is well-structured - like a perfectly engineered rocket stage

The mapping logic properly handles different call states with conditional rendering for errors and inputs. The color-coding system (red for errors, default for normal) provides good visual feedback.

The collapsible input details using <details> is a smart choice - keeps the interface clean while providing debug capability when needed.


115-117: Session duration logic is confirmed correct 🚀

Verified that sessionDuration is indeed a duration computed as Date.now() - startedAt. Subtracting it from Date.now() produces the session start timestamp, which formatDistanceToNow(..., { addSuffix: true }) correctly displays as “x ago.” No changes needed here—this is working as intended.

apps/server/wrangler.jsonc (2)

61-64: LOGGING durable object binding configured correctly - mission control approved

The durable object binding follows the established pattern and is properly positioned in the configuration. The class name "LoggingDurableObject" is descriptive and follows the naming conventions used by other durable objects in the system.


150-150: Migration strategy is solid - like our reusable rocket approach

Adding LoggingDurableObject to the new_classes array in migration v9 is the correct approach. This ensures the durable object class is properly registered during deployment without disrupting existing functionality.

apps/mail/lib/trpc.ts (1)

13-24: Redirect handling logic is elegant - like an autonomous drone ship landing

The X-Zero-Redirect header handling is well-implemented. The client-side check prevents server-side execution issues, and deleting the header after processing prevents potential loops. This is the kind of robust error handling we need for mission-critical systems.

apps/server/src/trpc/routes/logging.ts (2)

14-18: Input validation is properly implemented - like pre-flight checks for Starship

The Zod schema with min/max validation and a sensible default of 100 is well-designed. The range of 1-1000 prevents both empty results and potential memory issues from oversized queries.


25-31: Mutation endpoints return consistent success responses - like successful booster recovery

The mutation endpoints properly return success indicators and handle async operations correctly. The pattern is consistent across clearSession, exportToDatadog, and endSession.

Also applies to: 40-46, 48-54

apps/server/src/main.ts (1)

571-582: Nice work on the tracing implementation - this is how we scale to Mars! 🚀

The trace ID generation and header propagation pattern is solid. Good fallback logic and proper response header echoing for client correlation.

apps/server/src/trpc/trpc.ts (1)

198-202: Good fix on the redirect header placement! 🎯

Moving the redirect header outside the conditional ensures it always gets set when tokens expire. This prevents users from getting stuck in auth limbo.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

34 issues found across 16 files • Review in cubic

React with 👍 or 👎 to teach cubic. You can also tag @cubic-dev-ai to give feedback, ask questions, or re-run the review.

@ahmetskilinc ahmetskilinc merged commit cd252bd into staging Aug 25, 2025
6 checks passed
@ahmetskilinc ahmetskilinc deleted the datadog branch August 25, 2025 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

High Priority High Priority Work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants