FlourishHealth / ferns-api

Apache License 2.0
1 stars 3 forks source link

Capture Sentry message when querying over limit without page #370

Closed joshgachnang closed 6 months ago

joshgachnang commented 7 months ago

User description

Otherwise we are silently discarding data.


Type

enhancement


Description


Changes walkthrough

Relevant files
Enhancement
api.test.ts
Enhance API Tests with Sentry Integration                               

src/api.test.ts
  • Imported Sentry from @sentry/node.
  • Mocked Sentry methods (captureMessage, captureException,
    isInitialized) for testing.
  • Added a test to check if Sentry.captureMessage is called when querying
    over the limit without pagination.
  • +17/-0   
    api.ts
    Log and Capture Sentry Message for Over-limit Queries without
    Pagination

    src/api.ts
  • Imported Sentry and logger for logging and capturing messages.
  • Added a check to log and capture a message with Sentry when a query
    returns more results than the limit without pagination.
  • +14/-0   

    PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    codiumai-pr-agent-pro[bot] commented 7 months ago

    PR Description updated to latest commit (https://github.com/FlourishHealth/ferns-api/commit/5ee84a0e23d917c30f07f70e6f454c0aff94209b)

    codiumai-pr-agent-pro[bot] commented 7 months ago

    PR Review

    ⏱️ Estimated effort to review [1-5] 2, because the changes are focused and well-defined, involving integration with Sentry for specific logging functionality and the addition of tests to verify this behavior. The changes are not extensive and are localized to specific parts of the code, making the review process straightforward.
    🧪 Relevant tests Yes
    🔍 Possible issues Logging Verbosity: The addition of Sentry logging for every query that exceeds a limit without pagination could potentially lead to a high volume of log messages, especially if the API is heavily used. This could clutter the Sentry dashboard and might require filtering or rate limiting strategies.
    Mock Completeness: The mocking of Sentry methods in the test file is a good practice, but it's important to ensure that all relevant Sentry behaviors are accurately represented in the mocks to avoid discrepancies between test and production environments.
    🔒 Security concerns No

    ✨ Review tool usage guide:
    **Overview:** The `review` tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be [added](https://pr-agent-docs.codium.ai/tools/review/#general-configurations) by configuring the tool. The tool can be triggered [automatically](https://pr-agent-docs.codium.ai/usage-guide/automations_and_usage/#github-app-automatic-tools-when-a-new-pr-is-opened) every time a new PR is opened, or can be invoked manually by commenting on any PR. - When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L23) related to the review tool (`pr_reviewer` section), use the following template: ``` /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=... ``` - With a [configuration file](https://pr-agent-docs.codium.ai/usage-guide/configuration_options/), use the following template: ``` [pr_reviewer] some_config1=... some_config2=... ``` See the review [usage page](https://pr-agent-docs.codium.ai/tools/review/) for a comprehensive guide on using this tool.
    codiumai-pr-agent-pro[bot] commented 7 months ago

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Use TypeScript utility types for more focused mocking. ___ **Consider using TypeScript's Partial or Pick utility types to mock only the necessary
    Sentry methods instead of the entire module. This approach can make the test more focused
    and maintainable.** [src/api.test.ts [29-38]](https://github.com/FlourishHealth/ferns-api/pull/370/files#diff-7859a5148ded19af212367ec9a3558864109c5515dea657bc8f11ee238765dc7R29-R38) ```diff jest.mock("@sentry/node", () => { - // Auto-mock the Sentry module - const originalModule = jest.requireActual("@sentry/node"); - + const { captureMessage, captureException, isInitialized } = jest.requireActual("@sentry/node"); return { - ...originalModule, // Use the original module's implementations captureMessage: jest.fn(), captureException: jest.fn(), - isInitialized: jest.fn(() => true), // Override isInitialized + isInitialized: jest.fn(() => true), }; }); ```
    Abstract Sentry functionality behind a custom logger for better decoupling. ___ **Instead of directly using Sentry.isInitialized() and Sentry.captureMessage(msg) in the
    business logic, consider abstracting Sentry's functionality behind a custom logger or
    error handler. This can decouple your application logic from a specific logging or error
    tracking implementation, making it easier to switch or extend in the future.** [src/api.ts [574-576]](https://github.com/FlourishHealth/ferns-api/pull/370/files#diff-769911c416ccf8514d8fd941ae0abe8fb5c606ade0c218e22151a5f5f9f3d700R574-R576) ```diff -if (Sentry.isInitialized()) { - Sentry.captureMessage(msg); -} +logger.warn(msg); ```
    Enhancement
    Verify the number of times a mocked function is called for reliability. ___ **To ensure the test's reliability, consider verifying that Sentry.captureMessage was called
    the expected number of times using expect(Sentry.captureMessage).toHaveBeenCalledTimes(1)
    or a similar assertion.** [src/api.test.ts [507-509]](https://github.com/FlourishHealth/ferns-api/pull/370/files#diff-7859a5148ded19af212367ec9a3558864109c5515dea657bc8f11ee238765dc7R507-R509) ```diff +expect(Sentry.captureMessage).toHaveBeenCalledTimes(1); expect(Sentry.captureMessage).toHaveBeenCalledWith( 'More than 3 results returned for foods without pagination, data may be silently truncated. req.query: {"limit":"4"}' ); ```
    Validate query parameters early for informative client responses. ___ **Consider validating the presence and correctness of req.query.page and other query
    parameters at the beginning of the request handling. This can help in returning a more
    informative response to the client in case of invalid input, rather than logging a
    warning.** [src/api.ts [568-577]](https://github.com/FlourishHealth/ferns-api/pull/370/files#diff-769911c416ccf8514d8fd941ae0abe8fb5c606ade0c218e22151a5f5f9f3d700R568-R577) ```diff -if (!req.query.page) { - const msg = - `More than ${limit} results returned for ${model.collection.name} ` + - `without pagination, data may be silently truncated. req.query: ` + - `${JSON.stringify(req.query)}`; - logger.warn(msg); - if (Sentry.isInitialized()) { - Sentry.captureMessage(msg); - } +if (!req.query.page || isNaN(parseInt(req.query.page, 10))) { + return res.status(400).json({error: "Invalid or missing 'page' query parameter."}); } ```
    Maintainability
    Extract pagination warning message generation to a separate function. ___ **To improve code readability and maintainability, consider extracting the logic for
    generating the warning message about pagination into a separate function. This can also
    make it easier to reuse the logic in different parts of your application if needed.** [src/api.ts [569-576]](https://github.com/FlourishHealth/ferns-api/pull/370/files#diff-769911c416ccf8514d8fd941ae0abe8fb5c606ade0c218e22151a5f5f9f3d700R569-R576) ```diff -const msg = - `More than ${limit} results returned for ${model.collection.name} ` + - `without pagination, data may be silently truncated. req.query: ` + - `${JSON.stringify(req.query)}`; +const msg = generatePaginationWarningMessage(model.collection.name, limit, req.query); logger.warn(msg); if (Sentry.isInitialized()) { Sentry.captureMessage(msg); } ```

    ✨ Improve tool usage guide:
    **Overview:** The `improve` tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered [automatically](https://pr-agent-docs.codium.ai/usage-guide/automations_and_usage/#github-app-automatic-tools-when-a-new-pr-is-opened) every time a new PR is opened, or can be invoked manually by commenting on a PR. - When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L78) related to the improve tool (`pr_code_suggestions` section), use the following template: ``` /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=... ``` - With a [configuration file](https://pr-agent-docs.codium.ai/usage-guide/configuration_options/), use the following template: ``` [pr_code_suggestions] some_config1=... some_config2=... ``` See the improve [usage page](https://pr-agent-docs.codium.ai/tools/improve/) for a comprehensive guide on using this tool.