Skip to content

improve expr float() and int() to handle strings #161

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 1 commit into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/metrics/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func toInt64(i interface{}) int64 {
return v
case time.Duration:
return int64(v)
case string:
value, err := strconv.ParseInt(v, 10, 64)
if err != nil {
panic(err)
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not confident that we want to panic here. It's user supplied data after all. So a single malformed MQTT message will bring the exporter into a crashloop? 🤔

}
return value
default:
return v.(int64) // Hope for the best
}
Expand All @@ -103,6 +109,12 @@ func toFloat64(i interface{}) float64 {
return float64(v)
case time.Duration:
return float64(v)
case string:
value, err := strconv.ParseFloat(v, 64)
if err != nil {
panic(err)
}
return value
default:
return v.(float64) // Hope for the best
}
Expand Down
Loading