Monitoring and Logging
Effective monitoring and logging are essential for understanding application health, debugging issues, and identifying performance bottlenecks.
Cloudflare Analytics
Cloudflare provides a suite of built-in analytics tools that give us visibility into our applications.
Pages/Workers Analytics
- Location: In the Cloudflare dashboard, under Workers & Pages.
- Key Metrics to Monitor:
- Invocations: The number of times your server-side functions have been run. Spikes could indicate high traffic or a potential issue.
- CPU Time: The amount of time the CPU is active. Look for trends or outliers in the 50th, 90th, and 95th percentiles. High CPU time can indicate inefficient code.
- Errors: The number of unhandled exceptions in your functions. This should be as close to zero as possible.
- Alerts:
- Configure alerts to be notified when certain thresholds are breached (e.g., a sudden spike in errors).
Web Analytics
- Location: In the Cloudflare dashboard, under your Site’s Analytics & Logs tab.
- Key Metrics:
- Requests and Page Views: To understand traffic patterns.
- Bandwidth: To monitor data transfer.
- Cache Hit Ratio: A high cache hit ratio is good, as it means Cloudflare is serving content from its edge network, reducing load on our origin functions.
- Security: The number of threats blocked by the WAF.
Logging
By default, you can view real-time logs from your Cloudflare Workers and Pages functions directly in the dashboard.
- Location: From your Pages project, go to the deployment you want to inspect, and you can view live logs.
console.log(): Anything youconsole.log()in your server-side code (e.g., Next.js API routes) will appear in these logs.
Limitations of Default Logging
- Cloudflare’s default logging is real-time and not stored for long periods.
- It can be difficult to search and analyze large volumes of logs.
Third-Party Logging (Log Drains)
For more robust and persistent logging, we integrate with a third-party logging provider (e.g., Datadog, Logflare, Sentry).
- How it Works: We configure a “Log Drain” from Cloudflare to send our logs to the third-party service.
- Setup:
- Set up an account with the chosen logging provider.
- In the Cloudflare dashboard for your Site, go to Analytics & Logs > Logpush.
- Create a new Logpush job, select the dataset (e.g., Workers Trace Events), and configure the destination to be your logging provider’s endpoint.
- Benefits:
- Long-term storage: Logs are stored for days, weeks, or months.
- Advanced Search: Powerful query languages to search and filter logs.
- Alerting: Create alerts based on log patterns (e.g., alert if the string “FATAL” appears more than 10 times in a minute).
- Dashboards: Create visualizations and dashboards to monitor application health from logs.
Error Tracking
For capturing and tracking application errors in detail, we use a dedicated error tracking service like Sentry.
- Integration: The Sentry SDK is added to our Next.js application.
- Benefits:
- Rich Error Details: Sentry captures the stack trace, request details, user context, and more for every error.
- Grouping and Triaging: Sentry groups similar errors together, making it easy to see which errors are most common.
- Alerting: Sentry can send alerts to Slack or email when a new or high-priority error occurs.
By combining Cloudflare’s built-in analytics with third-party logging and error tracking services, we can maintain a comprehensive view of our application’s health and performance.