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

Add logging for HTTP failures #179

Merged
merged 5 commits into from
Oct 30, 2024
Merged

Add logging for HTTP failures #179

merged 5 commits into from
Oct 30, 2024

Conversation

dthaler
Copy link
Collaborator

@dthaler dthaler commented Oct 30, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new MezmoFetcher class for fetching and processing data from the Mezmo API.
    • Enhanced data retrieval methods with improved logging capabilities.
  • Bug Fixes

    • Improved error handling and logging for various data fetching methods to ensure better traceability.
  • Documentation

    • Updated method signatures across multiple classes to include logging parameters for better operational visibility.
  • Refactor

    • Removed Mezmo-related functionality from the Fetcher class, streamlining the codebase.

Copy link

coderabbitai bot commented Oct 30, 2024

Warning

Rate limit exceeded

@dthaler has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 19 minutes and 25 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between c9509d8 and 75fc144.

Walkthrough

The pull request introduces significant changes across several files in the OrcanodeMonitor project. The Fetcher class has had all Mezmo-related functionality removed, including constants and methods. Logging capabilities have been enhanced by adding ILogger parameters to multiple methods. A new class, MezmoFetcher, has been added to handle Mezmo API interactions. The PeriodicTasks class has been updated to improve logging for various asynchronous method calls. Additionally, the DataplicityNodeModel and NodeEventsModel classes have been modified to incorporate logging in their data fetching methods.

Changes

File Path Change Summary
OrcanodeMonitor/Core/Fetcher.cs Removed Mezmo-related URLs, constants, and logic; updated method signatures to include ILogger; added logging statements for error handling.
OrcanodeMonitor/Core/MezmoFetcher.cs Introduced MezmoFetcher class with methods for fetching and processing data from the Mezmo API; includes logging and error handling.
OrcanodeMonitor/Core/PeriodicTasks.cs Updated ExecuteTask method to pass ILogger to multiple asynchronous method calls for improved logging.
OrcanodeMonitor/Pages/DataplicityNode.cshtml.cs Updated OnGetAsync method to include _logger in Fetcher.GetDataplicityDataAsync call for enhanced logging.
OrcanodeMonitor/Pages/NodeEvents.cshtml.cs Modified FetchEvents method to accept ILogger parameter; removed internal error handling and ensured consistent logging.

Possibly related PRs

  • Add Mezmo state tracking #161: This PR adds Mezmo state tracking, which is directly related to the removal of Mezmo functionality in the main PR, indicating a shift in how Mezmo data is handled.
  • Generate Mezmo events #162: This PR modifies the Fetcher class to enhance Mezmo logging and event handling, which aligns with the main PR's focus on improving logging capabilities.
  • Add a node events page #173: This PR introduces a method to retrieve recent events for nodes, which is relevant as the main PR updates logging in methods that may interact with event data.
  • Fix value labels in event notifications #178: This PR updates event logging methods in the Fetcher class, which relates to the main PR's enhancements to logging and error handling in similar methods.

🐇 In the meadow, where code does bloom,
The Fetcher shed its Mezmo gloom.
With logging bright, our tasks align,
New methods spring, like hops divine.
Fetching data, we’ll trace each call,
In this code garden, we’ll stand tall! 🌼


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

@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: 15

🧹 Outside diff range and nitpick comments (9)
OrcanodeMonitor/Pages/DataplicityNode.cshtml.cs (1)

Line range hint 17-17: Fix logger type parameter.

The logger is typed for IndexModel but should be typed for the current class DataplicityNodeModel.

Apply this change:

-private readonly ILogger<IndexModel> _logger;
+private readonly ILogger<DataplicityNodeModel> _logger;
OrcanodeMonitor/Core/MezmoFetcher.cs (5)

124-129: Log a warning when no hosts data is retrieved.

If jsonArray is empty, the method returns silently. Logging a warning can aid in diagnosing issues with data retrieval.

Consider adding a log message:

  if (jsonArray.IsNullOrEmpty())
  {
+     logger.LogWarning("No hosts data retrieved from Mezmo.");
      // Error so do nothing.
      return;
  }

175-178: Implement creation of new nodes when missing.

The TODO suggests creating a node if one is not found for a Mezmo name. Implementing this will help track new nodes automatically.

Would you like assistance in implementing node creation for new Mezmo hosts?


234-239: Log a warning when no views data is retrieved.

Similar to the hosts data, if no views data is retrieved, logging a warning can help identify issues during data fetching.

Consider adding a log message:

  if (jsonArray.IsNullOrEmpty())
  {
+     logger.LogWarning("No views data retrieved from Mezmo.");
      // Error so do nothing.
      return;
  }

292-295: Implement creation of new nodes from Mezmo views.

As noted in the TODO, creating nodes when they are not found can improve the monitoring system's comprehensiveness.

Would you like assistance in implementing node creation for new Mezmo view hosts?


333-337: Consistent use of ILogger interface.

In UpdateMezmoDataAsync, consider using the generic ILogger<MezmoFetcher> for consistency and to take advantage of category-based logging.

Apply this diff to use the generic logger:

- public async static Task UpdateMezmoDataAsync(OrcanodeMonitorContext context, ILogger logger)
+ public async static Task UpdateMezmoDataAsync(OrcanodeMonitorContext context, ILogger<MezmoFetcher> logger)
OrcanodeMonitor/Core/Fetcher.cs (3)

332-333: Avoid duplication in error logging for empty serial

The error message for an empty serial is logged twice in lines 328 and 333. This can clutter the logs with redundant information.

Combine the conditions to log the error once:

if (serial.ToString().IsNullOrEmpty())
{
    logger.LogError($"Empty serial in UpdateDataplicityDataAsync result");
    continue;
}

Remove the redundant check and logging.


780-780: Simplify nullable DateTime assignment

The assignment node.LatestRecordedUtc = latestRecorded.HasValue ? latestRecorded.Value.ToUniversalTime() : null; can be simplified using the null-conditional operator.

Simplify the assignment:

-node.LatestRecordedUtc = latestRecorded.HasValue ? latestRecorded.Value.ToUniversalTime() : null;
+node.LatestRecordedUtc = latestRecorded?.ToUniversalTime();

256-256: Consider adding more context to exception logs

Logging only the exception in UpdateOrcaHelloDataAsync may not provide enough context. Including additional information can aid in debugging.

Include relevant context in the log message:

-logger.LogError(ex, "Exception in UpdateOrcaHelloDataAsync");
+logger.LogError(ex, $"Exception in UpdateOrcaHelloDataAsync while processing URL: {_orcaHelloHydrophonesUrl}");
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b8e8ba6 and c9509d8.

📒 Files selected for processing (5)
  • OrcanodeMonitor/Core/Fetcher.cs (18 hunks)
  • OrcanodeMonitor/Core/MezmoFetcher.cs (1 hunks)
  • OrcanodeMonitor/Core/PeriodicTasks.cs (1 hunks)
  • OrcanodeMonitor/Pages/DataplicityNode.cshtml.cs (1 hunks)
  • OrcanodeMonitor/Pages/NodeEvents.cshtml.cs (2 hunks)
🧰 Additional context used
📓 Learnings (2)
OrcanodeMonitor/Core/Fetcher.cs (1)
Learnt from: dthaler
PR: orcasound/orcanode-monitor#162
File: OrcanodeMonitor/Core/Fetcher.cs:579-582
Timestamp: 2024-10-17T20:47:37.157Z
Learning: In `OrcanodeMonitor/Core/Fetcher.cs`, `SaveChangesAsync()` is called after each update (e.g., in methods `UpdateMezmoHostsAsync` and `UpdateMezmoViewsAsync`), ensuring that duplicate events are not logged.
OrcanodeMonitor/Pages/NodeEvents.cshtml.cs (1)
Learnt from: dthaler
PR: orcasound/orcanode-monitor#173
File: OrcanodeMonitor/Pages/NodeEvents.cshtml.cs:0-0
Timestamp: 2024-10-25T18:29:41.800Z
Learning: In the `NodeEventsModel` class, the ID is part of the route, so the functions `OnGet` and `OnPost` cannot be called with an empty ID.
🔇 Additional comments (5)
OrcanodeMonitor/Core/PeriodicTasks.cs (1)

Line range hint 14-61: LGTM! Robust background service implementation.

The class follows best practices with proper error handling, cancellation support, and configurable polling frequency.

OrcanodeMonitor/Core/MezmoFetcher.cs (1)

216-217: Verify necessity of calling SaveChangesAsync.

Before calling SaveChangesAsync, ensure that there are changes pending to save. Unnecessary calls can impact performance.

Please review whether changes have been made to the context that require saving at this point.

OrcanodeMonitor/Core/Fetcher.cs (3)

572-572: Handle potential changes in dataplicity_id appropriately

When dataplicity_id changes, the current implementation logs a warning. However, it might be necessary to update the DataplicitySerial or handle the change explicitly to maintain data integrity.

Review the logic to ensure that changes in dataplicity_id are correctly processed. Consider updating the DataplicitySerial if appropriate.


652-652: Ensure consistent use of SaveChangesAsync()

Per previous learnings, calling SaveChangesAsync() after each update prevents duplicate events from being logged. In UpdateOrcasoundDataAsync, SaveChangesAsync() is called only at the end.

Review whether SaveChangesAsync() should be called after significant updates within loops to persist changes immediately and prevent potential issues with concurrent modifications.


610-615: ⚠️ Potential issue

Handle potential null references in UpdateOrcasoundSiteDataAsync

If dataArray is null, calling EnumerateArray() could throw an exception.

Add a null check before enumerating:

if (dataArray.HasValue)
{
    foreach (JsonElement feed in dataArray.Value.EnumerateArray())
    {
        UpdateOrcasoundNode(feed, foundList, unfoundList, context, site, logger);
    }
}
+else
+{
+    logger.LogError("Data array is null in UpdateOrcasoundSiteDataAsync");
+}

Likely invalid or redundant comment.

OrcanodeMonitor/Pages/NodeEvents.cshtml.cs Show resolved Hide resolved
OrcanodeMonitor/Pages/NodeEvents.cshtml.cs Show resolved Hide resolved
OrcanodeMonitor/Pages/NodeEvents.cshtml.cs Show resolved Hide resolved
OrcanodeMonitor/Core/PeriodicTasks.cs Show resolved Hide resolved
OrcanodeMonitor/Core/MezmoFetcher.cs Show resolved Hide resolved
OrcanodeMonitor/Core/Fetcher.cs Show resolved Hide resolved
OrcanodeMonitor/Core/Fetcher.cs Outdated Show resolved Hide resolved
OrcanodeMonitor/Core/Fetcher.cs Show resolved Hide resolved
OrcanodeMonitor/Core/Fetcher.cs Show resolved Hide resolved
@dthaler dthaler merged commit 7becca2 into main Oct 30, 2024
8 checks passed
@dthaler dthaler deleted the logging branch October 30, 2024 02:04
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.

1 participant