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

added timestamps to the API consumption logs #3732

Merged
merged 1 commit into from
Oct 23, 2024
Merged

Conversation

Baalmart
Copy link
Contributor

@Baalmart Baalmart commented Oct 23, 2024

Description

added timestamps to the API consumption logs

Changes Made

  • added timestamps to the API consumption logs

Testing

  • Tested locally
  • Tested against staging environment
  • Relevant tests passed: [List test names]

Affected Services

  • Which services were modified:
    • Device Registry

Endpoints Ready for Testing

  • New endpoints ready for testing:
    • Add Events

API Documentation Updated?

  • Yes, API documentation was updated
  • No, API documentation does not need updating

Additional Notes

added timestamps to the API consumption logs

Summary by CodeRabbit

  • New Features

    • Introduced a new logging function that includes timestamps in log messages for improved traceability.
    • Enhanced logging consistency across the application by replacing standard log statements with the new timestamped logging function.
  • Bug Fixes

    • Updated error handling in event insertion to log internal server errors with timestamps, aiding in debugging.

Copy link
Contributor

coderabbitai bot commented Oct 23, 2024

📝 Walkthrough

Walkthrough

The changes introduce a new logging function, logTextWithTimestamp, which includes timestamps in log messages to enhance logging capabilities. This function is integrated into the insert method of create-event.js, replacing console.log calls to standardize logging format. Additionally, the error handling within the insert method is updated to log server errors with timestamps. The logging module is also updated to export the new function while keeping existing logging functions unchanged.

Changes

File Path Change Summary
src/device-registry/utils/create-event.js - Added method: logTextWithTimestamp
- Updated insert method to replace console.log with logTextWithTimestamp for logging events and errors.
src/device-registry/utils/log.js - Added method: const logTextWithTimestamp = (message)
- Updated module exports to include logTextWithTimestamp.

Possibly related PRs

Suggested reviewers

  • BenjaminSsempala

🎉 In the land of logs, a new function did bloom,
With timestamps in tow, it brightens the room.
From console.log to a format so neat,
Debugging is now a more organized feat.
So here's to the changes, both clever and wise,
A logging enhancement that surely will rise! 🌟


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

codecov bot commented Oct 23, 2024

Codecov Report

Attention: Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 11.69%. Comparing base (7986491) to head (fe659de).
Report is 2 commits behind head on staging.

Files with missing lines Patch % Lines
src/device-registry/utils/create-event.js 25.00% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           staging    #3732      +/-   ##
===========================================
- Coverage    11.69%   11.69%   -0.01%     
===========================================
  Files          110      110              
  Lines        14847    14848       +1     
  Branches       274      274              
===========================================
  Hits          1737     1737              
- Misses       13110    13111       +1     
Files with missing lines Coverage Δ
src/device-registry/utils/create-event.js 3.57% <25.00%> (-0.01%) ⬇️

Copy link
Contributor

Device registry changes in this PR available for preview here

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

🧹 Outside diff range and nitpick comments (2)
src/device-registry/utils/log.js (1)

Line range hint 1-43: Consider centralizing logging configuration

To ensure consistency across the AirQo platform:

  1. Consider moving logging configuration to a shared utility
  2. Define standard timestamp formats in a configuration file
  3. Implement structured logging for better parsing in production

This would make it easier to:

  • Maintain consistent logging across all services
  • Switch logging providers if needed
  • Implement log aggregation and analysis
src/device-registry/utils/create-event.js (1)

2651-2652: Consider including the error stack trace for better debugging.

While the timestamp addition is good, consider including the error stack trace in the log message for better error tracking.

-      logTextWithTimestamp(`API: Internal Server Error ${error.message}`);
+      logTextWithTimestamp(`API: Internal Server Error ${error.message}\n${error.stack}`);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7986491 and fe659de.

📒 Files selected for processing (2)
  • src/device-registry/utils/create-event.js (4 hunks)
  • src/device-registry/utils/log.js (2 hunks)
🧰 Additional context used
🔇 Additional comments (3)
src/device-registry/utils/log.js (1)

37-43: LGTM! Clean and well-organized exports

The module exports are properly structured and include all logging utilities.

src/device-registry/utils/create-event.js (2)

2632-2633: LGTM! Good error tracking enhancement.

The addition of timestamp logging for failed operations will help with debugging and monitoring API consumption issues.


2642-2643: LGTM! Good success tracking enhancement.

The addition of timestamp logging for successful operations will help track API consumption patterns and performance.

Comment on lines +33 to +35
const logTextWithTimestamp = (message) => {
console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
};
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

Align logTextWithTimestamp with existing logging patterns

The new function differs from other logging utilities in three ways:

  1. Missing environment check (logs in production)
  2. No return value
  3. Inconsistent error handling

Suggested implementation:

 const logTextWithTimestamp = (message) => {
-  console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
+  if (process.env.NODE_ENV !== "production") {
+    console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
+  }
+  return "log deactivated in prod and stage";
 };
📝 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
const logTextWithTimestamp = (message) => {
console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
};
const logTextWithTimestamp = (message) => {
if (process.env.NODE_ENV !== "production") {
console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
}
return "log deactivated in prod and stage";
};

@@ -1,3 +1,5 @@
const moment = require("moment");
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

Consider using native Date methods instead of moment.js

For simple timestamp formatting, native JavaScript methods like new Date().toISOString() or Intl.DateTimeFormat could be more performant alternatives to moment.js, which is a relatively heavy library.

Example implementation:

-const moment = require("moment");
+const formatTimestamp = () => {
+  return new Intl.DateTimeFormat('en-US', {
+    year: 'numeric',
+    month: '2-digit',
+    day: '2-digit',
+    hour: '2-digit',
+    minute: '2-digit',
+    second: '2-digit',
+    hour12: false
+  }).format(new Date()).replace(/(\d+)\/(\d+)\/(\d+)/, '$3-$1-$2');
+};

Committable suggestion was skipped due to low confidence.

@Baalmart Baalmart merged commit a2bea53 into staging Oct 23, 2024
50 checks passed
@Baalmart Baalmart deleted the hf-timestamps branch October 23, 2024 13:27
@Baalmart Baalmart mentioned this pull request Oct 23, 2024
1 task
@coderabbitai coderabbitai bot mentioned this pull request Oct 25, 2024
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant