-
Notifications
You must be signed in to change notification settings - Fork 32
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
Add IsValidValue method to stats package. #116
Conversation
For programs which import opaque metrics from 3rd party APIs and then pass those into the stats package, this will help them avoid panics that result from passing in invalid values (such as structs or maps) that the 3rd party API might have returned.
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.
Structs and arrays are supported in some places (stats.Report
for example).
I wonder if this would be better served by introducing an invalid value for Type
, then when the type is not supported, we return a value of type Invalid
, then it can be checked with:
v := ValueOf(unsupportedValue)
if v.Type() == stats.Invalid {
...
}
What do you think?
Also, if you have an example of stack trace for a panic that was triggered, and a code example of how this new API would be used, it could be helpful to determine what the best solution would be.
The panic I saw was generated from a program that pulls metrics from the jolokia agent running on a kafka connect JVM. It calls the API, deserializes the results (which for the most part are valid), and then iterates through each result and calls
Which then generates this panic stacktrace:
My original thought is that I could do something like this instead:
|
What do you think about supporting an API like this? for k, v := range fromAPI {
if x := stats.ValueOf(v); x.Type() != stats.Invalid {
stats.Set(k, x)
}
} |
That would be totally fine. If we're going to do that though we'd need to push the panic logic to all of the callers of ValueOf, or perhaps create a
And then change the two production call sites and the myriad of test sites to use Thoughts? |
Sounds good to me 👍 |
I don't think it's necessary to change the tests since they're not validating the panic case. We'll probably need to add a case to func ValueOf(v interface{}) Value {
switch x := v.(type) {
...
case Value:
return x
default:
return Value{typ: Invalid}
}
} |
This inspects the return value of ValueOf, ideally. Non-test call-sites were changed to use MustValueOf(ValueOf(..)).
ready for review |
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.
Nice!
For programs which import opaque metrics from 3rd party APIs and
then pass those into the stats package, this will help them avoid
panics that result from passing in invalid values (such as structs
or maps) that the 3rd party API might have returned.