-
Notifications
You must be signed in to change notification settings - Fork 3
feat(purge): add Prometheus metrics for purge request tracking #35
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package purge | ||
|
|
||
| import "github.com/prometheus/client_golang/prometheus" | ||
|
|
||
| var ( | ||
| _metricPurgeRequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
| Namespace: "tr", | ||
| Subsystem: "tavern", | ||
| Name: "purge_requests_total", | ||
| Help: "Total number of purge requests", | ||
| }, []string{"code"}) | ||
| ) | ||
|
|
||
| func init() { | ||
| prometheus.MustRegister(_metricPurgeRequestsTotal) | ||
|
|
||
| // docs/purge.md references these labels | ||
| _metricPurgeRequestsTotal.WithLabelValues("200") | ||
| _metricPurgeRequestsTotal.WithLabelValues("403") | ||
| _metricPurgeRequestsTotal.WithLabelValues("404") | ||
| _metricPurgeRequestsTotal.WithLabelValues("500") | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ package purge | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||
| "encoding/binary" | ||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||
|
|
@@ -13,6 +14,7 @@ import ( | |||||||||||||||||||||||
| storagev1 "github.com/omalloc/tavern/api/defined/v1/storage" | ||||||||||||||||||||||||
| "github.com/omalloc/tavern/contrib/log" | ||||||||||||||||||||||||
| "github.com/omalloc/tavern/internal/constants" | ||||||||||||||||||||||||
| "github.com/omalloc/tavern/pkg/encoding" | ||||||||||||||||||||||||
| "github.com/omalloc/tavern/plugin" | ||||||||||||||||||||||||
| "github.com/omalloc/tavern/storage" | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
|
@@ -48,10 +50,26 @@ func (r *PurgePlugin) Stop(ctx context.Context) error { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| func (r *PurgePlugin) AddRouter(router *http.ServeMux) { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| codec := encoding.GetDefaultCodec() | ||||||||||||||||||||||||
| sharedkv := storage.Current().SharedKV() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| router.Handle("/plugin/purge/tasks", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||||||||||||||||||||||||
| // TODO: query sharedkv purge task list | ||||||||||||||||||||||||
| // query sharedkv purge task list | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| purgeTaskMap := make(map[string]uint64) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| sharedkv.IteratePrefix(req.Context(), []byte("dir/"), func(key, val []byte) error { | ||||||||||||||||||||||||
|
Comment on lines
+55
to
+62
|
||||||||||||||||||||||||
| purgeTaskMap[string(key)[4:]] = binary.LittleEndian.Uint64(val) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| purgeTaskMap[string(key)[4:]] = binary.LittleEndian.Uint64(val) | |
| if len(key) >= 4 { | |
| purgeTaskMap[string(key)[4:]] = binary.LittleEndian.Uint64(val) | |
| } |
Copilot
AI
Jan 21, 2026
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.
The error returned by IteratePrefix is being ignored. If the iteration fails, the handler will still return a 200 OK status with potentially incomplete data. Consider checking the error and returning an appropriate HTTP error status if the iteration fails.
| sharedkv.IteratePrefix(req.Context(), []byte("dir/"), func(key, val []byte) error { | |
| purgeTaskMap[string(key)[4:]] = binary.LittleEndian.Uint64(val) | |
| return nil | |
| }) | |
| if err := sharedkv.IteratePrefix(req.Context(), []byte("dir/"), func(key, val []byte) error { | |
| purgeTaskMap[string(key)[4:]] = binary.LittleEndian.Uint64(val) | |
| return nil | |
| }); err != nil { | |
| w.WriteHeader(http.StatusInternalServerError) | |
| return | |
| } |
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.
The binary.LittleEndian.Uint64(val) call will panic if val is less than 8 bytes long. Add a length check to ensure len(val) >= 8 before calling Uint64, or skip entries with invalid value lengths to prevent a potential runtime panic.