-
Notifications
You must be signed in to change notification settings - Fork 0
Instrument cleanup worker metrics #43
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
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package worker | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
| "unicode" | ||
|
|
||
| "github.com/evalops/asb/internal/app" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| type MetricsOptions struct { | ||
| Registerer prometheus.Registerer | ||
| DurationBuckets []float64 | ||
| } | ||
|
|
||
| type Metrics struct { | ||
| processed *prometheus.CounterVec | ||
| duration prometheus.Histogram | ||
| } | ||
|
|
||
| func NewMetrics(serviceName string, opts MetricsOptions) (*Metrics, error) { | ||
| if opts.Registerer == nil { | ||
| opts.Registerer = prometheus.DefaultRegisterer | ||
| } | ||
| if len(opts.DurationBuckets) == 0 { | ||
| opts.DurationBuckets = prometheus.DefBuckets | ||
| } | ||
|
|
||
| prefix := metricsPrefix(serviceName) | ||
| processed, err := registerCounterVec( | ||
| opts.Registerer, | ||
| prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Name: prefix + "_cleanup_processed_total", | ||
| Help: "Count of ASB cleanup items processed by type.", | ||
| }, | ||
| []string{"item_type"}, | ||
| ), | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| duration, err := registerHistogram( | ||
| opts.Registerer, | ||
| prometheus.NewHistogram( | ||
| prometheus.HistogramOpts{ | ||
| Name: prefix + "_cleanup_pass_seconds", | ||
| Help: "Duration of ASB cleanup passes in seconds.", | ||
| Buckets: opts.DurationBuckets, | ||
| }, | ||
| ), | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &Metrics{ | ||
| processed: processed, | ||
| duration: duration, | ||
| }, nil | ||
| } | ||
|
|
||
| func (m *Metrics) recordCleanupPass(stats *app.CleanupStats, duration time.Duration) { | ||
| if m == nil { | ||
| return | ||
| } | ||
| m.duration.Observe(duration.Seconds()) | ||
| if stats == nil { | ||
| return | ||
| } | ||
| m.processed.WithLabelValues("approvals").Add(float64(stats.ApprovalsExpired)) | ||
| m.processed.WithLabelValues("sessions").Add(float64(stats.SessionsExpired)) | ||
| m.processed.WithLabelValues("grants").Add(float64(stats.GrantsExpired)) | ||
| m.processed.WithLabelValues("artifacts").Add(float64(stats.ArtifactsExpired)) | ||
| } | ||
|
|
||
| func metricsPrefix(serviceName string) string { | ||
| serviceName = strings.TrimSpace(serviceName) | ||
| if serviceName == "" { | ||
| return "service" | ||
| } | ||
|
|
||
| var builder strings.Builder | ||
| for index, runeValue := range serviceName { | ||
| switch { | ||
| case unicode.IsLetter(runeValue), unicode.IsDigit(runeValue): | ||
| builder.WriteRune(unicode.ToLower(runeValue)) | ||
| default: | ||
| builder.WriteByte('_') | ||
| } | ||
| if index == 0 && unicode.IsDigit(runeValue) { | ||
| builder.WriteByte('_') | ||
| } | ||
| } | ||
|
|
||
| prefix := strings.Trim(builder.String(), "_") | ||
| if prefix == "" { | ||
| return "service" | ||
| } | ||
| if prefix[0] >= '0' && prefix[0] <= '9' { | ||
| return "service_" + prefix | ||
| } | ||
| return prefix | ||
| } | ||
|
|
||
| func registerCounterVec(registerer prometheus.Registerer, collector *prometheus.CounterVec) (*prometheus.CounterVec, error) { | ||
| if err := registerer.Register(collector); err != nil { | ||
| alreadyRegistered, ok := err.(prometheus.AlreadyRegisteredError) | ||
| if !ok { | ||
| return nil, err | ||
| } | ||
| existing, ok := alreadyRegistered.ExistingCollector.(*prometheus.CounterVec) | ||
| if !ok { | ||
| return nil, err | ||
| } | ||
| return existing, nil | ||
| } | ||
| return collector, nil | ||
| } | ||
|
|
||
| func registerHistogram(registerer prometheus.Registerer, collector prometheus.Histogram) (prometheus.Histogram, error) { | ||
| if err := registerer.Register(collector); err != nil { | ||
| alreadyRegistered, ok := err.(prometheus.AlreadyRegisteredError) | ||
| if !ok { | ||
| return nil, err | ||
| } | ||
| existing, ok := alreadyRegistered.ExistingCollector.(prometheus.Histogram) | ||
| if !ok { | ||
| return nil, fmt.Errorf("register histogram: existing collector has unexpected type %T", alreadyRegistered.ExistingCollector) | ||
| } | ||
| return existing, nil | ||
| } | ||
| return collector, nil | ||
| } | ||
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
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.
Three helper functions fully duplicated across packages
Low Severity
metricsPrefix,registerCounterVec, andregisterHistogramare verbatim copies of the identical unexported functions already ininternal/app/metrics.go. Any future bug fix to one copy risks being missed in the other. These could live in a shared internal package (e.g.internal/promutil) to eliminate the duplication.Additional Locations (1)
internal/worker/metrics.go#L108-L122Reviewed by Cursor Bugbot for commit ef4f60c. Configure here.