Skip to content

Conversation

@pablohashescobar
Copy link
Member

@pablohashescobar pablohashescobar commented Oct 3, 2025

Description

  • Added a new function save_webhook_log to log webhook requests and responses to MongoDB, with a fallback to the database if the MongoDB save fails.
  • Updated the webhook_send_task to utilize the new logging function.
  • Modified the get_webhook_logs_queryset to order logs by creation date and adjusted the chunk size for iteration.

Type of Change

  • Improvement (change that would cause existing functionality to not work as expected)

Test Scenarios

  • validate the webhook logs are getting saved in the mongo database

References

WEB-5071

Summary by CodeRabbit

  • New Features
    • Centralized, resilient webhook logging with persistent storage and fallback to improve delivery traceability.
  • Bug Fixes
    • Deterministic ordering of webhook log processing to avoid inconsistent sequences.
  • Chores
    • Fixed smaller batch size for webhook log processing for more predictable, stable background handling.

…7887)

- Added a new function `save_webhook_log` to log webhook requests and responses to MongoDB, with a fallback to the database if the MongoDB save fails.
- Updated the `webhook_send_task` to utilize the new logging function.
- Modified the `get_webhook_logs_queryset` to order logs by creation date and adjusted the chunk size for iteration.
Copilot AI review requested due to automatic review settings October 3, 2025 06:22
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 3, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds save_webhook_log to persist webhook request/response logs to MongoDB with a DB fallback, updates webhook_send_task to use it, and changes webhook log cleanup to order by created_at and use fixed batch size 100 instead of 500.

Changes

Cohort / File(s) Summary
Webhook logging refactor
apps/api/plane/bgtasks/webhook_task.py
Added save_webhook_log which builds a log payload, attempts MongoDB insert via MongoConnection, and falls back to WebhookLog DB creation; replaced direct WebhookLog.objects.create calls in webhook_send_task; imported MongoConnection.
Cleanup batching/order tweak
apps/api/plane/bgtasks/cleanup_task.py
get_webhook_logs_queryset now explicitly orders by created_at and uses a fixed batch size of 100 (was 500) via a two-step iterator chain to ensure deterministic ordering.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant WS as webhook_send_task
  participant SL as save_webhook_log
  participant MG as MongoConnection.collection
  participant DB as WebhookLog (SQL DB)

  WS->>SL: call save_webhook_log(webhook, req/resp, retry_count, event_type)
  Note over SL: assemble log payload (webhook, request, response, retry, event)

  alt Mongo connection & collection present
    SL->>MG: insert_one(payload)
    alt insert succeeds
      MG-->>SL: success
      SL-->>WS: return (logged)
    else insert fails
      MG--x SL: error
      SL->>DB: create(payload)
      DB-->>SL: ack/error
      SL-->>WS: return (logged or failed)
    end
  else Mongo not available
    SL->>DB: create(payload)
    DB-->>SL: ack/error
    SL-->>WS: return (logged or failed)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

Hop hop, logs now hop to Mongo first,
If that burrow’s blocked, SQL takes the burst.
Batches smaller, ordered by date,
Retries counted, saved as fate.
A rabbit nods — tidy and straight. 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly references the implementation of webhook logging to MongoDB with a database fallback and includes the related ticket number, concisely summarizing the primary change of the pull request.
Description Check ✅ Passed The description aligns with the repository template by including a detailed Description of the added logging function and iteration changes, marking the Type of Change as Improvement, listing Test Scenarios for MongoDB verification, and providing a References section linking the related ticket.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-webhook-log-saves

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e63fbfb and b1242c3.

📒 Files selected for processing (1)
  • apps/api/plane/bgtasks/webhook_task.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/api/plane/bgtasks/webhook_task.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Build and lint web apps
  • GitHub Check: Analyze (javascript)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@makeplane
Copy link

makeplane bot commented Oct 3, 2025

Linked to Plane Work Item(s)

This comment was auto-generated by Plane

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements webhook logging to MongoDB with database fallback functionality. The changes introduce a new save_webhook_log function that attempts to save webhook logs to MongoDB first, and falls back to the existing database if MongoDB operations fail.

  • Extracted webhook logging logic into a reusable save_webhook_log function with MongoDB primary storage and database fallback
  • Updated webhook task to use the new logging function instead of direct database operations
  • Modified webhook logs queryset to include ordering and adjusted chunk size

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
apps/api/plane/bgtasks/webhook_task.py Added save_webhook_log function with MongoDB/database fallback and updated webhook task to use new logging
apps/api/plane/bgtasks/cleanup_task.py Updated webhook logs queryset with ordering and modified chunk size

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
apps/api/plane/bgtasks/cleanup_task.py (1)

413-414: Consider using a constant for the chunk size.

The explicit ordering by created_at is beneficial for deterministic processing and aligns well with the new centralized logging approach.

However, the hardcoded chunk size of 100 deviates from the pattern used in other queryset functions (which use BATCH_SIZE). If this smaller chunk size is intentional for webhook logs (e.g., due to larger payloads), consider introducing a dedicated constant like WEBHOOK_BATCH_SIZE for maintainability.

Apply this diff if you'd like to introduce a dedicated constant:

+WEBHOOK_BATCH_SIZE = 100
+
 def get_webhook_logs_queryset():
     """Get email logs older than cutoff days."""
     cutoff_days = int(os.environ.get("HARD_DELETE_AFTER_DAYS", 30))
     cutoff_time = timezone.now() - timedelta(days=cutoff_days)
     logger.info(f"Webhook logs cutoff time: {cutoff_time}")
 
     return (
         WebhookLog.all_objects.filter(created_at__lte=cutoff_time)
         .values(
             "id",
             "created_at",
             "workspace_id",
             "webhook",
             "event_type",
             # Request
             "request_method",
             "request_headers",
             "request_body",
             # Response
             "response_status",
             "response_body",
             "response_headers",
             "retry_count",
         )
         .order_by("created_at")
-        .iterator(chunk_size=100)
+        .iterator(chunk_size=WEBHOOK_BATCH_SIZE)
     )
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 794271a and dcb02ea.

📒 Files selected for processing (2)
  • apps/api/plane/bgtasks/cleanup_task.py (1 hunks)
  • apps/api/plane/bgtasks/webhook_task.py (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
apps/api/plane/bgtasks/webhook_task.py (2)
apps/api/plane/settings/mongo.py (2)
  • MongoConnection (18-122)
  • get_collection (91-109)
apps/api/plane/db/models/webhook.py (2)
  • Webhook (30-57)
  • WebhookLog (60-86)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Lint API
  • GitHub Check: Cursor Bugbot
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (1)
apps/api/plane/bgtasks/webhook_task.py (1)

51-51: LGTM!

The import is necessary for MongoDB integration in the new logging function.

@cursor
Copy link

cursor bot commented Oct 7, 2025

You have run out of free Bugbot PR reviews for this billing cycle. This will reset on October 20.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@dheeru0198 dheeru0198 requested a review from Copilot October 7, 2025 11:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@sriramveeraghanta sriramveeraghanta merged commit 1a9ebc8 into preview Oct 15, 2025
6 of 7 checks passed
@sriramveeraghanta sriramveeraghanta deleted the fix-webhook-log-saves branch October 15, 2025 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants