-
Notifications
You must be signed in to change notification settings - Fork 24
Add leniency and type coercion for prom datasource options #310
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
Open
jcolladokuri
wants to merge
11
commits into
main
Choose a base branch
from
jck/lenient-promoptions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4213990
add lenient backend types
jcolladokuri 6568372
add logs to keep track of when leniency (either coerced or dropped) w…
jcolladokuri 6fc1278
changeset
jcolladokuri 58e103a
Merge branch 'main' into jck/lenient-promoptions
jcolladokuri 64d81f9
update grafana prometheus
jcolladokuri 6972f63
Merge branch 'jck/lenient-promoptions' of https://github.com/grafana/…
jcolladokuri 969fa0e
match other unmarshal functions structure for
jcolladokuri d06008a
drop lenient int and only use lenient float instead
jcolladokuri 44c7bae
support distinguising between empty limits and an actual 0 limit.
jcolladokuri bf7adfb
update changeset
jcolladokuri a42412f
Merge branch 'main' into jck/lenient-promoptions
jcolladokuri 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| 'grafana-prometheus-datasource': patch | ||
| --- | ||
|
|
||
| Fix: Stop rejecting loosely-typed jsonData. Since promlib v0.0.13 a datasource provisioned through the API, Terraform or an operator with an off-spec value — `"true"` for a boolean, `"1000"` for a number, `2.4` for a version string — failed to load, and every query, health check and metric lookup against it errored. Such values are now coerced where the type can be read and ignored where it cannot, each logging a warning. `timeInterval`, `queryTimeout` and `httpMethod` are unchanged and still reject a wrong type. | ||
|
|
||
| An ignored value leaves `seriesLimit` unset rather than 0, so it stays distinguishable from a configured limit of zero and readers still apply their own default. | ||
|
|
||
| **Breaking (Go API):** the affected `models.PromOptions` fields change from `string`/`bool`/`float64`/`*int64` to named lenient types (`LenientBool`, `LenientString`, `LenientFloat64`, `LenientExemplarTraceIDDestinations`) with the same underlying types and JSON encoding. Literals still assign and compare as before, but passing one to a `string`, `bool` or `float64` parameter now needs an explicit conversion, e.g. `string(opts.CustomQueryParameters)`. `SeriesLimit` becomes `*LenientFloat64`, matching the frontend's `number`. `HTTPMethod`, `TimeInterval` and `QueryTimeout` keep their existing types. |
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,9 @@ | ||
| --- | ||
| 'promlib': patch | ||
| --- | ||
|
|
||
| Fix: Stop rejecting loosely-typed jsonData. Since promlib v0.0.13 a datasource provisioned through the API, Terraform or an operator with an off-spec value — `"true"` for a boolean, `"1000"` for a number, `2.4` for a version string — failed to load, and every query, health check and metric lookup against it errored. Such values are now coerced where the type can be read and ignored where it cannot, each logging a warning. `timeInterval`, `queryTimeout` and `httpMethod` are unchanged and still reject a wrong type. | ||
|
|
||
| An ignored value leaves `seriesLimit` unset rather than 0, so it stays distinguishable from a configured limit of zero and readers still apply their own default. | ||
|
|
||
| **Breaking (Go API):** the affected `models.PromOptions` fields change from `string`/`bool`/`float64`/`*int64` to named lenient types (`LenientBool`, `LenientString`, `LenientFloat64`, `LenientExemplarTraceIDDestinations`) with the same underlying types and JSON encoding. Literals still assign and compare as before, but passing one to a `string`, `bool` or `float64` parameter now needs an explicit conversion, e.g. `string(opts.CustomQueryParameters)`. `SeriesLimit` becomes `*LenientFloat64`, matching the frontend's `number`. `HTTPMethod`, `TimeInterval` and `QueryTimeout` keep their existing types. |
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,166 @@ | ||
| package models | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/grafana/grafana-plugin-sdk-go/backend/log" | ||
| ) | ||
|
|
||
| // maxLoggedValueLen keeps a stored object or array from filling a log line. | ||
| const maxLoggedValueLen = 64 | ||
|
|
||
| func coerced(toValueType, fromValueType string, data []byte) { | ||
| logLenient(fromValueType, toValueType, "coerced", data) | ||
| } | ||
|
|
||
| func dropped(toValueType, fromValueType string, data []byte) { | ||
| logLenient(fromValueType, toValueType, "dropped", data) | ||
| } | ||
|
|
||
| func logLenient(fromValueType, toValueType, outcome string, data []byte) { | ||
| value := string(data) | ||
| if len(value) > maxLoggedValueLen { | ||
| value = value[:maxLoggedValueLen] + "…" | ||
| } | ||
| log.DefaultLogger.Warn("datasource jsonData value does not match its declared type", | ||
| "from", fromValueType, "to", toValueType, "outcome", outcome, "value", value) | ||
| } | ||
|
|
||
| // LenientBool also accepts the string and numeric spellings of a boolean. | ||
| type LenientBool bool | ||
|
|
||
| func (b *LenientBool) UnmarshalJSON(data []byte) error { | ||
| var value bool | ||
| if err := json.Unmarshal(data, &value); err == nil { | ||
| *b = LenientBool(value) | ||
| return nil | ||
| } | ||
|
|
||
| var str string | ||
| if err := json.Unmarshal(data, &str); err == nil { | ||
| if parsed, err := strconv.ParseBool(strings.TrimSpace(str)); err == nil { | ||
| *b = LenientBool(parsed) | ||
| coerced("bool", "string", data) | ||
| return nil | ||
| } | ||
| dropped("bool", "string", data) | ||
| return nil | ||
| } | ||
|
|
||
| var number float64 | ||
| if err := json.Unmarshal(data, &number); err == nil { | ||
| *b = LenientBool(number != 0) | ||
| coerced("bool", "float64", data) | ||
| return nil | ||
| } | ||
|
|
||
| dropped("bool", "unknown", data) | ||
| return nil | ||
| } | ||
|
|
||
| // LenientString also accepts a scalar, keeping its JSON text, so an identifier or version | ||
| // that YAML turned into a number (prometheusVersion: 2.4) is not blanked. | ||
| type LenientString string | ||
|
|
||
| func (s *LenientString) UnmarshalJSON(data []byte) error { | ||
| var str string | ||
| if err := json.Unmarshal(data, &str); err == nil { | ||
| *s = LenientString(str) | ||
| return nil | ||
| } | ||
|
|
||
| var number float64 | ||
| if err := json.Unmarshal(data, &number); err == nil { | ||
| *s = LenientString(strings.TrimSpace(string(data))) | ||
| coerced("string", "float64", data) | ||
| return nil | ||
| } | ||
|
|
||
| var boolean bool | ||
| if err := json.Unmarshal(data, &boolean); err == nil { | ||
| *s = LenientString(strings.TrimSpace(string(data))) | ||
| coerced("string", "bool", data) | ||
| return nil | ||
| } | ||
|
|
||
| dropped("string", "unknown", data) | ||
| return nil | ||
| } | ||
|
|
||
| // LenientFloat64 also accepts a quoted number. | ||
| type LenientFloat64 float64 | ||
|
|
||
| func (f *LenientFloat64) UnmarshalJSON(data []byte) error { | ||
| value, from, ok := readFloat64(data) | ||
| if !ok { | ||
| dropped("float64", from, data) | ||
| return nil | ||
| } | ||
|
|
||
| *f = LenientFloat64(value) | ||
| if from != "" { | ||
| coerced("float64", from, data) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // readFloat64 reports what LenientFloat64 reads, which JSON type it came from ("" meaning the | ||
| // declared type, so no leniency), and whether it could be read at all. Split out so | ||
| // clearDroppedPointers can ask the same question without logging and skewing the counts. | ||
| func readFloat64(data []byte) (value float64, from string, ok bool) { | ||
| var number float64 | ||
| if err := json.Unmarshal(data, &number); err == nil { | ||
| // value was expected float64 | ||
| return number, "", true | ||
| } | ||
|
|
||
| var str string | ||
| if err := json.Unmarshal(data, &str); err == nil { | ||
| if parsed, err := strconv.ParseFloat(strings.TrimSpace(str), 64); err == nil { | ||
| // value was a number string | ||
| return parsed, "string", true | ||
| } | ||
| // value was a non number string (i.e. "ten") | ||
| return 0, "string", false | ||
| } | ||
|
|
||
| // value was an unsupported value to coerce from. | ||
| return 0, "unknown", false | ||
| } | ||
|
|
||
| // LenientExemplarTraceIDDestinations ignores a value it cannot read. It does not salvage a | ||
| // partial one: a guess would only disagree with what the frontend reads from jsonData. | ||
| type LenientExemplarTraceIDDestinations []ExemplarTraceIDDestination | ||
|
|
||
| func (d *LenientExemplarTraceIDDestinations) UnmarshalJSON(data []byte) error { | ||
| var destinations []ExemplarTraceIDDestination | ||
| if err := json.Unmarshal(data, &destinations); err == nil { | ||
| *d = destinations | ||
| return nil | ||
| } | ||
|
|
||
| dropped("exemplarDestinations", "unknown", data) | ||
| return nil | ||
| } | ||
|
|
||
| // encoding/json allocates a pointer field before the lenient type sees the value, so a dropped | ||
| // value leaves it non-nil at zero — indistinguishable from a stored 0, which for seriesLimit is | ||
| // the difference between "apply your own default" and "limit is zero". A lenient type is handed | ||
| // a pointer to the allocated value, never to the field, so only the parser can restore nil. | ||
| func (o *PromOptions) clearDroppedPointers(data []byte) { | ||
| var raw map[string]json.RawMessage | ||
| if err := json.Unmarshal(data, &raw); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if value, ok := raw["seriesLimit"]; ok { | ||
| if _, _, readable := readFloat64(value); !readable { | ||
| // value was an unsupported value to coerce from | ||
| // set to nil so ensure it is not confused with a stored 0 | ||
| o.SeriesLimit = nil | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
can you pipe in the prometheus logger here so it has a little more context?
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.
is there anything specific from the additional context that you think would be helpful to add here? piping the prom logger has some trade-offs. Because these unmarshalling functions are called directly from
encoding/jsonlooking would have to live outside of theUnmarshalJSONfunctions and moved intoParsePromOptions, it requires additional parsing and we'd lose information about which path the specific value took (i.e. fromint64tostring).Would love to hear your thoughts on this