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

prometheus: add scrapeInterval #595

Merged
merged 5 commits into from
Feb 6, 2025
Merged

prometheus: add scrapeInterval #595

merged 5 commits into from
Feb 6, 2025

Conversation

evenh
Copy link
Member

@evenh evenh commented Feb 4, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a configurable scrape interval for Prometheus monitoring with a default of 60 seconds, now available in application and job configurations.
    • Added new resource definitions for enhanced monitoring setups, including customizable interval settings.
  • Refactor

    • Streamlined internal resource generation and logging processes to improve monitoring configuration.
  • Tests

    • Expanded test coverage to validate custom interval configurations and ensure proper handling of both valid and invalid settings.

@evenh evenh requested a review from a team as a code owner February 4, 2025 15:30
Copy link
Contributor

coderabbitai bot commented Feb 4, 2025

Walkthrough

This update introduces a new field, ScrapeInterval, to the Prometheus configuration in the API and CRD definitions. It updates monitoring configurations across ServiceMonitor, PodMonitor, and SKIPJob YAML files by standardizing scraping intervals, generally setting them to "60s" or custom values. The changes also adjust import statements and logging settings in controllers and resource generators, shifting resource generation logic from ServiceMonitor/PodMonitor to Prometheus-based functions. Additional tests have been added to validate both valid and invalid custom interval configurations.

Changes

File(s) Change Summary
api/v1alpha1/application_types.go
config/crd/skiperator.kartverket.no_applications.yaml
config/crd/skiperator.kartverket.no_skipjobs.yaml
Added a new scrapeInterval (or ScrapeInterval) field with validation rules to PrometheusConfig and CRDs to standardize scrape intervals.
tests/application/service-monitor/*.yaml
(application-istio-assert.yaml,
application-simple-assert.yaml,
patch-application-allowall-assert.yaml,
application-simple-custom-interval(.yaml/invalid).yaml,
chainsaw-test.yaml)
Updated or added interval fields, typically set to "60s" or custom values (e.g. "90s"), and applied minor formatting fixes in ServiceMonitor and Application configurations.
tests/skipjob/podmonitor/*.yaml
(patch-skipjob-remove-monitoring-error.yaml,
patch-skipjob-with-monitoring-assert.yaml,
skipjob-with-monitoring-assert.yaml,
chainsaw-test.yaml,
patch-skipjob-with-monitoring-custom-interval(.yaml/assert).yaml)
Adjusted interval values—from "30s" to "60s" or to custom values like "100s"—and introduced a new Job and PodMonitor resource for SKIPJob monitoring.
cmd/skiperator/main.go
internal/controllers/application.go
internal/controllers/skipjob.go
Modified logging configuration by adding DestWriter: os.Stdout, updated import references, and shifted resource generation calls from ServiceMonitor/PodMonitor to Prometheus functions.
pkg/resourcegenerator/prometheus/*.go
(pod_monitor.go,
prometheus.go,
service_monitor.go)
Renamed packages (from podmonitor/servicemonitor to prometheus), restructured generator functions (e.g. replacing Generate with an init function and new generation helpers), and introduced dedicated Prometheus resource generation logic.

Sequence Diagram(s)

sequenceDiagram
    participant AR as ApplicationReconciler
    participant PG as Prometheus Generator
    participant K8s as Kubernetes API
    AR->>PG: Call Generate(reconciliation)
    PG->>K8s: Create/Update Prometheus resource (ServiceMonitor/PodMonitor)
    K8s-->>PG: Return resource status
    PG-->>AR: Generation complete
Loading
sequenceDiagram
    participant SR as SKIPJobReconciler
    participant PG as Prometheus Generator
    participant K8s as Kubernetes API
    SR->>PG: Invoke generateForSkipJob(reconciliation)
    PG->>K8s: Create/Update monitoring resource for SKIPJob
    K8s-->>PG: Acknowledge update
    PG-->>SR: Return completion status
Loading

Possibly related PRs

Suggested reviewers

  • martinhny
  • SnorreSelmer
  • berg-thom
  • omaen

Poem

I'm a rabbit with a code-filled day,
Hopping through YAML in a gleeful way,
New fields and intervals hop into sight,
Prometheus monitors by day and night.
Logs twinkle like stars, so bright and keen,
Hoppity-hop—I'm living the dream!
🐇✨

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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>, 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. (Beta)
  • @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

github-actions bot commented Feb 4, 2025

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Files

@evenh evenh marked this pull request as draft February 4, 2025 15:30
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

🧹 Nitpick comments (2)
pkg/resourcegenerator/podmonitor/pod_monitor.go (1)

4-6: Remove unused import.

The strings package is imported but not used in this file.

-import (
-	"fmt"
-	"strings"
-
-	skiperatorv1alpha1 "github.com/kartverket/skiperator/api/v1alpha1"
+import (
+	"fmt"
+	skiperatorv1alpha1 "github.com/kartverket/skiperator/api/v1alpha1"
tests/application/service-monitor/application-simple-assert.yaml (1)

104-105: Refined formatting for NetworkPolicy ports.
The update to the ports section in the NetworkPolicy—ensuring the - port: istio-metrics entry is clearly defined—enhances readability and maintains consistency.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bd6bdb5 and f627e9a.

📒 Files selected for processing (9)
  • api/v1alpha1/application_types.go (2 hunks)
  • pkg/resourcegenerator/podmonitor/pod_monitor.go (3 hunks)
  • pkg/resourcegenerator/servicemonitor/service_monitor.go (3 hunks)
  • tests/application/service-monitor/application-istio-assert.yaml (1 hunks)
  • tests/application/service-monitor/application-simple-assert.yaml (2 hunks)
  • tests/application/service-monitor/patch-application-allowall-assert.yaml (1 hunks)
  • tests/skipjob/podmonitor/patch-skipjob-remove-monitoring-error.yaml (1 hunks)
  • tests/skipjob/podmonitor/patch-skipjob-with-monitoring-assert.yaml (1 hunks)
  • tests/skipjob/podmonitor/skipjob-with-monitoring-assert.yaml (1 hunks)
🧰 Additional context used
🪛 GitHub Check: CRD and ClusterRole
api/v1alpha1/application_types.go

[failure] 341-341:
extra arguments provided: "s" (at :1:3)


[failure] 341-341:
extra arguments provided: "s" (at :1:3)

🪛 GitHub Check: Build and run tests
api/v1alpha1/application_types.go

[failure] 341-341:
extra arguments provided: "s" (at :1:3)

🪛 GitHub Actions: Test
api/v1alpha1/application_types.go

[error] 341-341: extra arguments provided: 's' (at :1:3)

🪛 GitHub Actions: Build and Deploy Skiperator
api/v1alpha1/application_types.go

[error] 341-341: Extra arguments provided: 's'.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (go)
🔇 Additional comments (10)
pkg/resourcegenerator/podmonitor/pod_monitor.go (2)

29-32: LGTM! Good error handling for scrape interval.

The error handling for parsing the scrape interval is well implemented.


51-51: LGTM! Proper interval configuration.

The scrape interval is correctly configured in the PodMonitor spec.

pkg/resourcegenerator/servicemonitor/service_monitor.go (2)

38-41: LGTM! Good error handling for scrape interval.

The error handling for parsing the scrape interval is well implemented.


54-54: LGTM! Proper interval configuration.

The scrape interval is correctly configured in the ServiceMonitor spec.

tests/skipjob/podmonitor/patch-skipjob-with-monitoring-assert.yaml (1)

14-14: LGTM! Test configuration updated correctly.

The test configuration correctly includes the new scrape interval setting.

tests/application/service-monitor/patch-application-allowall-assert.yaml (1)

12-12: Added scrape interval in ServiceMonitor endpoints.
The new interval: "30s" field is correctly added under the endpoints section, explicitly defining the Prometheus scrape frequency. This change is consistent with the new ScrapeInterval configuration and supports the PR objectives.

tests/skipjob/podmonitor/patch-skipjob-remove-monitoring-error.yaml (1)

14-14: Added scrape interval in PodMonitor configuration.
The addition of interval: "30s" under podMetricsEndpoints ensures that metrics for this PodMonitor are scraped at the desired frequency. The change is straightforward and aligns with similar updates in other resources.

tests/skipjob/podmonitor/skipjob-with-monitoring-assert.yaml (1)

58-58: Explicitly defined scrape interval in PodMonitor.
The inclusion of interval: "30s" within the PodMonitor’s specification standardizes the metric collection frequency across configurations. This is a clear and well-integrated implementation in line with the overall monitoring enhancements.

tests/application/service-monitor/application-simple-assert.yaml (1)

60-60: Scrape interval added in ServiceMonitor endpoints.
The new interval: "30s" setting in the ServiceMonitor's endpoints section correctly establishes the desired metric scraping interval. It harmonizes with the overall configuration changes introduced in the PR.

tests/application/service-monitor/application-istio-assert.yaml (1)

71-71: Scrape interval specified for ServiceMonitor endpoints.
The addition of interval: "30s" in the ServiceMonitor configuration effectively sets the metrics scraping interval and is consistent with the changes across the test files. This update meets the PR objective and is well implemented.

api/v1alpha1/application_types.go Outdated Show resolved Hide resolved
@evenh evenh force-pushed the customizable-scrape-interval branch from f627e9a to ee00d15 Compare February 4, 2025 20:52
@evenh evenh force-pushed the customizable-scrape-interval branch from 4094117 to 8b0c12b Compare February 5, 2025 13:08
Copy link
Contributor

@Griznah Griznah left a comment

Choose a reason for hiding this comment

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

This is beyond my knowledge-set

This offloads the validation to the API server
@evenh evenh marked this pull request as ready for review February 6, 2025 13:22
@evenh
Copy link
Member Author

evenh commented Feb 6, 2025

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Feb 6, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

🔭 Outside diff range comments (1)
pkg/resourcegenerator/prometheus/pod_monitor.go (1)

64-64: Correct the log message to reflect the generated resource

The log message incorrectly states "Finished generating configmap" instead of "Finished generating podmonitor". This could cause confusion when debugging or reviewing logs.

Apply this diff to correct the log message:

-	ctxLog.Debug("Finished generating configmap", "name", skipJob.GetName())
+	ctxLog.Debug("Finished generating podmonitor", "name", skipJob.GetName())
🧹 Nitpick comments (4)
tests/application/service-monitor/application-simple-custom-interval.yaml (1)

1-19: Approved: Application and Namespace configuration with custom scrapeInterval.
The configuration clearly defines a Namespace and an Application resource with a custom scrapeInterval set to 90s. For consistency, consider quoting the interval (e.g., "90s") as seen in other files.

tests/skipjob/podmonitor/patch-skipjob-with-monitoring-custom-interval-assert.yaml (1)

1-18: Approved: PodMonitor configuration for SKIPJob.
The PodMonitor resource is properly set up with the correct endpoint details and uses an interval of "100s"—consistent with the SKIPJob configuration. As a minor note, you might review the quoting style across files for uniformity.

tests/application/service-monitor/application-simple-custom-interval-assert.yaml (1)

1-24: Approved: ServiceMonitor resource with custom scrapeInterval and metric relabelings.
This ServiceMonitor is configured to match the application’s custom interval ("90s") and includes a well-defined metricRelabelings section. For consistency purposes, review the usage of quotes for interval values across configurations.

config/crd/skiperator.kartverket.no_applications.yaml (1)

950-967: New Field: ScrapeInterval in Prometheus Section
The addition of the scrapeInterval field in the Prometheus section follows the same pattern as in the SKIPJob CRD, ensuring consistency across resource definitions. The configuration (default value, type, and validations) is clearly specified. The validations, which utilize regex matching and numerical boundary checks, ensure that only values meeting the criteria are accepted.

Consider adding an inline comment or an example (e.g., "e.g., '30s' or '2m'") near the field description to guide CRD users regarding accepted formats.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f627e9a and e6f6379.

📒 Files selected for processing (22)
  • api/v1alpha1/application_types.go (2 hunks)
  • cmd/skiperator/main.go (1 hunks)
  • config/crd/skiperator.kartverket.no_applications.yaml (1 hunks)
  • config/crd/skiperator.kartverket.no_skipjobs.yaml (1 hunks)
  • internal/controllers/application.go (2 hunks)
  • internal/controllers/skipjob.go (2 hunks)
  • pkg/resourcegenerator/prometheus/pod_monitor.go (2 hunks)
  • pkg/resourcegenerator/prometheus/prometheus.go (1 hunks)
  • pkg/resourcegenerator/prometheus/service_monitor.go (2 hunks)
  • tests/application/service-monitor/application-istio-assert.yaml (1 hunks)
  • tests/application/service-monitor/application-simple-assert.yaml (2 hunks)
  • tests/application/service-monitor/application-simple-custom-interval-assert.yaml (1 hunks)
  • tests/application/service-monitor/application-simple-custom-interval-invalid.yaml (1 hunks)
  • tests/application/service-monitor/application-simple-custom-interval.yaml (1 hunks)
  • tests/application/service-monitor/chainsaw-test.yaml (1 hunks)
  • tests/application/service-monitor/patch-application-allowall-assert.yaml (1 hunks)
  • tests/skipjob/podmonitor/chainsaw-test.yaml (1 hunks)
  • tests/skipjob/podmonitor/patch-skipjob-remove-monitoring-error.yaml (1 hunks)
  • tests/skipjob/podmonitor/patch-skipjob-with-monitoring-assert.yaml (1 hunks)
  • tests/skipjob/podmonitor/patch-skipjob-with-monitoring-custom-interval-assert.yaml (1 hunks)
  • tests/skipjob/podmonitor/patch-skipjob-with-monitoring-custom-interval.yaml (1 hunks)
  • tests/skipjob/podmonitor/skipjob-with-monitoring-assert.yaml (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • tests/application/service-monitor/application-simple-custom-interval-invalid.yaml
🚧 Files skipped from review as they are similar to previous changes (6)
  • tests/skipjob/podmonitor/patch-skipjob-with-monitoring-assert.yaml
  • tests/application/service-monitor/application-istio-assert.yaml
  • tests/application/service-monitor/patch-application-allowall-assert.yaml
  • tests/application/service-monitor/application-simple-assert.yaml
  • tests/skipjob/podmonitor/skipjob-with-monitoring-assert.yaml
  • tests/skipjob/podmonitor/patch-skipjob-remove-monitoring-error.yaml
🔇 Additional comments (14)
tests/skipjob/podmonitor/chainsaw-test.yaml (1)

21-25: Custom Scrape Interval Test Step Added

The new test step applies the patch file patch-skipjob-with-monitoring-custom-interval.yaml and asserts the changes using patch-skipjob-with-monitoring-custom-interval-assert.yaml. This explicitly tests the new scrapeInterval configuration and aligns with the PR objectives to introduce flexible monitoring intervals. The YAML structure and indentation are correct, and the new test step integrates well with the existing test cases.

pkg/resourcegenerator/prometheus/service_monitor.go (3)

1-1: Package name change is appropriate

Renaming the package to prometheus accurately reflects its broader responsibility in handling Prometheus resources, enhancing code organization and readability.


14-16: Generator registration is correctly implemented

The init function properly registers generateForApplication with the multiGenerator, ensuring that the resource generation for applications is included during reconciliation.


52-52: Scrape interval correctly set using getScrapeInterval

The scrape interval for Prometheus is appropriately set using the getScrapeInterval function, allowing for customizable intervals based on the application's Prometheus configuration.

pkg/resourcegenerator/prometheus/prometheus.go (2)

19-25: Verify ScrapeInterval validation to ensure only sane values are accepted

Currently, getScrapeInterval returns pc.ScrapeInterval directly without validating the input. To prevent users from setting unconventional scrape intervals like "23s", please verify that the ScrapeInterval is validated to accept only standard and reasonable intervals such as "10s", "15s", "30s", or "1m".


1-26: Prometheus resource generation implementation is well-structured

The new prometheus package provides a clear and modular approach to generating Prometheus resources. The Generate and getScrapeInterval functions are well-implemented, improving maintainability.

cmd/skiperator/main.go (1)

58-58: LGTM! Good logging practice.

Explicitly setting DestWriter to os.Stdout makes the logging behavior clear and predictable.

internal/controllers/skipjob.go (1)

17-17: LGTM! Consistent resource generation update.

The change from podmonitor to prometheus package aligns with the PR's objective and maintains consistency with similar changes in application.go.

Also applies to: 166-166

internal/controllers/application.go (1)

27-27: LGTM! Consistent resource generation update.

The change from servicemonitor to prometheus package aligns with the PR's objective and maintains consistency with similar changes in skipjob.go.

Also applies to: 210-210

api/v1alpha1/application_types.go (2)

6-6: LGTM! Required import for timestamps.

The time import is correctly added for timestamp handling in FillDefaultsStatus.


338-346: LGTM! Well-structured scrape interval configuration.

The ScrapeInterval field is well-designed with:

  • Clear validation rules for minimum intervals (15s or 1m)
  • Values in seconds must be divisible by 5
  • Sensible default of "60s"
  • Proper pattern validation that addresses previous pipeline failures
tests/skipjob/podmonitor/patch-skipjob-with-monitoring-custom-interval.yaml (1)

1-18: Approved: New SKIPJob resource with monitoring settings.
The file introduces a SKIPJob resource that includes a custom scrapeInterval of "100s". Please verify that the value "100s" conforms to the CRD validation rules (e.g., meeting the minimum interval criteria).

tests/application/service-monitor/chainsaw-test.yaml (1)

26-32: Approved: Enhanced testing steps for custom interval configurations.
The newly added try block correctly applies the custom interval configuration, asserts its expected state, and handles an error case via the invalid configuration file. Please verify that the referenced files (application-simple-custom-interval.yaml, application-simple-custom-interval-assert.yaml, and application-simple-custom-interval-invalid.yaml) exist in the expected locations.

config/crd/skiperator.kartverket.no_skipjobs.yaml (1)

883-900: New Field: ScrapeInterval Validation and Configuration
The newly added scrapeInterval field is well integrated into the Prometheus section. The default value of 60s and the detailed validation rules—checking that the value is either empty/default or follows the <number>s or <number>m format with minimum values (15s for seconds and 1m for minutes) and divisibility constraints—provide robust configuration against invalid inputs. The error messages are clear and informative.

@evenh
Copy link
Member Author

evenh commented Feb 6, 2025

When using CEL expressions the validation rules are now checked in the API server before eventually hitting skiperator for valid manifests
Screenshot 2025-02-06 at 14 33 30

@evenh evenh requested review from okpedersen, omaen and BardOve February 6, 2025 13:37
Copy link
Contributor

@martinhny martinhny left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

@evenh evenh merged commit af4f66c into main Feb 6, 2025
10 checks passed
@evenh evenh deleted the customizable-scrape-interval branch February 6, 2025 14:37
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.

4 participants