Skip to main content

Monitoring & Observability

The monitoring utilities provide comprehensive observability features specifically designed for edge environments, including metrics collection, structured logging, distributed tracing, and health checks.

Features

  • Metrics Collection: Performance metrics, custom counters, histograms
  • Structured Logging: JSON-formatted logs with redaction and filtering
  • Distributed Tracing: Request tracing across edge functions and services
  • Health Checks: Automated health monitoring and reporting
  • Performance Monitoring: Response times, throughput, error rates
  • Log Aggregation: Centralized log collection and analysis
  • Edge Optimized: Low-overhead monitoring for edge environments
  • Real-time Dashboards: Live metrics and alerting

Quick Start

MetricsCollector

Comprehensive metrics collection with support for counters, gauges, histograms, and summaries.

Constructor Options

Counter Metrics

Gauge Metrics

Histogram Metrics

Summary Metrics

Custom Metrics

StructuredLogger

JSON-formatted logging with redaction, filtering, and multiple output destinations.

Constructor Options

Logging Methods

Context and Correlation

Log Redaction

Log Filtering

TracingManager

Distributed tracing with span creation, context propagation, and sampling.

Constructor Options

Span Creation

Context Propagation

Sampling Strategies

HealthCheckManager

Automated health monitoring with configurable checks and reporting.

Constructor Options

Adding Health Checks

Health Check Execution

Health Endpoints

Middleware Integration

Complete Monitoring Middleware

Advanced Examples

Real-time Metrics Dashboard

Distributed Tracing Across Services

Health Check with Dependencies

Performance Considerations

Metrics Collection Overhead

Log Performance

Tracing Overhead

Platform-Specific Notes

Cloudflare Workers

  • Use Cloudflare Analytics Engine for metrics
  • Compatible with Cloudflare Logs for structured logging
  • Leverage Cloudflare’s distributed tracing

Vercel Edge Functions

  • Compatible with Vercel’s analytics
  • Use Vercel Log Drain for centralized logging
  • Support for Vercel’s performance monitoring

Deno Deploy

  • Native performance with Deno runtime
  • Compatible with Deno KV for metrics storage
  • Support for Web APIs for monitoring

Best Practices

Metrics Naming

Logging Levels

Tracing Guidelines

Testing

Run monitoring tests with:

API Reference

MetricsCollector Methods

  • increment(name, value, labels) - Increment counter
  • gauge(name, value, labels) - Set gauge value
  • histogram(name, value, labels) - Record histogram observation
  • summary(name, value, labels) - Record summary observation
  • getCounter(name, labels) - Get counter value
  • getGauge(name, labels) - Get gauge value
  • getHistogram(name, labels) - Get histogram statistics
  • getSummary(name, labels) - Get summary quantiles

StructuredLogger Methods

  • error(message, meta, error) - Log error message
  • warn(message, meta) - Log warning message
  • info(message, meta) - Log info message
  • debug(message, meta) - Log debug message
  • child(meta) - Create child logger with additional context

TracingManager Methods

  • startSpan(name, options) - Start new span
  • endSpan(spanId) - End span
  • addEvent(spanId, name, attributes) - Add event to span
  • setAttributes(spanId, attributes) - Set span attributes
  • extract(headers) - Extract trace context
  • inject(spanId, headers) - Inject trace context

HealthCheckManager Methods

  • addCheck(name, checkFn) - Add health check
  • runChecks() - Run all health checks
  • runCheck(name) - Run specific health check
  • getOverallHealth() - Get overall health status

Contributing

When contributing to monitoring utilities:
  1. Maintain observability best practices
  2. Add comprehensive monitoring tests
  3. Update documentation for monitoring features
  4. Consider performance impact of monitoring
  5. Test across all supported platforms

License

MIT