Skip to content

feat(collector): prometheus input #2425

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

Merged
merged 1 commit into from
Mar 12, 2025
Merged

feat(collector): prometheus input #2425

merged 1 commit into from
Mar 12, 2025

Conversation

tothandras
Copy link
Contributor

@tothandras tothandras commented Mar 12, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a Prometheus metrics input that collects metrics using user-defined queries on a scheduled basis.
  • Chores

    • Updated the AI integration’s HTTP communication defaults by replacing environment variable configurations with fixed default values for more consistent behavior.

@tothandras tothandras added the release-note/feature Release note: Exciting New Features label Mar 12, 2025
@tothandras tothandras requested a review from a team as a code owner March 12, 2025 15:53
Copy link

coderabbitai bot commented Mar 12, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a new Prometheus metrics input in the collector package. A new file is added that implements metrics collection using PromQL with a comprehensive setup including configuration, scheduling, query execution, and result handling. Additionally, the pull request updates the HTTP configuration in another file by replacing environment variable references with fixed default values for timeout and retry parameters.

Changes

File(s) Change Summary
collector/.../prometheus.go Added a new file implementing Prometheus metrics input. Introduces configuration specification, scheduling based on cron expressions, query scraping, thread-safe result storage, and methods for connection, batch reading, and closing the input.
collector/.../run_ai.go Modified HTTP configuration in the runAI input by replacing environment variable references with hardcoded default values for timeout, retry count, retry wait time, and maximum retry wait time.
✨ Finishing Touches
  • 📝 Generate Docstrings

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

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 (3)
collector/benthos/input/run_ai.go (1)

110-113: Revisit static HTTP configuration.

Replacing environment variable references with hardcoded values restricts the ability to customize or tune these parameters in diverse deployment environments. Consider supporting both a default value and an optional environment-based override to preserve flexibility.

collector/benthos/input/prometheus.go (2)

17-45: Use more specific prefix for field constants if multiple input plugins coexist.

While these constants are self-explanatory, adding a distinguishing prefix can reduce collisions in larger projects with many field definitions. For instance, consider using promFieldURL or promFieldSchedule.


174-305: Evaluate memory usage in repeated scraping scenarios.

Each scrape result is fully retained in in.store until read. Under high query frequency or large result sets, memory usage could spike. If real-time processing or streaming is possible, consider streaming partial results or purging them more aggressively.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d2ec8ae and 14f93d0.

📒 Files selected for processing (2)
  • collector/benthos/input/prometheus.go (1 hunks)
  • collector/benthos/input/run_ai.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: CI
  • GitHub Check: Analyze (go)
🔇 Additional comments (4)
collector/benthos/input/prometheus.go (4)

1-16: Imports and package initialization look good.

The chosen imports (client_golang and robfig/cron) are appropriate for Prometheus API interactions and scheduling. The overall structure sets a clear foundation for the new input component.


57-64: Registration process is correct.

Registering the new prometheus input is consistent with how Benthos plugin registration typically occurs. The error panic ensures immediate visibility for misconfiguration.


66-89: Data structures look appropriate and self-descriptive.

PromQuery and QueryResult clearly separate logical concerns of naming and storing intermediate PromQL vector data, aiding future maintenance and readability.


91-172: Validate scheduling interval logic for edge cases.

The code assumes at least two consecutive cron occurrences to compute an interval. For custom or irregular schedules (e.g., one-time or complex cron patterns), this might yield unexpected intervals. Consider adding guards or documentation clarifications about unsupported cron expressions.

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)
collector/benthos/input/run_ai.go (1)

110-113: Evaluate preserving environment variable references.

Replacing environment variables with hardcoded values can reduce configuration flexibility for deployments that rely on environment-based overrides. Consider reverting to an environment-based approach or adding an override mechanism to retain dynamic control over these parameters.

Here's an example approach:

 http:
-  timeout: 30s
-  retry_count: 1
-  retry_wait_time: 100ms
-  retry_max_wait_time: 1s
+  timeout: "${RUNAI_HTTP_TIMEOUT:30s}"
+  retry_count: "${RUNAI_HTTP_RETRY_COUNT:1}"
+  retry_wait_time: "${RUNAI_HTTP_RETRY_WAIT_TIME:100ms}"
+  retry_max_wait_time: "${RUNAI_HTTP_RETRY_MAX_WAIT_TIME:1s}"
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d2ec8ae and 14f93d0.

📒 Files selected for processing (2)
  • collector/benthos/input/prometheus.go (1 hunks)
  • collector/benthos/input/run_ai.go (1 hunks)
🔇 Additional comments (3)
collector/benthos/input/prometheus.go (3)

23-55: Configuration specification is well-defined.

Your prometheusInputConfig() function uses a clear structure with object fields for queries and an instructive example. This approach helps users understand how to configure multiple PromQL queries. Great job.


91-172: Efficient initialization and schedule-based interval setup.

In newPrometheusInput(), using two subsequent cron occurrences to calculate the interval is a clever technique. This ensures that the input aligns precisely with changing schedules and avoids large or unexpected intervals if the cron expression is updated.


174-305: Robust concurrency and retry mechanism.

The sequence of scrape(), Connect(), ReadBatch(), and Close() uses a mutex to ensure thread safety, and any failed batch is re-added to the store for reprocessing. This design effectively addresses concurrency concerns and fault tolerance, with good logging for errors and warnings.

@tothandras tothandras enabled auto-merge (squash) March 12, 2025 16:58
Comment on lines +211 to +213
in.mu.Lock()
in.store[t] = results
in.mu.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
in.mu.Lock()
in.store[t] = results
in.mu.Unlock()
in.mu.Lock()
defer in.mu.Unlock()
in.store[t] = results

@tothandras tothandras merged commit 13a1556 into main Mar 12, 2025
26 checks passed
@tothandras tothandras deleted the feat/promql-collector branch March 12, 2025 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/feature Release note: Exciting New Features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants