Files
Zero/apps/server/package.json
devin-ai-integration[bot] 51bc0ed868 feat: add Grafana monitoring for ZeroAgent, ZeroDriver, and tRPC performance (#1816)
# feat: add Grafana monitoring for ZeroAgent, ZeroDriver, and tRPC performance

## Summary

Implements centralized Grafana monitoring for the Zero server to track performance metrics across tRPC API calls, ZeroDriver operations, and email handling. The implementation uses Prometheus-format metrics that can be scraped by Grafana Cloud for observability and performance analysis.

**Key Components:**
- **Centralized metrics utility** (`src/lib/metrics.ts`) with Prometheus counters and histograms
- **tRPC middleware** for automatic API call timing and success/error tracking  
- **ZeroDriver instrumentation** for email operations (markAsRead, create)
- **Metrics endpoint** (`/metrics`) for Prometheus scraping
- **Environment-based configuration** for Grafana Cloud integration

**Metrics Tracked:**
- `trpc_request_duration_seconds` - API call timing by procedure and status
- `trpc_requests_total` - API call counts by procedure and status  
- `zero_driver_operation_duration_seconds` - Email operation timing
- `email_operations_total` - Email operation counts by provider and status
- `zero_agent_operation_duration_seconds` - Agent operation timing (framework ready)

## Review & Testing Checklist for Human

- [ ] **Test actual metric collection** - Perform real email operations (read, compose, sync) and verify metrics are collected at `/metrics` endpoint
- [ ] **Performance verification** - Monitor response times during normal usage to ensure no degradation from monitoring overhead
- [ ] **Environment configuration** - Set up `GRAFANA_ENDPOINT`, `GRAFANA_USERNAME`, `GRAFANA_PASSWORD` in staging/production environments
- [ ] **Staging deployment test** - Deploy to staging and verify Cloudflare Workers compatibility (removed `collectDefaultMetrics()` due to Node.js API incompatibility)
- [ ] **Complete instrumentation** - Consider adding monitoring to remaining ZeroAgent operations and additional ZeroDriver methods as needed

---

### Diagram

```mermaid
%%{ init : { "theme" : "default" }}%%
graph TD
    subgraph "Core Server"
        A["src/main.ts<br/>+metrics endpoint"]:::major-edit
        B["src/trpc/trpc.ts<br/>+middleware"]:::major-edit
    end
    
    subgraph "Monitoring Infrastructure"
        C["src/lib/metrics.ts<br/>NEW centralized utility"]:::major-edit
        D["src/overrides.d.ts<br/>+TypeScript types"]:::minor-edit
    end
    
    subgraph "Email Operations"
        E["src/routes/agent/index.ts<br/>+ZeroDriver monitoring"]:::major-edit
    end
    
    subgraph "Configuration"
        F["wrangler.jsonc<br/>+environment vars"]:::minor-edit
        G["package.json<br/>+prom-client"]:::minor-edit
    end
    
    A --> C
    B --> C
    E --> C
    C --> F
    D --> F
    
    subgraph Legend
        L1[Major Edit]:::major-edit
        L2[Minor Edit]:::minor-edit
        L3[Context/No Edit]:::context
    end

classDef major-edit fill:#90EE90
classDef minor-edit fill:#87CEEB  
classDef context fill:#FFFFFF
```

### Notes

- **Cloudflare Workers Compatibility**: Removed `collectDefaultMetrics()` due to `process.cpuUsage` incompatibility - this is expected and handled
- **Async Design**: Metrics collection is non-blocking to minimize performance impact
- **Partial Implementation**: Currently instruments key ZeroDriver operations; additional operations can be added incrementally
- **Environment Variables**: All Grafana configuration is externalized for security and flexibility

**Link to Devin run**: https://app.devin.ai/sessions/f88e66bd577843aba5f0d452d4dfe0e8  
**Requested by**: @MrgSub

![Metrics Endpoint Screenshot](file:///home/ubuntu/screenshots/localhost_8787_064620.png)
    
<!-- This is an auto-generated description by cubic. -->
---

## Summary by cubic
Added Grafana monitoring for ZeroAgent, ZeroDriver, and tRPC performance by exposing Prometheus-format metrics and instrumenting key operations.

- **New Features**
  - Centralized metrics utility for tracking API and email operation performance.
  - tRPC middleware and ZeroDriver methods now record timing and status metrics.
  - `/metrics` endpoint added for Prometheus scraping.
  - Environment variables support Grafana Cloud integration.

<!-- End of auto-generated description by cubic. -->
2025-07-25 10:56:50 -07:00

99 lines
2.8 KiB
JSON

{
"name": "@zero/server",
"type": "module",
"private": true,
"scripts": {
"dev": "wrangler dev --show-interactive-dev-session=false --experimental-vectorize-bind-to-prod --env local",
"deploy": "wrangler deploy",
"types": "wrangler types --env local",
"lint": "eslint .",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:push": "drizzle-kit push",
"db:studio": "drizzle-kit studio",
"eval": "evalite",
"eval:dev": "evalite watch"
},
"exports": {
"./trpc": "./src/trpc/index.ts",
"./auth": "./src/lib/auth.ts",
"./auth-providers": "./src/lib/auth-providers.ts",
"./schemas": "./src/lib/schemas.ts"
},
"dependencies": {
"@ai-sdk/anthropic": "1.2.12",
"@ai-sdk/google": "^1.2.18",
"@ai-sdk/groq": "1.2.9",
"@ai-sdk/openai": "^1.3.21",
"@ai-sdk/perplexity": "1.1.9",
"@ai-sdk/ui-utils": "1.2.11",
"@arcadeai/arcadejs": "1.8.1",
"@coinbase/cookie-manager": "1.1.8",
"@googleapis/gmail": "12.0.0",
"@googleapis/people": "3.0.9",
"@hono/trpc-server": "^0.3.4",
"@microsoft/microsoft-graph-client": "^3.0.7",
"@microsoft/microsoft-graph-types": "^2.40.0",
"@modelcontextprotocol/sdk": "1.15.1",
"@react-email/components": "^0.0.41",
"@react-email/render": "1.1.0",
"@trpc/client": "catalog:",
"@trpc/server": "catalog:",
"@tsndr/cloudflare-worker-jwt": "3.2.0",
"@upstash/ratelimit": "^2.0.5",
"@upstash/redis": "^1.34.9",
"agents": "0.0.106",
"ai": "^4.3.13",
"autumn-js": "catalog:",
"base64-js": "1.5.1",
"better-auth": "catalog:",
"cheerio": "1.1.0",
"date-fns": "^4.1.0",
"dedent": "^1.6.0",
"drizzle-orm": "catalog:",
"effect": "3.16.12",
"elevenlabs": "1.59.0",
"email-addresses": "^5.0.0",
"google-auth-library": "9.15.1",
"he": "^1.2.0",
"hono": "^4.7.8",
"hono-agents": "0.0.83",
"hono-party": "^0.0.12",
"jose": "6.0.11",
"jsonrepair": "^3.12.0",
"mime-types": "3.0.1",
"mimetext": "^3.0.27",
"p-retry": "6.2.1",
"partyserver": "^0.0.71",
"postgres": "3.4.5",
"prom-client": "15.1.3",
"react": "catalog:",
"remeda": "2.21.3",
"resend": "^4.5.1",
"sanitize-html": "^2.16.0",
"string-strip-html": "^13.4.12",
"superjson": "catalog:",
"twilio": "5.7.0",
"uuid": "11.1.0",
"wrangler": "catalog:",
"zod": "catalog:"
},
"devDependencies": {
"@types/he": "1.2.3",
"@types/node": "^22.9.0",
"@types/react": "19.1.6",
"@types/sanitize-html": "2.13.0",
"@types/uuid": "10.0.0",
"@zero/eslint-config": "workspace:*",
"@zero/tsconfig": "workspace:*",
"autoevals": "0.0.130",
"drizzle-kit": "catalog:",
"eslint": "^9.27.0",
"evalite": "0.11.4",
"jiti": "2.4.2",
"typescript": "catalog:",
"vite": "^6.3.5",
"vitest": "3.2.4"
}
}