Skip to content

Add xray openMetrics#183

Open
maitreyaatish wants to merge 3 commits intopeimanja:masterfrom
maitreyaatish:XrayOpenMetrics
Open

Add xray openMetrics#183
maitreyaatish wants to merge 3 commits intopeimanja:masterfrom
maitreyaatish:XrayOpenMetrics

Conversation

@maitreyaatish
Copy link
Copy Markdown
Contributor

@maitreyaatish maitreyaatish commented Oct 30, 2025

Adds JFrog Xray OpenMetrics collection while refactoring existing OpenMetrics code for better reusability.

  • Modular OpenMetrics processing: Extracted shared logic into processOpenMetrics() helper function
  • New Xray support: Added --xray.scrape-uri flag and xray optional metric
  • Metric collision prevention: Added prefixing for process_* metrics

@maitreyaatish maitreyaatish marked this pull request as ready for review October 30, 2025 18:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds JFrog Xray OpenMetrics collection support while refactoring existing OpenMetrics code for better reusability. It introduces a new --xray.scrape-uri flag and xray optional metric, along with a modular processOpenMetrics() helper function that extracts shared processing logic. The changes also implement metric prefixing for process_* metrics to prevent collisions with Prometheus internal metrics.

Key changes:

  • Refactored OpenMetrics processing into a reusable processOpenMetrics() helper function
  • Added Xray OpenMetrics support with new configuration flag and optional metric
  • Implemented metric name prefixing to avoid conflicts with internal Prometheus metrics

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
config/config.go Added xray optional metric, --xray.scrape-uri flag, and XrayScrapeURI configuration field with validation
config/config_test.go Updated test to include xray in the expected optional metrics list
collector/openMetricsHelper.go New file containing extracted sanitizeOpenMetrics() and processOpenMetrics() helper functions
collector/openMetrics.go Refactored to use processOpenMetrics() helper and added exportXrayOpenMetrics() function
collector/collector.go Integrated Xray OpenMetrics export into the main collection flow
artifactory/openmetrics.go Added FetchXrayOpenMetrics() method with URI swapping mechanism
artifactory/client.go Added XrayURI field to Client struct
README.md Updated documentation to describe new --xray.scrape-uri flag and xray optional metric

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread config/config.go Outdated
Comment thread collector/openMetricsHelper.go Outdated
Comment thread collector/openMetricsHelper.go Outdated
Comment thread README.md Outdated
Comment thread collector/collector.go Outdated
fName := family.GetName()
fHelp := family.GetHelp()
if strings.HasPrefix(fName, "process_") { // avoid conflict with promethues internal metrics
fName = source + fName
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

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

The prefix concatenation for process_* metrics results in names like artifactoryprocess_* or xrayprocess_*, missing an underscore separator. This should be fName = source + "_" + fName to produce properly formatted metric names like artifactory_process_* and xray_process_*.

Suggested change
fName = source + fName
fName = source + "_" + fName

Copilot uses AI. Check for mistakes.
Comment thread collector/openMetrics.go
func (e *Exporter) exportXrayOpenMetrics(ch chan<- prometheus.Metric) error {
openMetrics, err := e.client.FetchXrayOpenMetrics()
if err != nil {
e.logger.Error("There was an issue when try to fetch openMetrics")
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

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

Grammatical error in error message: "when try to fetch" should be "when trying to fetch". This matches the pattern used elsewhere in the codebase (e.g., artifactory/client.go:104, artifactory/federation.go:86).

Copilot uses AI. Check for mistakes.
Comment thread collector/openMetricsHelper.go Outdated
Comment thread README.md Outdated
@peimanja
Copy link
Copy Markdown
Owner

@maitreyaatish Thanks for the contribution. Please check copilot comments and address the relevant ones

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings January 5, 2026 23:45
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jan 5, 2026

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread config/config.go
if err != nil {
return nil, err
}
} else if optMetrics.Xray {
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

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

The validation logic has a flaw: xrayScrapeURI is defined with a default value of "http://localhost:8081/xray" at line 21, which means it will never be an empty string. Therefore, the else condition at line 174 checking if optMetrics.Xray is true will never execute. This prevents the proper validation that requires users to explicitly set the Xray scrape URI when enabling the xray optional metric. Consider removing the default value or adjusting the validation logic to allow the default only when the xray metric is not explicitly enabled.

Suggested change
} else if optMetrics.Xray {
}
if optMetrics.Xray && (*xrayScrapeURI == "" || *xrayScrapeURI == "http://localhost:8081/xray") {

Copilot uses AI. Check for mistakes.
Comment thread collector/openMetrics.go
func (e *Exporter) exportXrayOpenMetrics(ch chan<- prometheus.Metric) error {
openMetrics, err := e.client.FetchXrayOpenMetrics()
if err != nil {
e.logger.Error("There was an issue when try to fetch openMetrics")
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

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

Grammar error: "when try to fetch" should be "when trying to fetch". This error message appears in both exportOpenMetrics and exportXrayOpenMetrics functions.

Copilot uses AI. Check for mistakes.
Comment thread collector/collector.go
if err != nil {
return false
}

Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

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

There's an unnecessary blank line between the closing brace and the next statement. Remove the extra blank line at line 217 for consistency with the rest of the codebase.

Suggested change

Copilot uses AI. Check for mistakes.
Comment on lines +43 to +44
if err.(*APIError).status == 404 {
return openMetrics, nil
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

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

Unsafe type assertion: if the error is not of type *APIError, this will cause a panic. Use a type assertion with a safety check instead, such as: if apiErr, ok := err.(*APIError); ok && apiErr.status == 404. This is particularly important because FetchHTTP can return other error types like UnmarshalError or network errors.

Copilot uses AI. Check for mistakes.
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.

3 participants