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

feat(observability): add prometheus metrics #37

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

k-yang
Copy link
Member

@k-yang k-yang commented Nov 24, 2023

This PR defines two metrics that are used to monitor the performance and behavior of the pricefeeder application.

pricefeeder_voting_periods_total

This is a counter metric that keeps track of the total number of voting periods the pricefeeder has participated in. A voting period is a specific duration during which votes are collected. This metric is incremented each time the pricefeeder participates in a voting period.

var NumVotingPeriods = promauto.NewCounter(prometheus.CounterOpts{
    Name: "pricefeeder_voting_periods_total",
    Help: "The total number of voting periods this pricefeeder has participated in",
})

pricefeeder_price_source_latency_seconds

This is a histogram metric that measures the latency of querying a price source in seconds. The latency is the amount of time it takes for the pricefeeder to receive a response after it has sent a request to the price source. This metric is useful for monitoring the performance of the price sources and identifying any potential issues or delays.

var PriceSourceLatency = promauto.NewHistogramVec(prometheus.HistogramOpts{
    Name:    "pricefeeder_price_source_latency_seconds",
    Help:    "The latency of querying a price source in seconds",
    Buckets: prometheus.DefBuckets,
}, []string{"source"})

The source label is used to differentiate the latencies of different price sources.

Summary by CodeRabbit

  • New Features

    • Introduced Prometheus metrics for monitoring the application's performance.
    • Added an HTTP server to expose a /metrics endpoint for Prometheus scraping.
  • Enhancements

    • Improved the SendPrices function to increment a counter for tracking voting periods.
    • Enhanced price source handling by passing source type information to the NewTickSource function.
  • Documentation

    • Updated the README to include information about new monitoring metrics.
  • Tests

    • Modified test cases to accommodate the new parameter in the NewTickSource function calls.

@k-yang k-yang requested a review from a team as a code owner November 24, 2023 15:06
Copy link
Contributor

coderabbitai bot commented Nov 24, 2023

It seems like you've provided a comprehensive summary of changes and a delightful poem! Is there anything else you'd like to add or modify?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

@k-yang k-yang enabled auto-merge (squash) November 24, 2023 15:06
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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 80f4212 and 110b81e.
Files selected for processing (7)
  • cmd/feeder/main.go (2 hunks)
  • feeder/priceposter/client.go (2 hunks)
  • feeder/priceprovider/priceprovider.go (1 hunks)
  • feeder/priceprovider/sources/tick_source.go (4 hunks)
  • feeder/priceprovider/sources/tick_source_test.go (3 hunks)
  • metrics/README.md (1 hunks)
  • metrics/metrics.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • metrics/README.md
Additional comments: 11
feeder/priceposter/client.go (2)
  • 7-13:
    The import of the metrics package is correctly placed and follows the convention of grouping third-party imports together.

  • 122-122:
    The addition of the metric increment operation is correctly implemented and follows the logic described in the summary.

feeder/priceprovider/priceprovider.go (1)
  • 39-51:

The changes to the NewPriceProvider function correctly pass the source type as an additional argument to the NewTickSource function for each case. This aligns with the implementation of observability features described in the summary.

feeder/priceprovider/sources/tick_source.go (4)
  • 4-10: The import of the metrics package is correctly added to support the new Prometheus metrics functionality.

  • 4-22: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [19-28]

The NewTickSource function has been correctly updated to accept a new source parameter and initialize the source field in the TickSource struct.

  • 43-47: The source field has been correctly added to the TickSource struct to store the source identifier for the metrics.

  • 60-67: The metric recording for PriceSourceLatency is correctly implemented within the TickSource loop, capturing the duration of the fetchPrices call and observing it with the appropriate label.

feeder/priceprovider/sources/tick_source_test.go (3)
  • 29-32: The test case for NewTickSource correctly passes the "test_source" string identifier as per the changes described in the summary. This ensures that the test cases are aligned with the updated function signature.

  • 64-66: The test case for NewTickSource within the "price update dropped due to shutdown" scenario has been updated to include the "test_source" string identifier. This maintains consistency with the changes made to the NewTickSource function.

  • 83-85: The test case for NewTickSource within the "logs on price update errors" scenario has been updated to include the "test_source" string identifier. This maintains consistency with the changes made to the NewTickSource function.

metrics/metrics.go (1)
  • 1-17:

The implementation of Prometheus metrics using the promauto package is correct, and the metrics are well-defined with appropriate names and help descriptions. The use of default buckets for the histogram is acceptable for initial implementation but may need to be revisited for fine-tuning based on actual latency distributions observed.

Comment on lines 54 to 55
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":3000", nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

The code to start the HTTP server for Prometheus metrics is not wrapped in a goroutine, which means that the ListenAndServe call will block the main function from proceeding. This could prevent the handleInterrupt function from working as intended since it's supposed to listen for an interrupt signal to gracefully shut down the feeder.

To fix this, the HTTP server should be started in a separate goroutine to allow the main program to continue running and be able to handle interrupts.

- http.ListenAndServe(":3000", nil)
+ go func() {
+     if err := http.ListenAndServe(":3000", nil); err != nil {
+         logger.Error().Err(err).Msg("Metrics HTTP server failed")
+     }
+ }()

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

Suggested change
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":3000", nil)
http.Handle("/metrics", promhttp.Handler())
go func() {
if err := http.ListenAndServe(":3000", nil); err != nil {
logger.Error().Err(err).Msg("Metrics HTTP server failed")
}
}()

Wrap the `http.ListenAndServe` in a goroutine to prevent conflicts with the interrupt handler
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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 110b81e and 187375d.
Files selected for processing (1)
  • cmd/feeder/main.go (2 hunks)
Additional comments: 2
cmd/feeder/main.go (2)
  • 5-10:
    The import statements for the HTTP server functionality and Prometheus client are correctly added.

  • 61-61:
    Clarify the purpose of the select{} statement blocking indefinitely, as it may not be obvious to maintainers why this is needed.

Comment on lines +54 to +59
http.Handle("/metrics", promhttp.Handler())
go func() {
if err := http.ListenAndServe(":3000", nil); err != nil {
logger.Error().Err(err).Msg("Metrics HTTP server failed")
}
}()
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider logging a message when the HTTP server for metrics starts successfully to provide clear feedback on the server status.

@Unique-Divine Unique-Divine added the type: enhancement New feature or request label Apr 30, 2024
@k-yang k-yang disabled auto-merge June 7, 2024 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
Status: ➡️ Pending Review/Merge
Development

Successfully merging this pull request may close these issues.

2 participants