Skip to content

Conversation

junhaoliao
Copy link
Member

@junhaoliao junhaoliao commented Mar 10, 2025

Description

  1. fix: Format unstructured log events by prepending timestamps.

Checklist

  • The PR satisfies the contribution guidelines.
  • This is a breaking change and that has been indicated in the PR title, OR this isn't a
    breaking change.
  • Necessary docs have been updated, OR no docs need to be updated.

Validation performed

  1. Launched debug server and opened demo file https://yscope.s3.us-east-2.amazonaws.com/sample-logs/yarn-ubuntu-resourcemanager-ip-172-31-17-135.log.1.clp.zst in the log viewer.
  2. Observed formatted timestamps are prepended to each log event. Compared the text with an older version (1e8455e) of the log viewer, which is before the unintended formatted timestamp removal in feat!: Add support for viewing auto-generated keys in Structured IR files. #192 , and found the year, zero-padded month, zero-padded day, T, zero-padded 24-h hour, zero-padded minute, zero-padded second and zero-padded 3-digit milliseconds parts matched. For the timezone, +00:00 is shown to represent UTC, instead of Z or `` (not present) depending on the timestamp format string in the IR file's metadata.
  3. Observed the dates (i.e., formatted timestamps) are fully highlighted in green colour in the monaco-editor.

Summary by CodeRabbit

  • New Features
    • Enhanced date recognition in log messages to support timestamps with optional time components, fractional seconds, and timezone offsets.
    • Introduced consistent formatting for unstructured log entries by adding a formatted timestamp to each message.
  • Bug Fixes
    • Improved error handling in the decoding process by replacing console error logging with exception throwing.

@junhaoliao junhaoliao requested a review from a team as a code owner March 10, 2025 19:48
Copy link

coderabbitai bot commented Mar 10, 2025

Walkthrough

The pull request updates two components. First, it modifies the regular expression responsible for matching date strings in a custom log language in the Monaco editor to enhance flexibility, allowing for optional time components and timezone offsets. Second, it introduces a new static method in the ClpIrDecoder class that formats unstructured log events by prepending formatted timestamps. The decodeRange method is adjusted to utilize this new method when no formatter is set, altering its control flow.

Changes

File Path Change Summary
src/components/Editor/MonacoInstance/language.ts Updated the regex pattern for date matching to allow optional time components and timezone offsets, enhancing date string recognition.
src/services/decoders/ClpIrDecoder/index.ts Added a new static method #formatUnstructuredResults to format unstructured log events by prepending a formatted timestamp. Modified decodeRange to call this method when no formatter is set, replacing console error logging with an exception.

Possibly related PRs

Suggested reviewers

  • kirkrodrigues

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 46e2a5e and 97bb6ac.

📒 Files selected for processing (1)
  • src/components/Editor/MonacoInstance/language.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/Editor/MonacoInstance/language.ts

🪧 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 generate docstrings to generate docstrings for this 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.

@junhaoliao
Copy link
Member Author

junhaoliao commented Mar 10, 2025

This PR is submitted to unblock a formal release of KV-Pair IR logs.

#188 will be merged later as an improvement / extension to the timestamp formatting ability in the current PR.

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

🧹 Nitpick comments (1)
src/services/decoders/ClpIrDecoder/index.ts (1)

88-96: Consider defensive coding practices for timestamp handling

The current implementation directly modifies the input array and doesn't handle potential null or invalid timestamps. Consider adding validation checks:

static #formatUnstructuredResults = (results: DecodeResult[]): DecodeResult[] => {
    for (const r of results) {
        const [
            message, timestamp,
        ] = r;

+       // Skip formatting if timestamp is invalid
+       if (timestamp === null || timestamp === undefined) {
+           continue;
+       }

        const dayJsTimestamp: Dayjs = convertToDayjsTimestamp(timestamp);
        r[0] = dayJsTimestamp.format("YYYY-MM-DDTHH:mm:ss.SSSZ") + message;
    }

    return results;
};
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7baafbb and 679e4a5.

📒 Files selected for processing (2)
  • src/components/Editor/MonacoInstance/language.ts (1 hunks)
  • src/services/decoders/ClpIrDecoder/index.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}`: - Prefer `false == ...

**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

  • src/components/Editor/MonacoInstance/language.ts
  • src/services/decoders/ClpIrDecoder/index.ts
🔇 Additional comments (3)
src/components/Editor/MonacoInstance/language.ts (1)

36-36: Consider supporting both timezone formats in the date regex

The updated regex now supports timestamps with timezone offsets ([+-]HH:MM) but no longer supports UTC timestamps with the 'Z' suffix. This might cause regression issues if logs contain timestamps with 'Z' notation.

Consider using a more inclusive pattern like:

-/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}[+-]\d{2}:\d{2}/
+/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(?:[+-]\d{2}:\d{2}|Z)/

This would support both timezone offset notation and the 'Z' suffix for UTC time.

src/services/decoders/ClpIrDecoder/index.ts (2)

82-99: New method effectively formats timestamps for unstructured logs

The implementation correctly prepends formatted timestamps to unstructured log messages, addressing the issue mentioned in the PR (#201).


152-152: The modified control flow effectively applies formatting to unstructured logs

Good change - this ensures all unstructured log events are now formatted with timestamps, meeting the PR's objective.

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

🧹 Nitpick comments (1)
src/services/decoders/ClpIrDecoder/index.ts (1)

82-99: New static method added to format unstructured log events

The implementation looks good overall. A few observations:

  1. The method modifies the input array in-place rather than creating a copy. This is efficient but could cause issues if the caller expects the original array to remain unchanged.

  2. There's no space between the timestamp and the message in line 95, which might affect readability. Consider adding a space:

- r[0] = dayJsTimestamp.format("YYYY-MM-DDTHH:mm:ss.SSSZ") + message;
+ r[0] = dayJsTimestamp.format("YYYY-MM-DDTHH:mm:ss.SSSZ") + " " + message;
  1. The timestamp format is hardcoded. In the future, you might want to make this configurable.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 679e4a5 and de3f3e6.

📒 Files selected for processing (1)
  • src/services/decoders/ClpIrDecoder/index.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}`: - Prefer `false == ...

**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

  • src/services/decoders/ClpIrDecoder/index.ts
🔇 Additional comments (3)
src/services/decoders/ClpIrDecoder/index.ts (3)

166-166: Code style suggestion for boolean comparison

According to the coding guidelines, prefer false == <expression> rather than !<expression>.

- if (false === isJsonObject(fields)) {
+ if (false === isJsonObject(fields)) {

The code already follows the preferred style, so this is just confirming compliance with the coding guidelines.


141-141: Code style - boolean comparison follow coding guidelines

The code is using the preferred style null === results rather than results === null, which follows the coding guidelines for boolean comparisons.


152-152:

❓ Verification inconclusive

Updated return statement to format unstructured logs

This change ensures unstructured log events now have timestamps prepended, which aligns with the PR objective.

However, there's no explicit check to verify that this.#streamType is CLP_IR_STREAM_TYPE.UNSTRUCTURED before applying the formatting. This block currently handles both the case where formatting is not set for structured logs (which triggers an error) and unstructured logs (which should be formatted).

Consider making the intention clearer:

if (null === this.#formatter) {
    if (this.#streamType === CLP_IR_STREAM_TYPE.STRUCTURED) {
        // eslint-disable-next-line no-warning-comments
        // TODO: Revisit when we allow displaying structured logs without a formatter.
        console.error("Formatter is not set for structured logs.");
+       throw new Error("Formatter must be set for structured logs");
    }

-   return ClpIrDecoder.#formatUnstructuredResults(results);
+   // For unstructured logs, prepend timestamps when no formatter is set
+   return ClpIrDecoder.#formatUnstructuredResults(results);
}

This change ensures that the code fails explicitly when a formatter is required but not provided. Without this change, the code would continue execution with a warning, potentially leading to unexpected behavior. Does this approach align with how other error conditions are handled in the codebase?


Clarify and Explicitly Distinguish Log Handling

The updated return statement to format unstructured logs is a step forward, as it prepends timestamps in line with the PR’s objective. However, there is still a potential ambiguity: the code relies on falling through the condition to assume the logs are unstructured when no formatter is set. It might be clearer to explicitly verify that the stream type is indeed unstructured before calling ClpIrDecoder.#formatUnstructuredResults(results).

A suggested refactor would be:

if (null === this.#formatter) {
    if (this.#streamType === CLP_IR_STREAM_TYPE.STRUCTURED) {
        // eslint-disable-next-line no-warning-comments
        // TODO: Revisit when we allow displaying structured logs without a formatter.
        console.error("Formatter is not set for structured logs.");
+       throw new Error("Formatter must be set for structured logs");
    } else if (this.#streamType === CLP_IR_STREAM_TYPE.UNSTRUCTURED) {
        // For unstructured logs, prepend timestamps when no formatter is set.
        return ClpIrDecoder.#formatUnstructuredResults(results);
    }
}

This explicit branch ensures that:

  • Structured logs: an error is thrown immediately when a required formatter is missing.
  • Unstructured logs: the logs are formatted as intended.

Please verify that this approach aligns with how other error conditions are handled in the codebase, ensuring consistency across modules.

Copy link
Member

@kirkrodrigues kirkrodrigues left a comment

Choose a reason for hiding this comment

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

For the PR title, how about:

fix: Prepend formatted timestamps to unstructured log events (fixes #201).

@junhaoliao junhaoliao changed the title fix: Format unstructured log events by prepending timestamps (fixes #201). fix: Prepend formatted timestamps to unstructured log events (fixes #201). Mar 11, 2025
Copy link
Contributor

@AVMatthews AVMatthews left a comment

Choose a reason for hiding this comment

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

LGTM

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