Skip to content
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

Closed
wants to merge 5 commits into from

Conversation

cwurm
Copy link
Contributor

@cwurm cwurm commented Jun 6, 2019

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.

@cwurm cwurm added bug review needs_backport PR is waiting to be backported to other branches. labels Jun 6, 2019
@cwurm cwurm requested a review from a team as a code owner June 6, 2019 01:49
@@ -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)
Copy link
Member

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.

Copy link
Contributor Author

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.

@cwurm cwurm force-pushed the fix_custom_index_pattern branch from 5f0680f to a60364c Compare July 2, 2019 16:08
@cwurm
Copy link
Contributor Author

cwurm commented Jul 3, 2019

jenkins, test this

@cwurm cwurm requested a review from andrewkroh July 3, 2019 13:20
@cwurm
Copy link
Contributor Author

cwurm commented Jul 3, 2019

This slipped through the cracks for a while. Rebased and retested, CI is green. @andrewkroh let me know if this looks good now.

objectMap, ok := object.(map[string]interface{})
if !ok {
continue
objectMaps, ok := content["objects"].([]common.MapStr)
Copy link
Member

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.

}
content["objects"] = objects
Copy link

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?

Copy link
Contributor Author

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)?

@kvch kvch self-requested a review July 17, 2019 15:47
Copy link
Contributor

@kvch kvch left a 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.

@cwurm
Copy link
Contributor Author

cwurm commented Jul 23, 2019

@urso @kvch I changed to a switch as suggested and rebased on master.

@cbricedavis
Copy link

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?

@kaiyan-sheng kaiyan-sheng added Team:Beats Team:Integrations Label for the Integrations team Team:Services (Deprecated) Label for the former Integrations-Services team and removed Team:Beats labels Feb 28, 2020
@botelastic
Copy link

botelastic bot commented Jul 17, 2020

Hi!
We just realized that we haven't looked into this PR in a while. We're sorry!

We're labeling this issue as Stale to make it hit our filters and make sure we get back to it in as soon as possible. In the meantime, it'd be extremely helpful if you could take a look at it as well and confirm its relevance. A simple comment with a nice emoji will be enough :+1.
Thank you for your contribution!

@botelastic botelastic bot added the Stalled label Jul 17, 2020
@elasticmachine
Copy link
Collaborator

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
@elasticmachine
Copy link
Collaborator

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?

@botelastic botelastic bot removed the Stalled label Jul 17, 2020
@cbricedavis
Copy link

Still relevant 👍

@kvch
Copy link
Contributor

kvch commented Jul 21, 2020

The issue was fixed by #17749

@kvch kvch closed this Jul 21, 2020
@zube zube bot removed the [zube]: Done label Oct 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs_backport PR is waiting to be backported to other branches. review Team:Integrations Label for the Integrations team Team:Services (Deprecated) Label for the former Integrations-Services team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants