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

Allow template pattern to be overwritten #4769

Merged
merged 1 commit into from
Aug 21, 2017

Conversation

ruflin
Copy link
Contributor

@ruflin ruflin commented Jul 27, 2017

With the current template loading is was not possible to overload the pattern used as name and pattern were mixed. For example in the case where someone wanted to define pattern name a that applies to the indice a this was not possible because the pattern generated was a-* so it didn't apply to the index a. This change now allows to set pattern and name separately.

To make sure index pattern and templates are in sync, the beat will not start in case the index pattern was modified but the template patterns not. We should figure out later some automated mechanisms here.

We should find a better solution for 6.x to require less config options and also include dashboard generation in this.

@ruflin ruflin added discuss Issue needs further discussion. in progress Pull request is currently in progress. labels Jul 27, 2017
# The version of the beat will always be appended to the given name
# so the final name is beatname-%{[beat.version]}.
#setup.template.name: "beatname"
# The f
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ignore this doc and below for the moment. Take a look at the PR description on the behaviour.

@ruflin
Copy link
Contributor Author

ruflin commented Jul 27, 2017

It would be nice if the template name would be based on the elasticsearch index name. the problem with that for the moment is that the index name includes the daily pattern: index: "beatname-%{[beat.version]}-%{+yyyy.MM.dd}"

We could change this to:

elasticsearch
  index: "beatname-%{[beat.version]}"
  pattern: "-%{+yyyy.MM.dd}"

This would allow the template loading to take by default the index name as the name for the template. Problem with this it's an additional break in case someone configured a different index-name.


# Template patttern. By default the template patter is "-%{[beat.version]}-*" to apply to the default index settings.
# The first part is the version of the beat and then -* is used to match all daily indicies.
#setup.template.pattern: "beatname-%{[beat.version]}-*"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we even need the -* for the pattern or could we just use * so it would also apply do indices which are exactly like the index name?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I understand this suggestion. You mean something lie beatname-%{[beat.version]}*?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes


pattern := config.Pattern
if pattern == "" {
pattern = name + "-*"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we remove here the - and make this just *?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is just the default, right? I'd say let's keep the -*, so that it's less likely to apply accidentally to other indices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes 👍

@ruflin ruflin force-pushed the template-pattern branch from f05ecb1 to b7e9e00 Compare July 28, 2017 08:16
}

// Get ES Index name for comparison
esCfg := struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@urso Should I also check for indicies here and len==0?

@@ -2790,28 +2790,28 @@ Bytes in non-idle span.


[[exported-fields-graphite]]
== graphite Fields
== graphite fields
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dedemorton Seems like some of your recent changes are revert? I wonder why the previous builds went green?

return t.name
}

// GetPattern returns the name of the template which is {index}-{version}
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment seems out of date.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

}

if esCfg.Index != "" && (cfg.Name == "" || cfg.Pattern == "") {
return fmt.Errorf("setup.template.name and setup.template.pattern have to be set if index name is modified.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the name really have to be changed? I'm thinking we could only check on the setup.template.pattern?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will be kind of strange if the name is not changed. Assuming someone changes the index to test-{daily} and the pattern to test-* he then sees beatname-{version}-{daily} as the template name with test-* as pattern. In case that template is already there, it would not be overwritten and he would still not have the expected template.

Copy link
Contributor

@tsg tsg left a comment

Choose a reason for hiding this comment

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

Generally LGTM. Waiting for a review from @urso as well.

@ruflin ruflin force-pushed the template-pattern branch 2 times, most recently from ab01baa to 5bc30f9 Compare August 1, 2017 12:58
@ruflin ruflin force-pushed the template-pattern branch from 50d71f4 to 882e29d Compare August 7, 2017 06:38
simitt pushed a commit to elastic/apm-server that referenced this pull request Aug 7, 2017
The version number of apm-server and the system tests have to be kept manually in sync until elastic/beats#4769 is merged.
@ruflin ruflin added review and removed discuss Issue needs further discussion. in progress Pull request is currently in progress. labels Aug 7, 2017
@ruflin ruflin force-pushed the template-pattern branch from 882e29d to bb589c3 Compare August 7, 2017 07:48
@tsg
Copy link
Contributor

tsg commented Aug 9, 2017

@ruflin I tested this branch, but when using a configuration like this (which is what's suggested in the docs):

setup.template:
  name: test
  pattern: "metricbeat-%{[beat.version]}-*"

I get this in the resulting template:

{
  "test": {
    "order": 1,
    "index_patterns": [
      "metricbeat-%{[beat.version]}-*"
    ],

So it seems like the variables are not resolved as I was expecting.

@urso
Copy link

urso commented Aug 9, 2017

I don't see any use of format strings being applied. It's all strings only. When/how does beat.version get resolved? How do you handle event fields being used, that are specific to a module/prospector? e.g. %{[fields.index]}-%{+yyyy.MM.dd}? In this case you have to log an error and ask the user to explicitly configure the template pattern (if possible).

@ruflin
Copy link
Contributor Author

ruflin commented Aug 10, 2017

Currently the default works when not set because it is set internal. But as @urso said there is currently no handling of the special strings. My suggestion is to support in this version everything under beat.* as that is static but not support dynamic fields from event (I would not know how we could do this). Like this the defaults also work when they are written out in the config and it should cover most of the case. We should open later a separate issue on how and if we want to support the more complex cases with fields.index for example.

}

logp.Debug("template", "Lookup key for pattern not available: %s", key)
return nil, nil
Copy link

Choose a reason for hiding this comment

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

should we handled this as error? Compiling a pattern with missing field might give you an invalid index name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, will return an error if there is a pattern but doesn't match either of the two.


if key == "[beat.version]" {
return Formatter{s: bV.String()}, nil
}
Copy link

Choose a reason for hiding this comment

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

Date patterns start with + character. See: https://github.com/elastic/beats/blob/master/libbeat/common/fmtstr/formatevents.go#L252

That is, we can replace %{+<pattern>} with * in here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there an easy way to reuse this pattern compiling?

@@ -167,3 +204,19 @@ func loadYaml(path string) (Fields, error) {
}
return fields, nil
}

type Formatter struct {
Copy link

Choose a reason for hiding this comment

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

don't leak/export this type if you don't need it. Also template.Formatter is kind of a bad choice here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree, need to come up with a better name

@@ -41,6 +45,39 @@ func New(beatVersion string, beatName string, esVersion string, config TemplateC
pattern = name + "-*"
}

compiler := func(key string, ops []fmtstr.VariableOp) (fmtstr.FormatEvaler, error) {
Copy link

Choose a reason for hiding this comment

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

Any plan to handle ops? Event format strings can have defaults if the key is missing in the event. See: https://github.com/elastic/beats/blob/master/libbeat/common/fmtstr/formatevents.go#L280

One can use %{[my.field]:defaultValue} in event format strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now to keep it as simple as possible I suggest to return an error in case any ops are used.

@monicasarbu monicasarbu added libbeat discuss Issue needs further discussion. labels Aug 15, 2017
Copy link
Contributor

@tsg tsg left a comment

Choose a reason for hiding this comment

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

Tested this one manually, looks good 👍

With the current template loading is was not possible to overload the pattern used as name and pattern were mixed. For example in the case where someone wanted to define pattern name `a` that applies to the indice `a` this was not possible because the pattern generated was `a-*` so it didn't apply to the index `a`. This change now allows to set pattern and name separately.

To make sure index pattern and templates are in sync, the beat will not start in case the index pattern was modified but the template patterns not. We should figure out later some automated mechanisms here.

We should find a better solution for 6.x to require less config options and also include dashboard generation in this.
@ruflin
Copy link
Contributor Author

ruflin commented Aug 21, 2017

Rebased and squashed again.

@ruflin ruflin added needs_backport PR is waiting to be backported to other branches. needs_docs labels Aug 21, 2017
@ruflin
Copy link
Contributor Author

ruflin commented Aug 21, 2017

jenkins, test it

@ruflin
Copy link
Contributor Author

ruflin commented Aug 21, 2017

jenkins, retest it

@urso urso merged commit 02a59a1 into elastic:master Aug 21, 2017
@ruflin ruflin deleted the template-pattern branch August 22, 2017 06:24
@ruflin ruflin removed the needs_backport PR is waiting to be backported to other branches. label Aug 22, 2017
ruflin added a commit to ruflin/beats that referenced this pull request Aug 22, 2017
With the current template loading is was not possible to overload the pattern used as name and pattern were mixed. For example in the case where someone wanted to define pattern name `a` that applies to the indice `a` this was not possible because the pattern generated was `a-*` so it didn't apply to the index `a`. This change now allows to set pattern and name separately.

To make sure index pattern and templates are in sync, the beat will not start in case the index pattern was modified but the template patterns not. We should figure out later some automated mechanisms here.

We should find a better solution for 6.x to require less config options and also include dashboard generation in this.

(cherry picked from commit 02a59a1)
@tsg tsg added the needs_backport PR is waiting to be backported to other branches. label Aug 22, 2017
tsg pushed a commit that referenced this pull request Aug 22, 2017
With the current template loading is was not possible to overload the pattern used as name and pattern were mixed. For example in the case where someone wanted to define pattern name `a` that applies to the indice `a` this was not possible because the pattern generated was `a-*` so it didn't apply to the index `a`. This change now allows to set pattern and name separately.

To make sure index pattern and templates are in sync, the beat will not start in case the index pattern was modified but the template patterns not. We should figure out later some automated mechanisms here.

We should find a better solution for 6.x to require less config options and also include dashboard generation in this.

(cherry picked from commit 02a59a1)
@tsg tsg removed the needs_backport PR is waiting to be backported to other branches. label Aug 22, 2017
ruflin added a commit to ruflin/apm-server that referenced this pull request Aug 23, 2017
Changes:
* Add support for cloud id. See elastic/beats#4964 for more details. This could simplify the onboard for users that use apm-server with the Cloud in the future.
* Be able to overwrite index pattern in the index template. See elastic/beats#4769 for more details. This allows to clean up our system tests.
ruflin added a commit to ruflin/apm-server that referenced this pull request Aug 23, 2017
Important changes / additions:

* Add support for cloud id. See elastic/beats#4964 for more details. This could simplify the onboard for users that use apm-server with the Cloud in the future.
* Be able to overwrite index pattern in the index template. See elastic/beats#4769 for more details. This allows to clean up our system tests.
ruflin added a commit to ruflin/apm-server that referenced this pull request Aug 23, 2017
Important changes / additions:

* Add support for cloud id. See elastic/beats#4964 for more details. This could simplify the onboard for users that use apm-server with the Cloud in the future.
* Be able to overwrite index pattern in the index template. See elastic/beats#4769 for more details. This allows to clean up our system tests.
simitt pushed a commit to elastic/apm-server that referenced this pull request Aug 24, 2017
Important changes / additions:

* Add support for cloud id. See elastic/beats#4964 for more details. This could simplify the onboard for users that use apm-server with the Cloud in the future.
* Be able to overwrite index pattern in the index template. See elastic/beats#4769 for more details. This allows to clean up our system tests.
@dedemorton
Copy link
Contributor

Removing needs_docs labels because template.name and template.pattern options are now documented. If additional doc work is required, please open an issue against the docs.

leweafan pushed a commit to leweafan/beats that referenced this pull request Apr 28, 2023
With the current template loading is was not possible to overload the pattern used as name and pattern were mixed. For example in the case where someone wanted to define pattern name `a` that applies to the indice `a` this was not possible because the pattern generated was `a-*` so it didn't apply to the index `a`. This change now allows to set pattern and name separately.

To make sure index pattern and templates are in sync, the beat will not start in case the index pattern was modified but the template patterns not. We should figure out later some automated mechanisms here.

We should find a better solution for 6.x to require less config options and also include dashboard generation in this.

(cherry picked from commit 2a8f795)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants