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

Bring back zerolog.ConsoleWriter for local development #312

Merged
merged 5 commits into from
Jun 24, 2024

Conversation

m-Peter
Copy link
Collaborator

@m-Peter m-Peter commented Jun 21, 2024

Closes: #162

Description

There's a potential bug in the logging changes of ID: https://github.com/onflow/flow-evm-gateway/pull/152/files#diff-a5457320f6b67f92cbcb51459dd5ebd160912638c6b22d0964e52921b58a05d9R223
This ID is not always a number, it can be a string, or anything defined by the client.

This has been already fixed:

{
    "level": "debug",
    "component": "API",
    "IP": "127.0.0.1:60314",
    "url": "/",
    "id": 1,
    "jsonrpc": "2.0",
    "method": "eth_blockNumber",
    "params": [],
    "is-ws": false,
    "time": "2024-06-21T11:39:02Z",
    "message": "API request"
}

{
    "level": "debug",
    "component": "API",
    "IP": "127.0.0.1:33588",
    "url": "/",
    "id": "abc-123-abd", // id is a string, and it does not error out
    "jsonrpc": "2.0",
    "method": "eth_blockNumber",
    "params": [],
    "is-ws": false,
    "time": "2024-06-21T11:39:14Z",
    "message": "API request"
}

I would also bring back console writer if configured because it's very useful when running locally: https://github.com/onflow/flow-evm-gateway/pull/152/files#diff-fe44f09c4d5977b5f5eaea29170b6a0748819c9d02271746a20d81a5f3efca17L158
The change should just be to change the stderr to stdout not to change the option of console output.

This has been brought back.


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Summary by CodeRabbit

  • New Features

    • Enhanced logging capabilities with new options for log levels ('warn', 'fatal', 'panic') and log writer output ('stderr', 'console').
  • Bug Fixes

    • Improved logging configuration and usage in test helper functions, ensuring consistent log output handling.
  • Refactor

    • Replaced global log usage with specific logger instances for better logging management.
  • Chores

    • Updated Makefile to include enhanced logging options for local execution.

@m-Peter m-Peter added this to the Flow-EVM-M2 milestone Jun 21, 2024
@m-Peter m-Peter self-assigned this Jun 21, 2024
Copy link
Contributor

coderabbitai bot commented Jun 21, 2024

Walkthrough

The updates improve logging capabilities by introducing a --log-writer=console flag, enhancing the logging configuration in the Config struct, and using a logger instance instead of the global logger in various functions. These changes target better local debugging and flexible logging management within both the application and tests.

Changes

File Change Summary
Makefile Added --log-writer=console flag to start-local target to enhance logging during local execution.
config/config.go Enhanced FromFlags function to support new logWriter variable and additional log-level options.
api/server.go Replaced direct log usage with a logger instance h.logger across multiple functions.
tests/helpers.go Switched from os.Stdout to zerolog.NewConsoleWriter() for logging setup and control.
tests/integration_test.go Updated logging to use zerolog.NewConsoleWriter() instead of os.Stdout in test cases.

Sequence Diagram(s)

None required for these changes.

Assessment against linked issues

Objective (Issue #162) Addressed Explanation
Fix potential bug in logging changes where ID is not always a number. The summary does not mention specific changes addressing this particular bug.
Bring back console writer if configured, useful for local runs, by changing the stderr to stdout.
Change should maintain the option of console output.

Poem

When logs can sing and dance in sight,
The console shines with debugging light,
Errors warned and panics tamed,
In tests and code, the changes framed.
With rabbit's touch, the logs are clear,
In stdout console, they now appear.


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d5292a2 and 4bcb98e.

Files selected for processing (2)
  • Makefile (1 hunks)
  • config/config.go (3 hunks)
Additional context used
Gitleaks
Makefile

47-47: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key)

Additional comments not posted (1)
config/config.go (1)

Line range hint 92-244: Comprehensive update to configuration handling.

The updates to the FromFlags function are extensive and introduce several new configuration options, including handling for multiple log levels and writers. Ensure that these new options are documented and that their interactions are tested, especially with respect to backward compatibility.

Verification successful

Comprehensive update to configuration handling.

The updates to the FromFlags function are extensive and introduce several new configuration options, including handling for multiple log levels and writers. The new flags are properly integrated into the configuration handling, and there are tests associated with the usage of bootstrap.Start and the new flags.

  • The FromFlags function is utilized correctly in cmd/main/main.go.
  • The bootstrap.Start function properly uses the configuration options set by FromFlags.
  • Tests in tests/helpers.go and tests/integration_test.go cover the initialization and starting process, ensuring backward compatibility and proper handling of new flags.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the backward compatibility of the new configuration options.

# Test: Search for usages of the FromFlags function. Expect: Proper handling of new and old flags.
rg --type go $'FromFlags'

Length of output: 126



Script:

#!/bin/bash
# Description: Inspect the context of FromFlags usage in cmd/main/main.go.

# Display the lines around the usage of FromFlags to understand how it is being used.
rg --type go --context 10 'cfg, err := config.FromFlags()' cmd/main/main.go

Length of output: 467



Script:

#!/bin/bash
# Description: Inspect the bootstrap.Start function to verify how the configuration is used.

# Search for the definition and usage of the bootstrap.Start function.
rg --type go 'func Start' --context 10

Length of output: 1288



Script:

#!/bin/bash
# Description: Retrieve the entire implementation of the bootstrap.Start function.

# Display the complete implementation of the bootstrap.Start function.
rg --type go 'func Start' --context 50 bootstrap/bootstrap.go

Length of output: 2610



Script:

#!/bin/bash
# Description: Verify if there are any tests associated with bootstrap.Start and the new flags.

# Search for test files and functions related to bootstrap.Start.
rg --type go 'bootstrap.Start' --context 10

# Search for test files and functions related to the new flags.
rg --type go 'coa-key' --context 10
rg --type go 'coa-key-file' --context 10
rg --type go 'coa-cloud-kms-keys' --context 10

Length of output: 21559

@@ -44,4 +44,4 @@ start:
.PHONY: start-local
start-local:
rm -rf db/
go run cmd/main/main.go --flow-network-id=flow-emulator --coinbase=FACF71692421039876a5BB4F10EF7A439D8ef61E --coa-address=f8d6e0586b0a20c7 --coa-key=2619878f0e2ff438d17835c2a4561cb87b4d24d72d12ec34569acd0dd4af7c21 --coa-resource-create=true --gas-price=0
go run cmd/main/main.go --flow-network-id=flow-emulator --coinbase=FACF71692421039876a5BB4F10EF7A439D8ef61E --coa-address=f8d6e0586b0a20c7 --coa-key=2619878f0e2ff438d17835c2a4561cb87b4d24d72d12ec34569acd0dd4af7c21 --coa-resource-create=true --gas-price=0 --log-writer=console
Copy link
Contributor

Choose a reason for hiding this comment

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

Potential security issue: Exposed sensitive key in the Makefile.

The command includes a hardcoded key, which is a security risk. This should be replaced with an environment variable or a secure vault solution.

- go run cmd/main/main.go --flow-network-id=flow-emulator --coinbase=FACF71692421039876a5BB4F10EF7A439D8ef61E --coa-address=f8d6e0586b0a20c7 --coa-key=2619878f0e2ff438d17835c2a4561cb87b4d24d72d12ec34569acd0dd4af7c21 --coa-resource-create=true --gas-price=0 --log-writer=console
+ go run cmd/main/main.go --flow-network-id=flow-emulator --coinbase=$COINBASE --coa-address=$COA_ADDRESS --coa-key=$COA_KEY --coa-resource-create=true --gas-price=0 --log-writer=console
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
go run cmd/main/main.go --flow-network-id=flow-emulator --coinbase=FACF71692421039876a5BB4F10EF7A439D8ef61E --coa-address=f8d6e0586b0a20c7 --coa-key=2619878f0e2ff438d17835c2a4561cb87b4d24d72d12ec34569acd0dd4af7c21 --coa-resource-create=true --gas-price=0 --log-writer=console
go run cmd/main/main.go --flow-network-id=flow-emulator --coinbase=$COINBASE --coa-address=$COA_ADDRESS --coa-key=$COA_KEY --coa-resource-create=true --gas-price=0 --log-writer=console
Tools
Gitleaks

47-47: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key)

Comment on lines +240 to +244
if logWriter == "stderr" {
cfg.LogWriter = os.Stderr
} else {
cfg.LogWriter = zerolog.NewConsoleWriter()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Improvement in log writer configuration.

The conditional handling of the logWriter to decide between stderr and console is a good addition. Consider adding a default case for safety to handle unexpected values.

if logWriter == "stderr" {
	cfg.LogWriter = os.Stderr
} else if logWriter == "console" {
	cfg.LogWriter = zerolog.NewConsoleWriter()
} else {
	// Default case to handle unexpected values
	cfg.LogWriter = os.Stderr
	fmt.Println("Warning: Unrecognized log writer option, defaulting to stderr")
}
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
if logWriter == "stderr" {
cfg.LogWriter = os.Stderr
} else {
cfg.LogWriter = zerolog.NewConsoleWriter()
}
if logWriter == "stderr" {
cfg.LogWriter = os.Stderr
} else if logWriter == "console" {
cfg.LogWriter = zerolog.NewConsoleWriter()
} else {
// Default case to handle unexpected values
cfg.LogWriter = os.Stderr
fmt.Println("Warning: Unrecognized log writer option, defaulting to stderr")
}

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4bcb98e and 3c44425.

Files selected for processing (3)
  • api/server.go (7 hunks)
  • tests/helpers.go (4 hunks)
  • tests/integration_test.go (2 hunks)
Additional context used
Gitleaks
tests/helpers.go

51-51: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key)


54-54: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key)

Additional comments not posted (6)
tests/integration_test.go (2)

74-74: Logging Configuration Updated

The logging output has been switched to zerolog.NewConsoleWriter() to enhance readability during local development. This change is consistent with the PR's objectives.


204-204: Consistent Logging Enhancement

The update to zerolog.NewConsoleWriter() here ensures consistent logging across different test scenarios, aiding in better debugging and development.

tests/helpers.go (2)

42-44: Logger Initialization Updated

The logger has been initialized with zerolog.NewConsoleWriter(), which is suitable for local development. Additionally, the handling of the LOG_OUTPUT environment variable to toggle logging is a flexible approach.


Line range hint 51-51: False Positive: API Key Exposure

The detected API key exposure issues are false positives. The keys in question are for testing purposes only and not real API keys.

Also applies to: 54-54

api/server.go (2)

214-214: Enhanced Logging for RPC Services

Logging statements have been added to indicate when JSON-RPC over HTTP and WebSocket are enabled, providing clear runtime diagnostics.

Also applies to: 219-219


305-311: Server Shutdown Handling Enhanced

The changes improve the server's shutdown process by adding logging to indicate when the server has stopped and if the shutdown was not graceful. This is crucial for operational transparency.

@m-Peter m-Peter merged commit 5ca04ed into onflow:main Jun 24, 2024
2 checks passed
@m-Peter m-Peter deleted the logging-improvements branch June 24, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Logging improvements
2 participants