Skip to content

Conversation

MichaelBuessemeyer
Copy link
Contributor

@MichaelBuessemeyer MichaelBuessemeyer commented Nov 28, 2024

The method listGrouped needs to be investigated further.

Answering 400 at /api/datasets?isActive=false for user <id>: {"messages":[{"error":"Grouping datasets failed"},{"chain":"[Server Time 2024-11-28 13:54] <~ Failed to retrieve list of data sets."}]}

Follow up to #8246

Steps to test:

Copy link
Contributor

coderabbitai bot commented Nov 28, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces enhancements to the DatasetController class, specifically improving logging and error handling in the listGrouped and segmentAnythingMask methods. Modifications include additional logging for dataset processing and user details, refined error messages, and strengthened input validation. These updates aim to enhance the observability and robustness of dataset operations.

Changes

File Change Summary
app/controllers/DatasetController.scala - Enhanced logging in listGrouped method, including dataset and user details.
- Improved error handling with descriptive messages.
- Strengthened input validation in segmentAnythingMask method with new checks and logging.

Possibly related PRs

Suggested labels

bug, urgent

Suggested reviewers

  • fm3

Poem

🐇 In the code where datasets play,
Logging brightens the data way.
Errors caught with messages clear,
Observability brings us cheer!
With each new check, we hop along,
Enhancing our code, making it strong! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between fe47da7 and 2179355.

📒 Files selected for processing (1)
  • app/controllers/DatasetController.scala (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/controllers/DatasetController.scala

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.

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: 3

🧹 Outside diff range and nitpick comments (2)
app/controllers/DatasetController.scala (1)

179-262: Consider implementing structured logging

To improve the debugging capabilities while maintaining security and performance, consider implementing structured logging:

  1. Define a logging schema for dataset operations
  2. Use MDC (Mapped Diagnostic Context) to track request context
  3. Implement log sanitization to prevent PII exposure
  4. Add request tracing for better correlation of logs

Example implementation:

object DatasetLogging {
  case class DatasetOperationContext(
    datasetCount: Int,
    organizationId: String,
    requestId: String,
    userId: Option[String]
  )

  def withContext[T](context: DatasetOperationContext)(block: => T): T = {
    MDC.put("request.id", context.requestId)
    MDC.put("dataset.count", context.datasetCount.toString)
    MDC.put("organization.id", context.organizationId)
    context.userId.foreach(id => MDC.put("user.id", id))
    try {
      block
    } finally {
      MDC.clear()
    }
  }
}
app/models/dataset/Dataset.scala (1)

223-227: Consider enhancing the second log message.

While the first log message provides valuable information about selection predicates, the second log message "Requesting datasets with query" could be more informative.

Consider this improvement:

-      _ = logger.info("Requesting datasets with query")
+      _ = logger.info(s"Executing dataset query with limit ${limitOpt.getOrElse("none")}")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 781f687 and 82690ac.

📒 Files selected for processing (2)
  • app/controllers/DatasetController.scala (1 hunks)
  • app/models/dataset/Dataset.scala (2 hunks)
🔇 Additional comments (1)
app/models/dataset/Dataset.scala (1)

118-125: LGTM! Improved error messages for better debugging.

The error messages are now more specific and descriptive, which will help in identifying parsing issues more effectively.

m: MessagesProvider): Fox[List[JsObject]] =
for {
_ <- Fox.successful(())
_ = logger.info(s"datasets: $datasets, requestingUser: $requestingUser")
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Security & Performance: Revise logging strategy

The current logging implementation has several concerns:

  1. Security: Logging entire objects (datasets, requestingUser, requestingUserTeamManagerMemberships) may expose sensitive information
  2. Performance: Excessive logging at multiple levels
  3. Log levels: Using INFO level for debug information
-      _ = logger.info(s"datasets: $datasets, requestingUser: $requestingUser")
+      _ = logger.debug(s"Processing ${datasets.size} datasets for user ${requestingUser.map(_._id).getOrElse("anonymous")}")

-      _ = logger.info(s"requestingUserTeamManagerMemberships: $requestingUserTeamManagerMemberships")
+      _ = logger.debug(s"User ${requestingUser.map(_._id).getOrElse("anonymous")} has ${requestingUserTeamManagerMemberships.size} team manager memberships")

-          _ = logger.info(s"byOrgaTuple: $byOrgaTuple")
+          _ = logger.debug(s"Processing organization: ${byOrgaTuple._1} with ${byOrgaTuple._2.size} datasets")

-              logger.info(s"datastore: ${byDataStoreTuple._1}, datasets: ${byDataStoreTuple._2}")
+              logger.debug(s"Processing datastore: ${byDataStoreTuple._1} with ${byDataStoreTuple._2.size} datasets")

Also applies to: 233-233, 238-238, 243-243

Comment on lines +230 to +232
userService
.teamManagerMembershipsFor(user._id)) ?~> s"Could not find team manager memberships for user ${requestingUser
.map(_._id)}"
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance error messages for better debugging

The error messages should provide more context about what failed and why.

        userService
          .teamManagerMembershipsFor(user._id)) ?~> s"Could not find team manager memberships for user ${requestingUser
-        .map(_._id)}"
+        .map(_._id)}. Verify user permissions and team associations."

-          } ?~> s"Could not group by datastore for datasets ${byOrgaTuple._2.map(_.name)}"
+          } ?~> s"Failed to process datasets by datastore. Organization: ${organization._id}, Datastores: ${groupedByDataStore.map(_._1).mkString(", ")}"

-      } ?~> s"Could not group by organization for datasets ${datasets.map(_.name)}"
+      } ?~> s"Failed to process datasets by organization. Dataset count: ${datasets.size}, Organizations: ${groupedByOrga.map(_._1).mkString(", ")}"

Also applies to: 260-260, 262-262

Comment on lines 213 to 215
limit) ?~> "dataset.list.failed" ?~> "Dataset listing failed"
_ = logger.info(s"Found ${datasets.size} datasets successfully")
js <- listGrouped(datasets, request.identity) ?~> "dataset.list.failed" ?~> "Grouping datasets failed"
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling and messages

The error handling in these lines could be enhanced:

  1. Remove the redundant error message "Dataset listing failed" as it duplicates "dataset.list.failed"
  2. Enhance the error message for grouping failure to include more context
-                                                     limit) ?~> "dataset.list.failed" ?~> "Dataset listing failed"
+                                                     limit) ?~> "dataset.list.failed"
            _ = logger.info(s"Found ${datasets.size} datasets successfully")
-            js <- listGrouped(datasets, request.identity) ?~> "dataset.list.failed" ?~> "Grouping datasets failed"
+            js <- listGrouped(datasets, request.identity) ?~> "dataset.list.grouping.failed" ?~> s"Failed to group ${datasets.size} datasets"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
limit) ?~> "dataset.list.failed" ?~> "Dataset listing failed"
_ = logger.info(s"Found ${datasets.size} datasets successfully")
js <- listGrouped(datasets, request.identity) ?~> "dataset.list.failed" ?~> "Grouping datasets failed"
limit) ?~> "dataset.list.failed"
_ = logger.info(s"Found ${datasets.size} datasets successfully")
js <- listGrouped(datasets, request.identity) ?~> "dataset.list.grouping.failed" ?~> s"Failed to group ${datasets.size} datasets"

Copy link
Member

@fm3 fm3 left a comment

Choose a reason for hiding this comment

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

let’s avoid printing entire user objects, other than that LGTM

m: MessagesProvider): Fox[List[JsObject]] =
for {
_ <- Fox.successful(())
_ = logger.info(s"datasets: $datasets, requestingUser: $requestingUser")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_ = logger.info(s"datasets: $datasets, requestingUser: $requestingUser")
_ = logger.info(s"datasets: $datasets, requestingUser: ${requestingUser._id}")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let’s avoid printing entire user objects,

only user objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just reduced the logging to only id or such and not whole object. Do you think whole object is ok (besides users)? This might make debugging easier 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Yes, should be ok. This is only temporary, this isn’t super sensitive data and the logs are of course not public.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants