-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 custom index name in setup #12457
Conversation
libbeat/dashboards/modify_json.go
Outdated
@@ -46,20 +46,16 @@ func ReplaceIndexInIndexPattern(index string, content common.MapStr) common.MapS | |||
return content | |||
} | |||
|
|||
objects, ok := content["objects"].([]interface{}) | |||
objects, ok := content["objects"].([]common.MapStr) |
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.
It looks like this ReplaceIndexInIndexPattern
function is used by a few code paths. There is one case where it is used with the internally generated index pattern (this probably contains []common.MapStr
) and another case where it operates on data produced directly from a json.Unmarshal
(this would have []interface
).
So perhaps this needs to handle both cases? Take a look at the callers ReplaceIndexInIndexPattern
.
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.
Ah, you're right. I don't think we usually use the direct file import functionality since #10478, but it's still there. I pushed an update to make both cases work.
5f0680f
to
a60364c
Compare
jenkins, test this |
This slipped through the cracks for a while. Rebased and retested, CI is green. @andrewkroh let me know if this looks good now. |
libbeat/dashboards/modify_json.go
Outdated
objectMap, ok := object.(map[string]interface{}) | ||
if !ok { | ||
continue | ||
objectMaps, ok := content["objects"].([]common.MapStr) |
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.
You might as well constrain the scope of objectMaps
to the if
block by declaring as part of the if statement just like it's done in the if else
.
c8456a4
to
9773d63
Compare
} | ||
content["objects"] = objects |
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.
When dealing with external input and some implicitely assumed types, then we should validate and log if something doesn't match our expectations.
Maybe use this pattern:
switch objects := content["objects"].(type) {
case []interface{}:
...
case []common.MapStr;
...
default:
// log error with type + contents e.g.
logp.Errf("Unexpected object type %T, expected list of objects. Got: %#v", content["objects"], content["objects"])
}
This if-else construct seems to normalize the object contents for further processing. Maybe we should error if we can't do so, but the field is present? How about moving the normalization into a separate function?
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.
Thanks, I've changed to a switch
.
I initially tried separating the normalization from the rest of the logic. Unfortunately, it's not just a single type cast. The whole structure is nested, with each nested object being either []interface{}
or []common.MapStr
. So we'd need to recursively cast everything from one to the other, and maybe that's out of scope for this PR (which is essentially just fixing a regression bug)?
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.
I prefer you rewrite the if
-else
part of the code to use type switches as @urso mentioned in his comment. It's more idiomatic when multiple types are valid/can be handled.
9773d63
to
8c508d1
Compare
I've run into this issue today and it doesn't look like there have been any updates since July - is there a way to implement this fix? |
Hi! We're labeling this issue as |
Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually? |
1 similar comment
Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually? |
Still relevant 👍 |
The issue was fixed by #17749 |
beatname setup -E setup.dashboards.index="myname-*"
is supposed to create an index pattern in Kibana with a custom index name.This currently doesn't work (discuss thread) because of an impossible type assertion in the code (
x.([]interface{})
will never work if x is of type[]common.MapStr
which it is).This fixes the type assertions. I've tested that
setup
can create custom index patterns.