Skip to main content

Source: ocean/docs/observability-edge-functions-report.md | ✏️ Edit on GitHub

Edge Functions Observability Integration Report

Overview

This report analyzes the observability integration status of all Edge Functions in the supabase/functions/ directory, focusing on Sentry and PostHog integration.

Summary

Functions with Proper Sentry Integration (10/14)

  1. check-tenant-health

    • Uses initSentry() at startup
    • Uses Logger class for structured logging
    • Properly tracks errors with logger.error()
    • Implements metrics with counters and histograms
    • Has trackOperation wrapper for automatic error capture
  2. GraphQL-v2

    • Uses initSentryWithTracing() for distributed tracing
    • Uses Logger class for structured logging
    • Implements withTracing() wrapper for operations
    • Properly captures exceptions with context
    • Tracks GraphQL operations with spans
    • Note: Has comment "PostHog analytics now handled via CDP integration"
  3. handle-stripe-webhook

    • Uses initSentry() at startup
    • Uses Logger class for structured logging
    • Implements metrics (webhookCounter, webhookDuration)
    • Uses trackOperation for automatic error tracking
    • Properly logs and tracks webhook events
  4. provision-tenant-resources

    • Uses initSentry() at startup
    • Uses Logger class with context
    • Implements tenant provisioning metrics
    • Uses trackOperation wrapper
    • Properly tracks Neon API calls and provisioning status
  5. stripe-billing

    • Uses initSentryWithTracing() for distributed tracing
    • Uses Logger class
    • Implements withTracing() wrapper for the entire function
    • Uses tracedFetch() for external API calls
  6. stripe-portal

    • Uses initSentryWithTracing() for distributed tracing
    • Uses Logger class
    • Implements withTracing() wrapper
    • Uses trackSupabaseOperation() for database operations
  7. stripe-products

    • Uses initSentryWithTracing() for distributed tracing
    • Uses Logger class
    • Implements withTracing() wrapper
  8. stripe-setup-intent

    • Uses initSentryWithTracing() for distributed tracing
    • Uses Logger class
    • Implements withTracing() wrapper
    • Uses trackSupabaseOperation() for database operations
  9. stripe-subscription

    • Uses initSentryWithTracing() for distributed tracing
    • Uses Logger class
    • Implements withTracing() wrapper
    • Uses trackSupabaseOperation() and tracedFetch()
  10. sync-stripe-data

    • Uses initSentryWithTracing() for distributed tracing
    • Uses Logger class
    • Implements withTracing() wrapper
    • Uses trackSupabaseOperation() for database operations
  11. sync-user-to-stripe

    • Uses initSentryWithTracing() for distributed tracing
    • Uses Logger class
    • Implements withTracing() wrapper
    • Uses Sentry.withScope for detailed error context
  12. test-sentry

    • Uses initSentry() at startup
    • Uses Logger class
    • Designed specifically to test Sentry integration

Functions Missing Sentry Integration (2/14)

  1. provision-user-resources

    • No Sentry import or initialization
    • No Logger class usage
    • Uses basic console.log for logging
    • No error tracking with Sentry.captureException
  2. send-slack-alert

    • No Sentry import or initialization
    • No Logger class usage
    • No structured error handling with Sentry

PostHog Integration Status

Current State

  • NO functions have direct PostHog integration
  • Only graphql-v2 has a comment mentioning: "PostHog analytics now handled via CDP integration"
  • This suggests PostHog is handled at a different layer (Customer Data Platform)

Functions That Would Benefit from PostHog Analytics

  1. GraphQL-v2 - Track API usage, query patterns, performance
  2. stripe-subscription - Track subscription events, conversion rates
  3. stripe-billing - Track billing operations, payment success rates
  4. provision-tenant-resources - Track provisioning success/failure rates
  5. check-tenant-health - Track health check patterns, issues by region

Observability Features Used

Sentry Features

  1. Basic Error Tracking: initSentry() - 3 functions
  2. Distributed Tracing: initSentryWithTracing() - 9 functions
  3. Structured Logging: Logger class - 12 functions
  4. Operation Tracking: trackOperation() - 3 functions
  5. Traced Operations: withTracing() - 9 functions
  6. Supabase Operation Tracking: trackSupabaseOperation() - 6 functions
  7. HTTP Request Tracing: tracedFetch() - 2 functions

Metrics Implementation

  • Custom Prometheus-style metrics in _shared/observability.ts
  • Used by: check-tenant-health, handle-stripe-webhook, provision-tenant-resources
  • Metrics endpoint exposed at /metrics path

Recommendations

Immediate Actions

  1. Add Sentry to Missing Functions:

    // provision-user-resources/index.ts
    import { initSentry, Logger } from '../_shared/observability.ts'

    initSentry('provision-user-resources')
    const logger = new Logger({ functionName: 'provision-user-resources' })
    // send-slack-alert/index.ts
    import { initSentry, Logger } from '../_shared/observability.ts'

    initSentry('send-slack-alert')
    const logger = new Logger({ functionName: 'send-slack-alert' })
  2. Replace console.log with logger:

    • provision-user-resources: Replace all console.log with appropriate logger.info/warn/error
    • send-slack-alert: Add structured logging throughout
  3. Add Error Tracking:

    • Wrap main logic in try-catch blocks
    • Use logger.error() which automatically sends to Sentry
    • Add context to errors for better debugging

Future Enhancements

  1. Standardize Tracing Approach:

    • Consider using initSentryWithTracing for all functions that make external API calls
    • Add withTracing wrapper to functions missing it
  2. Enhance Metrics Collection:

    • Add metrics to more functions (especially Stripe-related ones)
    • Create dashboards for monitoring key metrics
  3. PostHog Integration:

    • If CDP integration isn't sufficient, consider adding direct PostHog tracking for:
      • User actions (subscriptions, billing changes)
      • API usage patterns
      • Feature adoption metrics
  4. Correlation IDs:

    • Add request/correlation IDs to track operations across functions
    • Enhance Logger to include correlation IDs automatically

Conclusion

The Edge Functions have good observability coverage with 85% (12/14) having Sentry integration. The two functions missing integration (provision-user-resources and send-slack-alert) should be updated to match the patterns used in other functions. PostHog appears to be handled through a CDP integration rather than direct implementation in Edge Functions.