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

Integrate prometheus to evm gateway #360

Merged

Conversation

illia-malachyn
Copy link
Contributor

@illia-malachyn illia-malachyn commented Jul 17, 2024

Closes: #359

This covers the following requirements:

Measure end-to-end request/response time by method call (track the time taken from the start of a request to the return of the response for each method call to understand relative performance and user experience using percentiles).
api_request_duration_seconds_bucket
api_request_duration_seconds_bucket{resolver="SendRawTransaction"}

API requests per time interval metric
rate(api_request_duration_seconds_count{resolver="SendRawTransaction"}[5m])

API calls by API endpoint (most used to least used calls)
sort_desc(sum by(resolver) (rate(api_request_duration_seconds_count{resolver="SendRawTransaction"}[5m])))

API errors should be submitted to a counter metric
api_errors_total

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a metrics collection system to monitor API errors and request response times.
    • Added a metrics server for serving HTTP metrics to Prometheus.
    • Enhanced configuration options for specifying Prometheus configuration file paths.
    • Implemented an HTTP handler to integrate with Prometheus for monitoring HTTP request metrics.
  • Chores

    • Updated .gitignore to adjust directory exclusions.
    • Modified Makefile to clean up the metrics/data/ directory before running the application.

- Added prometheus.yml to configure prometheus server
- Added collector struct to gather metrics via it
- Added api_errors_total metric as example of usage
- Added api_request_duration metric as example of usage
Copy link
Contributor

coderabbitai bot commented Jul 17, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

These changes integrate Prometheus metrics into the EVM gateway, significantly enhancing the monitoring of API errors and request response times. Key updates include modifications to the .gitignore and Makefile, the implementation of a metrics collection framework across various API files, and the introduction of configuration support for Prometheus scraping.

Changes

Files Change Summaries
.gitignore, Makefile Updated .gitignore to include metrics/data/ and modified Makefile to remove metrics/data/ from the start-local target.
api/api.go, api/debug.go, api/server.go Integrated Prometheus metrics collection into BlockChainAPI, DebugAPI, and httpServer for enhanced error tracking and request duration measurement.
bootstrap/bootstrap.go Added a metrics server and collector for improved metrics tracking during server operations.
metrics/collector.go Established a framework for metrics collection to monitor API performance and errors.
tests/helpers.go, tests/integration_test.go Enhanced test configurations by adding the Prometheus configuration file path to support monitoring in tests.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant BlockChainAPI
    participant MetricsCollector
    participant Prometheus

    User->>BlockChainAPI: Make API Request
    BlockChainAPI->>MetricsCollector: Start Request Timer
    BlockChainAPI->>BlockChainAPI: Process Request
    alt Request Fails
        BlockChainAPI->>MetricsCollector: Report API Error
    end
    BlockChainAPI->>MetricsCollector: Stop Request Timer
    MetricsCollector->>Prometheus: Push Metrics Data
    Prometheus->>User: Provide Metrics Data
Loading

Assessment against linked issues

Objective Addressed Explanation
Integrate Prometheus to EVM gateway (#359)

Poem

In the code, metrics bloom,
Prometheus watches, no room for gloom.
Errors and times, all recorded fine,
EVM gateway shines, metrics align.
Data flows, insights show,
With Prometheus, we now know. 🐇✨


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 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.

@illia-malachyn
Copy link
Contributor Author

@sideninja Hey, I'm adding this as skeleton.

Should I create separate issue for each metrics/task as I did for this one or I can just mention #125 in every PR?

@illia-malachyn
Copy link
Contributor Author

To test this:

  • run gateway and emulator
  • do some calls to eth_blockNumber endpoint
  • run prometheus back-end via prometheus --config.file=prometheus.yml
  • open prometheus localhost:9090/graph
  • query api_request_duration

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, codebase verification and nitpick comments (1)
bootstrap/bootstrap.go (1)

34-34: Consider parsing the port number from prometheus.yml for consistency.

The port number 9091 is hardcoded in bootstrap/bootstrap.go but is also specified in metrics/prometheus.yml. To align with configuration management practices, it is recommended to parse the port number from the prometheus.yml file instead of hardcoding it.

  • bootstrap/bootstrap.go: Line 34 (metricsServer := metrics.NewServer(logger, 9091))
Analysis chain

Approved: Integration of metricsServer in the startup process.

The creation of metricsServer and its integration into the application's startup process are well-implemented. However, consider parsing the port number from prometheus.yml for consistency with configuration management practices, as previously discussed.

Also applies to: 143-143

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if port numbers are consistent across configuration files.

# Test: Search for port number usage in configuration files.
rg '9091' -- 'config/'

Length of output: 20


Script:

#!/bin/bash
# Description: Verify if port numbers are consistent across the entire codebase.

# Test: Search for port number usage in the entire codebase.
rg '9091'

Length of output: 5159

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aec1191 and 58f8264.

Files selected for processing (9)
  • .gitignore (1 hunks)
  • Makefile (1 hunks)
  • api/api.go (24 hunks)
  • api/debug.go (4 hunks)
  • bootstrap/bootstrap.go (5 hunks)
  • metrics/collector.go (1 hunks)
  • metrics/noop_collector.go (1 hunks)
  • metrics/prometheus.yml (1 hunks)
  • metrics/server.go (1 hunks)
Additional context used
yamllint
metrics/prometheus.yml

[error] 6-6: no new line character at the end of file

(new-line-at-end-of-file)

golangci-lint
api/api.go

146-146: defers: call to time.Since is not deferred

(govet)

Additional comments not posted (13)
.gitignore (1)

9-9: Approved addition of metrics data directory.

The inclusion of metrics/data/ in the .gitignore file is a good practice to avoid committing generated data to the repository.

metrics/noop_collector.go (1)

1-10: NoopCollector implementation is appropriate.

The NoopCollector serves as a placeholder or default implementation where no metrics collection is required. This implementation is clean and follows the noop pattern correctly.

Makefile (1)

47-47: Approved: Cleanup of metrics data directory in the start-local target.

The addition of rm -rf metrics/data/ ensures that Prometheus metrics data is cleaned up before each local run, which is a good practice to prevent data persistence issues during development.

metrics/server.go (3)

22-35: Approved: Implementation of the Prometheus metrics server setup.

The NewServer function correctly sets up an HTTP server dedicated to serving Prometheus metrics. The use of http.NewServeMux() and promhttp.Handler() is standard for such setups.


43-51: Approved: Robust error handling in the metrics server.

The error handling within the goroutine correctly distinguishes between a normal shutdown (http.ErrServerClosed) and other errors, which is a good practice for clear operational logging.


62-72: Approved: Proper lifecycle management using Go channels.

The Done method's implementation using a channel and a context with timeout for server shutdown is a clean and effective way to manage the server lifecycle.

api/debug.go (1)

39-39: Clarify the use of NoopCollector in error handling.

The use of NoopCollector in error handling functions across several methods raises questions about whether this is the intended permanent approach or if a more comprehensive metrics collection strategy is planned. Given the previous discussion about potentially using DefaultCollector, it might be worthwhile to revisit this decision.

Also applies to: 51-51, 72-72

Verification successful

Clarify the use of NoopCollector in error handling.

The use of NoopCollector in api/debug.go for error handling appears to be intentional to avoid collecting metrics during error handling. The DefaultCollector is defined and used within the metrics package, likely for more comprehensive metrics collection in other contexts. Therefore, the current approach using NoopCollector seems appropriate for the intended purpose.

  • api/debug.go: Uses NoopCollector for error handling.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if `DefaultCollector` or other collectors are defined and used elsewhere in the project.

# Test: Search for other collectors in the metrics package.
rg --type go 'Collector' -- 'metrics/'

Length of output: 707


Script:

#!/bin/bash
# Description: Search for instantiations and usages of NoopCollector and DefaultCollector.

# Search for instantiations and usages of NoopCollector.
rg --type go 'NoopCollector' -- 'metrics/'

# Search for instantiations and usages of DefaultCollector.
rg --type go 'DefaultCollector' -- 'metrics/'

Length of output: 577


Script:

#!/bin/bash
# Description: Search for instantiations and usages of NoopCollector and DefaultCollector in the entire codebase.

# Search for instantiations and usages of NoopCollector.
rg --type go 'NoopCollector'

# Search for instantiations and usages of DefaultCollector.
rg --type go 'DefaultCollector'

Length of output: 833

bootstrap/bootstrap.go (1)

Line range hint 318-329: Approved: Setup of metricsCollector and its integration in API services.

The creation and integration of metricsCollector into the blockchain API setup are correctly implemented, ensuring that metrics are collected throughout the application's operation.

api/api.go (5)

9-9: Added import for time.

This import is necessary for the new timing metrics functionality. Ensure it is used appropriately in the code.


23-23: Added import for metrics.

This import is crucial for enabling the metrics collection features. It appears to be correctly used in the subsequent changes.


89-89: Added collector field to BlockChainAPI struct.

This addition is essential for the metrics collection functionality. The field is properly typed as metrics.Collector, which should facilitate the integration of Prometheus metrics.


Line range hint 101-119: Modified NewBlockChainAPI constructor to include collector.

The addition of the collector parameter is necessary for initializing the metrics collection. The function correctly handles the new parameter and integrates it into the BlockChainAPI struct.


154-154: Refactored error handling to include metrics collection.

The handleError function has been modified across various methods to include a call to collector.ApiErrorOccurred(). This is a crucial change for accurate error metrics reporting.

Ensure that this pattern is consistently applied across all error handling in the project to maintain uniformity.

Also applies to: 230-230, 235-235, 252-252, 257-257, 275-275, 286-286, 304-304, 314-314, 331-331, 336-336, 341-341, 360-360, 365-365, 393-393, 399-399, 404-404, 434-434, 441-441, 460-460, 483-483, 512-512, 517-517, 571-571, 585-585, 590-590, 609-609, 615-615, 621-621, 689-689, 695-695

@illia-malachyn
Copy link
Contributor Author

Are we going to run prometheus back-end as a side car ? or is it gonna be a separate server running independently?
I want to know whether I should add this to docker/docker-compose

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 58f8264 and e144996.

Files selected for processing (6)
  • api/api.go (24 hunks)
  • bootstrap/bootstrap.go (5 hunks)
  • go.mod (2 hunks)
  • metrics/collector.go (1 hunks)
  • metrics/noop_collector.go (1 hunks)
  • metrics/prometheus.yml (1 hunks)
Files skipped from review due to trivial changes (2)
  • go.mod
  • metrics/prometheus.yml
Files skipped from review as they are similar to previous changes (4)
  • api/api.go
  • bootstrap/bootstrap.go
  • metrics/collector.go
  • metrics/noop_collector.go

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 e144996 and 2413ae5.

Files selected for processing (1)
  • metrics/prometheus.yml (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • metrics/prometheus.yml

Copy link
Contributor

@devbugging devbugging left a comment

Choose a reason for hiding this comment

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

Generally looks good, left couple of comments

With `api_request_duration` we can track variety of metrics. For instance:
- Measure end-to-end request/response time by method call
- API requests per time interval metric
- API calls by API endpoint (most used to least used calls)
* Add metrics handler for server
* Add test for prometheus config file parsing
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 c2b2abe and b86d0d2.

Files selected for processing (11)
  • api/api.go (31 hunks)
  • api/debug.go (4 hunks)
  • api/server.go (5 hunks)
  • bootstrap/bootstrap.go (7 hunks)
  • config/config.go (2 hunks)
  • metrics/collector.go (1 hunks)
  • metrics/config_test.go (1 hunks)
  • metrics/handler.go (1 hunks)
  • metrics/server.go (1 hunks)
  • tests/helpers.go (1 hunks)
  • tests/integration_test.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • metrics/collector.go
Files skipped from review as they are similar to previous changes (2)
  • bootstrap/bootstrap.go
  • metrics/server.go
Additional comments not posted (28)
metrics/handler.go (1)

17-22: LGTM!

The NewHttpHandler function is correctly implemented.

api/debug.go (4)

23-28: LGTM!

The NewDebugAPI function is correctly implemented.


Line range hint 36-41:
LGTM!

The TraceTransaction function is correctly implemented.


Line range hint 48-53:
LGTM!

The TraceBlockByNumber function is correctly implemented.


Line range hint 69-74:
LGTM!

The TraceBlockByHash function is correctly implemented.

metrics/config_test.go (2)

8-101: LGTM!

The TestReadPortFromConfigFile function is correctly implemented.


103-131: LGTM!

The TestExtractPortFromTarget function is correctly implemented.

tests/integration_test.go (2)

62-75: LGTM! Ensure Prometheus configuration file exists.

The addition of the PrometheusConfigFilePath field is correct. Ensure that the file ./metrics/prometheus.yml exists and is correctly configured.


199-212: LGTM! Ensure Prometheus configuration file exists.

The addition of the PrometheusConfigFilePath field is correct. Ensure that the file ./metrics/prometheus.yml exists and is correctly configured.

tests/helpers.go (1)

157-158: LGTM! Ensure Prometheus configuration file exists.

The addition of the PrometheusConfigFilePath field is correct. Ensure that the file ./metrics/prometheus.yml exists and is correctly configured.

api/server.go (3)

54-55: LGTM!

The addition of the collector field to the httpServer structure is correct and enhances the server's functionality by integrating a metrics collector.


Line range hint 64-82:
LGTM!

The updates to the NewHTTPServer function are correct and necessary for integrating the metrics collector.


184-185: LGTM!

The updates to the Start function are correct and enhance the server's capabilities by providing insights into its performance and usage.

config/config.go (2)

96-97: Addition of PrometheusConfigFilePath field approved.

This addition enhances the configurability of the application by allowing the specification of a Prometheus configuration file path.


160-160: Addition of flag for PrometheusConfigFilePath approved.

This addition allows users to specify the Prometheus configuration file location via command-line flags, enhancing deployment flexibility.

api/api.go (13)

24-24: Addition of metrics import approved.

This import is necessary for integrating Prometheus metrics.


90-90: Addition of collector field to BlockChainAPI struct approved.

This addition enables the collection of Prometheus metrics within the BlockChainAPI methods.


102-102: Addition of collector parameter to NewBlockChainAPI function approved.

This addition allows the BlockChainAPI to be initialized with a metrics collector.


120-120: Modification to include collector field in NewBlockChainAPI function approved.

This modification ensures that the collector field is properly initialized when creating a new BlockChainAPI instance.


152-152: Modification to include collector for error reporting in BlockNumber method approved.

This modification enhances error reporting by utilizing the collector to track API errors.


171-177: Modification to include collector for error reporting in Syncing method approved.

This modification enhances error reporting by utilizing the collector to track API errors.


200-200: Modification to include collector for error reporting in SendRawTransaction method approved.

This modification enhances error reporting by utilizing the collector to track API errors.


220-225: Modification to include collector for error reporting in GetBalance method approved.

This modification enhances error reporting by utilizing the collector to track API errors.


242-247: Modification to include collector for error reporting in GetTransactionByHash method approved.

This modification enhances error reporting by utilizing the collector to track API errors.


Line range hint 265-276:
Modification to include collector for error reporting in GetTransactionByBlockHashAndIndex method approved.

This modification enhances error reporting by utilizing the collector to track API errors.


Line range hint 294-304:
Modification to include collector for error reporting in GetTransactionByBlockNumberAndIndex method approved.

This modification enhances error reporting by utilizing the collector to track API errors.


321-331: Modification to include collector for error reporting in GetTransactionReceipt method approved.

This modification enhances error reporting by utilizing the collector to track API errors.


917-919: Modification to include collector for error reporting in handleError function approved.

This modification ensures that API errors are tracked using the collector.

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 b86d0d2 and 99c2d93.

Files selected for processing (5)
  • api/api.go (31 hunks)
  • api/debug.go (4 hunks)
  • api/server.go (5 hunks)
  • bootstrap/bootstrap.go (7 hunks)
  • go.mod (4 hunks)
Files skipped from review as they are similar to previous changes (5)
  • api/api.go
  • api/debug.go
  • api/server.go
  • bootstrap/bootstrap.go
  • go.mod

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 99c2d93 and 178d6df.

Files selected for processing (1)
  • metrics/handler.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • metrics/handler.go

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 178d6df and 57e3572.

Files selected for processing (2)
  • api/server.go (5 hunks)
  • metrics/handler.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • api/server.go
Additional comments not posted (2)
metrics/handler.go (2)

19-24: LGTM!

The NewHttpHandler function correctly initializes and returns an HttpHandler instance.


26-36: Consider logging errors before returning.

Logging errors can help in debugging and monitoring.

-  if err != nil {
-    return
-  }
+  if err != nil {
+    h.collector.LogError(err)
+    return
+  }

Likely invalid or redundant comment.

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 57e3572 and f5aa437.

Files selected for processing (2)
  • api/server.go (5 hunks)
  • metrics/handler.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • api/server.go
  • metrics/handler.go

@illia-malachyn
Copy link
Contributor Author

@sideninja Please, take a look at this again once you have time

Copy link
Contributor

@devbugging devbugging left a comment

Choose a reason for hiding this comment

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

We need to change where we capture API error

@devbugging
Copy link
Contributor

@illia-malachyn can you update base branch again sorry

@devbugging devbugging merged commit b60ed38 into onflow:main Jul 30, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Integrate prometheus to evm gateway
3 participants