Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return primary response and request body fix #1197

Merged
merged 3 commits into from
Sep 16, 2024

Conversation

nour-borgi
Copy link
Contributor

@nour-borgi nour-borgi commented Jun 27, 2023

Before: the response body of the primary route only will be sent to the next route.
Now: returning the response and request body of the primary route to the next route.

Summary by CodeRabbit

  • New Features

    • Enhanced request and response logging capabilities within the middleware.
    • Introduced new properties for better tracking: resDetails for response details and reqBody for request data.
  • Bug Fixes

    • Improved handling of Buffer types for request and response bodies, ensuring accurate logging and processing.

@codecov
Copy link

codecov bot commented Jun 27, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.77%. Comparing base (e686ddb) to head (76892af).
Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1197      +/-   ##
==========================================
- Coverage   86.78%   86.77%   -0.02%     
==========================================
  Files          90       90              
  Lines        6040     6042       +2     
==========================================
+ Hits         5242     5243       +1     
- Misses        798      799       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rcrichton
Copy link
Member

Hi @nour-borgi could you please remind me why we need this. I don't really understand what this is doing exactly or why it's needed.

@nour-borgi
Copy link
Contributor Author

nour-borgi commented Sep 14, 2023

Hello @rcrichton, this PR to replace Hapi proxy by the Kafka channel in eth-cdr (we need the request body)

How it was implemented: the response body will be saved as a message in a kafka topic
This PR is to: store both the response body and the request body in kafka topic

Thus, we can get the bundle from the request body in Logstash to be processed

* Add more response details
* Ensure request body is available when kafka route is primary
* Ensure backwards compatible properties are used
Copy link
Contributor

coderabbitai bot commented Sep 13, 2024

Walkthrough

The changes enhance the Koa middleware's request and response handling by introducing two new properties to the context object: resDetails and reqBody. The setKoaResponse function now captures detailed response information, while the sendRequestToRoutes function saves the request body. Additionally, the sendKafkaRequest function is updated to utilize these new context properties, improving the integration with Kafka messaging.

Changes

Files Change Summary
src/middleware/router.js Added ctx.resDetails to store response details and ctx.reqBody to store request body.

Poem

In the land of code where rabbits play,
New details hop in, brightening the day.
With requests and responses neatly stored,
Our middleware dances, never ignored.
So let’s celebrate with a joyful cheer,
For better logging is finally here! 🐇✨

Tip

OpenAI O1 model for chat
  • We have deployed OpenAI's latest O1 model for chat.
  • OpenAI claims that this model has superior reasoning capabilities than their GPT-4o model.
  • Please share any feedback with us in the discussions post.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e686ddb and 76892af.

Files selected for processing (1)
  • src/middleware/router.js (3 hunks)
Additional comments not posted (4)
src/middleware/router.js (4)

64-71: LGTM! Consider rcrichton's suggestion for future enhancements.

The changes to save the response details of the primary route in ctx.resDetails look good. As suggested by rcrichton in the past review comment, consider sending a structure similar to what the orchestrations capture for future enhancements.


68-70: Good practice to convert the body to a string if it is a Buffer.

Converting the body to a string if it is a Buffer ensures that the response can be easily logged or processed later.


307-308: Verify the usage of ctx.reqBody and address rcrichton's concern.

Saving the request body of the primary route in ctx.reqBody looks good. However, as pointed out by rcrichton in the past review comment, ensure that ctx.reqBody is set correctly even if the Kafka route is a primary route.

Run the following script to verify the usage of ctx.reqBody:

#!/bin/bash
# Description: Verify that `ctx.reqBody` is set correctly for Kafka routes.

# Test 1: Search for the usage of `ctx.reqBody` in Kafka routes. 
# Expect: `ctx.reqBody` should be used in the `sendKafkaRequest` function.
ast-grep --lang javascript --pattern $'function sendKafkaRequest(ctx, route) {
  $$$
  const message = {
    $$$
    body: ctx.reqBody,
    $$$
  }
  $$$
}'

# Test 2: Search for the assignment of `ctx.reqBody` in the `sendRequestToRoutes` function.
# Expect: `ctx.reqBody` should be set for all primary routes, including Kafka routes.
ast-grep --lang javascript --pattern $'function sendRequestToRoutes(ctx, routes, next) {
  $$$
  for (const route of Array.from(routesToRunSimultaneously)) {
    $$$
    if (route.primary) {
      $$$
      ctx.reqBody = Buffer.isBuffer(ctx.body) ? ctx.body.toString() : ctx.body
      $$$
    }
    $$$
  }
  $$$
}'

674-675: LGTM!

Using ctx.reqBody and ctx.resDetails to construct the Kafka message ensures that the request body and response details are included correctly. The changes are consistent with the modifications made in the setKoaResponse and sendRequestToRoutes functions.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@rcrichton rcrichton merged commit 8ba033d into master Sep 16, 2024
4 of 5 checks passed
@rcrichton rcrichton deleted the primary-res-req-body-fix branch September 16, 2024 10:15
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.

3 participants