{
  "license": "CC BY 4.0",
  "recipe": {
    "slug": "database-connection-timeouts-churn",
    "title": "Measure Database Connection Timeouts and Churn",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-28"
  },
  "contract": {
    "tableName": "database_connection_events",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "Connection lifecycle event time."
      },
      {
        "name": "service",
        "type": "Utf8",
        "description": "Application service."
      },
      {
        "name": "region",
        "type": "Utf8",
        "description": "Deployment region."
      },
      {
        "name": "event_name",
        "type": "Utf8",
        "description": "opened, closed, acquisition_timeout, or connection_error."
      },
      {
        "name": "request_id",
        "type": "Utf8",
        "description": "Safe affected request identifier."
      },
      {
        "name": "acquisition_ms",
        "type": "Float64",
        "description": "Pool acquisition duration when available."
      }
    ]
  },
  "query": "SELECT\n  service,\n  region,\n  SUM(CASE WHEN event_name = 'opened' THEN 1 ELSE 0 END) AS opened,\n  SUM(CASE WHEN event_name = 'closed' THEN 1 ELSE 0 END) AS closed,\n  SUM(CASE WHEN event_name = 'acquisition_timeout' THEN 1 ELSE 0 END)\n    AS acquisition_timeouts,\n  SUM(CASE WHEN event_name = 'connection_error' THEN 1 ELSE 0 END)\n    AS connection_errors,\n  COUNT(DISTINCT CASE\n    WHEN event_name IN ('acquisition_timeout', 'connection_error') THEN request_id\n  END) AS affected_requests\nFROM database_connection_events\nWHERE timestamp_utc >= now() - INTERVAL '24 hours'\nGROUP BY service, region\nORDER BY acquisition_timeouts DESC, connection_errors DESC;",
  "fixtureKind": "schema-example",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "service": "service_example_1",
      "region": "region_example_1",
      "event_name": "workflow_completed",
      "request_id": "request_id_example_1",
      "acquisition_ms": 12
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "service": "service_example_2",
      "region": "region_example_2",
      "event_name": "workflow_completed",
      "request_id": "request_id_example_2",
      "acquisition_ms": 37
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "service": "service_example_3",
      "region": "region_example_3",
      "event_name": "workflow_completed",
      "request_id": "request_id_example_3",
      "acquisition_ms": 24
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "service": "service_example_1",
      "region": "region_example_1",
      "event_name": "workflow_completed",
      "request_id": "request_id_example_1",
      "acquisition_ms": 12
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "service": "service_example_2",
      "region": "region_example_2",
      "event_name": "workflow_completed",
      "request_id": "request_id_example_2",
      "acquisition_ms": 37
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "service": "service_example_3",
      "region": "region_example_3",
      "event_name": "workflow_completed",
      "request_id": "request_id_example_3",
      "acquisition_ms": 24
    }
  ],
  "expectedOutput": {
    "columns": [
      "service",
      "region",
      "opened",
      "closed",
      "acquisition_timeouts",
      "connection_errors",
      "affected_requests"
    ],
    "rows": [
      {
        "service": "checkout-api",
        "region": "us-east",
        "opened": 1840,
        "closed": 1792,
        "acquisition_timeouts": 91,
        "connection_errors": 18,
        "affected_requests": 104
      },
      {
        "service": "query-api",
        "region": "eu-west",
        "opened": 620,
        "closed": 614,
        "acquisition_timeouts": 12,
        "connection_errors": 4,
        "affected_requests": 16
      }
    ]
  },
  "notes": [
    "The compact input rows demonstrate field names and JSON types; they are not claimed to generate the displayed aggregate by themselves.",
    "The expected output is deterministic synthetic review data for the documented output shape.",
    "Adapt time windows, thresholds, identities, and business definitions before operational use."
  ]
}