generated from cobaltcore-dev/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Add committed resource metrics and alerts #611
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
+511
−96
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
internal/scheduling/reservations/commitments/api_change_commitments_metrics.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright SAP SE | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package commitments | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/sapcc/go-api-declarations/liquid" | ||
| ) | ||
|
|
||
| // recordMetrics records Prometheus metrics for a change commitments request. | ||
| func (api *HTTPAPI) recordMetrics(req liquid.CommitmentChangeRequest, resp liquid.CommitmentChangeResponse, statusCode int, startTime time.Time) { | ||
| duration := time.Since(startTime).Seconds() | ||
| statusCodeStr := strconv.Itoa(statusCode) | ||
|
|
||
| // Record request counter and duration | ||
| api.monitor.requestCounter.WithLabelValues(statusCodeStr).Inc() | ||
| api.monitor.requestDuration.WithLabelValues(statusCodeStr).Observe(duration) | ||
|
|
||
| // Count total commitment changes in the request | ||
| commitmentCount := countCommitments(req) | ||
|
|
||
| // Determine result based on response | ||
| result := "success" | ||
| if resp.RejectionReason != "" { | ||
| result = "rejected" | ||
| } | ||
|
|
||
| // Record commitment changes counter | ||
| api.monitor.commitmentChanges.WithLabelValues(result).Add(float64(commitmentCount)) | ||
| } | ||
|
|
||
| // countCommitments counts the total number of commitments in a request. | ||
| func countCommitments(req liquid.CommitmentChangeRequest) int { | ||
| count := 0 | ||
| for _, projectChanges := range req.ByProject { | ||
| for _, resourceChanges := range projectChanges.ByResource { | ||
| count += len(resourceChanges.Commitments) | ||
| } | ||
| } | ||
| return count | ||
| } |
54 changes: 54 additions & 0 deletions
54
internal/scheduling/reservations/commitments/api_change_commitments_monitor.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright SAP SE | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package commitments | ||
|
|
||
| import ( | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| // ChangeCommitmentsAPIMonitor provides metrics for the CR change API. | ||
| type ChangeCommitmentsAPIMonitor struct { | ||
| requestCounter *prometheus.CounterVec | ||
| requestDuration *prometheus.HistogramVec | ||
| commitmentChanges *prometheus.CounterVec | ||
| timeouts prometheus.Counter | ||
| } | ||
|
|
||
| // NewChangeCommitmentsAPIMonitor creates a new monitor with Prometheus metrics. | ||
| func NewChangeCommitmentsAPIMonitor() ChangeCommitmentsAPIMonitor { | ||
| return ChangeCommitmentsAPIMonitor{ | ||
| requestCounter: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "cortex_committed_resource_change_api_requests_total", | ||
| Help: "Total number of committed resource change API requests by HTTP status code", | ||
| }, []string{"status_code"}), | ||
| requestDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
| Name: "cortex_committed_resource_change_api_request_duration_seconds", | ||
| Help: "Duration of committed resource change API requests in seconds by HTTP status code", | ||
| }, []string{"status_code"}), | ||
| commitmentChanges: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "cortex_committed_resource_change_api_commitment_changes_total", | ||
| Help: "Total number of commitment changes processed by result", | ||
| }, []string{"result"}), | ||
| timeouts: prometheus.NewCounter(prometheus.CounterOpts{ | ||
| Name: "cortex_committed_resource_change_api_timeouts_total", | ||
| Help: "Total number of commitment change requests that timed out while waiting for reservations to become ready", | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| // Describe implements prometheus.Collector. | ||
| func (m *ChangeCommitmentsAPIMonitor) Describe(ch chan<- *prometheus.Desc) { | ||
| m.requestCounter.Describe(ch) | ||
| m.requestDuration.Describe(ch) | ||
| m.commitmentChanges.Describe(ch) | ||
| m.timeouts.Describe(ch) | ||
| } | ||
|
|
||
| // Collect implements prometheus.Collector. | ||
| func (m *ChangeCommitmentsAPIMonitor) Collect(ch chan<- prometheus.Metric) { | ||
| m.requestCounter.Collect(ch) | ||
| m.requestDuration.Collect(ch) | ||
| m.commitmentChanges.Collect(ch) | ||
| m.timeouts.Collect(ch) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fragile status code derivation via error string matching.
Determining HTTP status codes by inspecting error message substrings is brittle—if the error messages change, this logic silently breaks. Consider using typed errors or sentinel errors to convey the status code.
💡 Suggested approach using sentinel errors
🤖 Prompt for AI Agents