-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix: pika exporter error output due to versions dismatch #2951
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 | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -252,9 +252,10 @@ func (e *exporter) collectInfo(c *client, ch chan<- prometheus.Metric) error { | |||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
parseOpt := metrics.ParseOption{ | ||||||||||||||||||||||||
Version: version, | ||||||||||||||||||||||||
Extracts: extracts, | ||||||||||||||||||||||||
Info: info, | ||||||||||||||||||||||||
Version: version, | ||||||||||||||||||||||||
Extracts: extracts, | ||||||||||||||||||||||||
Info: info, | ||||||||||||||||||||||||
CurrentVersion: selectversion(version.Original()), | ||||||||||||||||||||||||
Comment on lines
+255
to
+258
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for unsupported versions The Consider adding error handling: - CurrentVersion: selectversion(version.Original()),
+ CurrentVersion: selectversion(version.Original())
+ }
+ if parseOpt.CurrentVersion == nil {
+ return fmt.Errorf("unsupported Pika version: %s", version.Original())
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
for _, m := range metrics.MetricConfigs { | ||||||||||||||||||||||||
m.Parse(m, collector, parseOpt) | ||||||||||||||||||||||||
|
@@ -263,6 +264,37 @@ func (e *exporter) collectInfo(c *client, ch chan<- prometheus.Metric) error { | |||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const ( | ||||||||||||||||||||||||
VERSION_336 = "3.3.6" | ||||||||||||||||||||||||
VERSION_350 = "3.5.0" | ||||||||||||||||||||||||
VERSION_355 = "3.5.5" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func selectversion(version string) metrics.VersionChecker { | ||||||||||||||||||||||||
if !isValidVersion(version) { | ||||||||||||||||||||||||
log.Warnf("Invalid version format: %s", version) | ||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
var v metrics.VersionChecker | ||||||||||||||||||||||||
switch version { | ||||||||||||||||||||||||
case VERSION_336: | ||||||||||||||||||||||||
v = &metrics.VersionChecker336{} | ||||||||||||||||||||||||
case VERSION_355: | ||||||||||||||||||||||||
v = &metrics.VersionChecker355{} | ||||||||||||||||||||||||
case VERSION_350: | ||||||||||||||||||||||||
v = &metrics.VersionChecker350{} | ||||||||||||||||||||||||
default: | ||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
v.InitVersionChecker() | ||||||||||||||||||||||||
return v | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+273
to
+291
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve version selection logic and error handling The function can be improved for better error handling and cleaner code:
-func selectversion(version string) metrics.VersionChecker {
+func selectversion(version string) (metrics.VersionChecker, error) {
if !isValidVersion(version) {
log.Warnf("Invalid version format: %s", version)
- return nil
+ return nil, fmt.Errorf("invalid version format: %s", version)
}
- var v metrics.VersionChecker
switch version {
- case VERSION_336:
- v = &metrics.VersionChecker336{}
- case VERSION_355:
- v = &metrics.VersionChecker355{}
- case VERSION_350:
- v = &metrics.VersionChecker350{}
+ case PIKA_VERSION_336:
+ checker := &metrics.VersionChecker336{}
+ checker.InitVersionChecker()
+ return checker, nil
+ case PIKA_VERSION_355:
+ checker := &metrics.VersionChecker355{}
+ checker.InitVersionChecker()
+ return checker, nil
+ case PIKA_VERSION_350:
+ checker := &metrics.VersionChecker350{}
+ checker.InitVersionChecker()
+ return checker, nil
default:
- return nil
+ return nil, fmt.Errorf("unsupported version: %s", version)
}
- v.InitVersionChecker()
- return v
}
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
// isValidVersion validates the version string format (e.g., x.y.z) | ||||||||||||||||||||||||
func isValidVersion(version string) bool { | ||||||||||||||||||||||||
matched, _ := regexp.MatchString(`^\d+\.\d+\.\d+$`, version) | ||||||||||||||||||||||||
return matched | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+293
to
+297
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Optimize version validation with pre-compiled regex The current implementation compiles the regex on every call and ignores potential compilation errors. Consider pre-compiling the regex for better performance and error handling. +var versionRegex = regexp.MustCompile(`^\d+\.\d+\.\d+$`)
+
// isValidVersion validates the version string format (e.g., x.y.z)
func isValidVersion(version string) bool {
- matched, _ := regexp.MatchString(`^\d+\.\d+\.\d+$`, version)
- return matched
+ return versionRegex.MatchString(version)
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||
func (e *exporter) collectKeys(c *client) error { | ||||||||||||||||||||||||
allKeys := append([]dbKeyPair{}, e.keys...) | ||||||||||||||||||||||||
keys, err := getKeysFromPatterns(c, e.keyPatterns, e.scanCount) | ||||||||||||||||||||||||
|
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.
🛠️ Refactor suggestion
Optimize version checker implementation
The current implementation has several areas for improvement:
Consider these improvements:
This refactoring: