Skip to content

Commit 0601fc6

Browse files
committed
add ecs compatibility mode v1
1 parent 7f5785d commit 0601fc6

File tree

12 files changed

+6015
-20
lines changed

12 files changed

+6015
-20
lines changed

Diff for: Rakefile

+27
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
11
require "logstash/devutils/rake"
2+
3+
ECS_VERSIONS = {
4+
v1: 'v1.5.0'
5+
}
6+
7+
task 'vendor-ecs-schemata' do
8+
download_ecs_schema(:v1, 6)
9+
download_ecs_schema(:v1, 7)
10+
end
11+
12+
def download_ecs_schema(ecs_major_version, es_major)
13+
require 'net/http'
14+
require 'json'
15+
Net::HTTP.start('raw.githubusercontent.com', :use_ssl => true) do |http|
16+
ecs_release_tag = ECS_VERSIONS.fetch(ecs_major_version)
17+
response = http.get("/elastic/ecs/#{ecs_release_tag}/generated/elasticsearch/#{es_major}/template.json")
18+
fail "#{response.code} #{response.message}" unless (200...300).cover?(response.code)
19+
template_directory = File.expand_path("../lib/logstash/outputs/elasticsearch/templates/ecs-#{ecs_major_version}", __FILE__)
20+
Dir.mkdir(template_directory) unless File.exists?(template_directory)
21+
File.open(File.join(template_directory, "/elasticsearch-#{es_major}x.json"), "w") do |handle|
22+
template = JSON.load(response.body)
23+
template.fetch('index_patterns').clear
24+
template.fetch('index_patterns') << 'ecs-logstash-*'
25+
handle.write(JSON.pretty_generate(template))
26+
end
27+
end
28+
end

Diff for: docs/index.asciidoc

+32-4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
234234
| <<plugins-{type}s-{plugin}-doc_as_upsert>> |<<boolean,boolean>>|No
235235
| <<plugins-{type}s-{plugin}-document_id>> |<<string,string>>|No
236236
| <<plugins-{type}s-{plugin}-document_type>> |<<string,string>>|No
237+
| <<plugins-{type}s-{plugin}-ecs_compatibility>> | <<string,string>>|No
237238
| <<plugins-{type}s-{plugin}-failure_type_logging_whitelist>> |<<array,array>>|No
238239
| <<plugins-{type}s-{plugin}-healthcheck_path>> |<<string,string>>|No
239240
| <<plugins-{type}s-{plugin}-hosts>> |<<uri,uri>>|No
@@ -393,6 +394,25 @@ If you don't set a value for this option:
393394
- for elasticsearch clusters 6.x: the value of 'doc' will be used;
394395
- for elasticsearch clusters 5.x and below: the event's 'type' field will be used, if the field is not present the value of 'doc' will be used.
395396

397+
[id="plugins-{type}s-{plugin}-ecs_compatibility"]
398+
===== `ecs_compatibility`
399+
400+
* Value type is <<string,string>>
401+
* Supported values are:
402+
** `disabled`: does not provide ECS-compatible templates
403+
** `v1`: provides defaults that are compatible with v1 of the Elastic Common Schema
404+
* Default value depends on which version of Logstash is running:
405+
** When Logstash provides a `pipeline.ecs_compatibility` setting, its value is used as the default
406+
** Otherwise, the default value is `disabled`.
407+
408+
Controls this plugin's desired compatibility with the https://www.elastic.co/blog/introducing-the-elastic-common-schema[Elastic Common Schema],
409+
including the installation of ECS-Compatible index templates.
410+
The value of this setting affects the _default_ values of:
411+
412+
* <<plugins-{type}s-{plugin}-index>>
413+
* <<plugins-{type}s-{plugin}-template_name>>
414+
* <<plugins-{type}s-{plugin}-ilm_rollover_alias>>
415+
396416
[id="plugins-{type}s-{plugin}-failure_type_logging_whitelist"]
397417
===== `failure_type_logging_whitelist`
398418

@@ -500,7 +520,9 @@ NOTE: If this setting is specified, the policy must already exist in Elasticsear
500520
===== `ilm_rollover_alias`
501521

502522
* Value type is <<string,string>>
503-
* Default value is `logstash`
523+
* Default value depends on whether <<plugins-{type}s-{plugin}-ecs_compatibility>> is enabled:
524+
** ECS Compatibility disabled: `logstash`
525+
** ECS Compatibility enabled: `ecs-logstash` (ECS Compatibility is enabled)
504526

505527
The rollover alias is the alias where indices managed using Index Lifecycle Management will be written to.
506528

@@ -514,7 +536,9 @@ NOTE: `ilm_rollover_alias` does NOT support dynamic variable substitution as `in
514536
===== `index`
515537

516538
* Value type is <<string,string>>
517-
* Default value is `"logstash-%{+yyyy.MM.dd}"`
539+
* Default value depends on whether <<plugins-{type}s-{plugin}-ecs_compatibility>> is enabled:
540+
** ECS Compatibility disabled: `"logstash-%{+yyyy.MM.dd}"`
541+
** ECS Compatibility enabled: `"ecs-logstash-%{+yyyy.MM.dd}"`
518542

519543
The index to write events to. This can be dynamic using the `%{foo}` syntax.
520544
The default value will partition your indices by day so you can more easily
@@ -548,7 +572,8 @@ Set the keystore password
548572
* Default value is `true`
549573

550574
From Logstash 1.3 onwards, a template is applied to Elasticsearch during
551-
Logstash's startup if one with the name `template_name` does not already exist.
575+
Logstash's startup if one with the name <<plugins-{type}s-{plugin}-template_name>>
576+
does not already exist.
552577
By default, the contents of this template is the default template for
553578
`logstash-%{+YYYY.MM.dd}` which always matches indices based on the pattern
554579
`logstash-*`. Should you require support for other index names, or would like
@@ -799,7 +824,10 @@ If not set, the included template will be used.
799824
===== `template_name`
800825

801826
* Value type is <<string,string>>
802-
* Default value is `"logstash"`
827+
* Default value depends on whether <<plugins-{type}s-{plugin}-ecs_compatibility>> is enabled:
828+
** ECS Compatibility disabled: `logstash`
829+
** ECS Compatibility enabled: `ecs-logstash`
830+
803831

804832
This configuration option defines how the template is named inside Elasticsearch.
805833
Note that if you have used the template management features and subsequently

Diff for: lib/logstash/outputs/elasticsearch.rb

+33
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base
9292
require "logstash/outputs/elasticsearch/common"
9393
require "logstash/outputs/elasticsearch/ilm"
9494

95+
require 'logstash/plugin_mixins/ecs_compatibility_support'
96+
9597
# Protocol agnostic (i.e. non-http, non-java specific) configs go here
9698
include(LogStash::Outputs::ElasticSearch::CommonConfigs)
9799

@@ -101,6 +103,9 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base
101103
# Methods for ILM support
102104
include(LogStash::Outputs::ElasticSearch::Ilm)
103105

106+
# ecs_compatibility option, provided by Logstash core or the support adapter.
107+
include(LogStash::PluginMixins::ECSCompatibilitySupport)
108+
104109
config_name "elasticsearch"
105110

106111
# The Elasticsearch action to perform. Valid actions are:
@@ -242,6 +247,34 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base
242247
# Custom Headers to send on each request to elasticsearch nodes
243248
config :custom_headers, :validate => :hash, :default => {}
244249

250+
def register
251+
setup_ecs_compatibility_related_defaults
252+
super
253+
end
254+
255+
def setup_ecs_compatibility_related_defaults
256+
case ecs_compatibility
257+
when :disabled
258+
@default_index = "logstash-%{+yyyy.MM.dd}"
259+
@default_ilm_rollover_alias = "logstash"
260+
@default_template_name = 'logstash'
261+
when :v1
262+
@default_index = "ecs-logstash-%{+yyyy.MM.dd}"
263+
@default_ilm_rollover_alias = "ecs-logstash"
264+
@default_template_name = 'ecs-logstash'
265+
else
266+
fail("unsupported ECS Compatibility `#{ecs_compatibility}`")
267+
end
268+
269+
@index ||= default_index
270+
@ilm_rollover_alias ||= default_ilm_rollover_alias
271+
@template_name ||= default_template_name
272+
end
273+
274+
attr_reader :default_index
275+
attr_reader :default_ilm_rollover_alias
276+
attr_reader :default_template_name
277+
245278
# @override to handle proxy => '' as if none was set
246279
def config_init(params)
247280
proxy = params['proxy']

Diff for: lib/logstash/outputs/elasticsearch/common.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def safe_bulk(actions)
436436
end
437437

438438
def default_index?(index)
439-
@index == LogStash::Outputs::ElasticSearch::CommonConfigs::DEFAULT_INDEX_NAME
439+
@index == @default_index
440440
end
441441

442442
def dlq_enabled?

Diff for: lib/logstash/outputs/elasticsearch/common_configs.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def self.included(mod)
1717
# For weekly indexes ISO 8601 format is recommended, eg. logstash-%{+xxxx.ww}.
1818
# LS uses Joda to format the index pattern from event timestamp.
1919
# Joda formats are defined http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html[here].
20-
mod.config :index, :validate => :string, :default => DEFAULT_INDEX_NAME
20+
mod.config :index, :validate => :string
2121

2222
mod.config :document_type,
2323
:validate => :string,
@@ -44,7 +44,7 @@ def self.included(mod)
4444
# `curl -XDELETE <http://localhost:9200/_template/OldTemplateName?pretty>`
4545
#
4646
# where `OldTemplateName` is whatever the former setting was.
47-
mod.config :template_name, :validate => :string, :default => "logstash"
47+
mod.config :template_name, :validate => :string
4848

4949
# You can set the path to your own template here, if you so desire.
5050
# If not set, the included template will be used.
@@ -153,7 +153,7 @@ def self.included(mod)
153153
mod.config :ilm_enabled, :validate => [true, false, 'true', 'false', 'auto'], :default => 'auto'
154154

155155
# Rollover alias used for indexing data. If rollover alias doesn't exist, Logstash will create it and map it to the relevant index
156-
mod.config :ilm_rollover_alias, :validate => :string, :default => DEFAULT_ROLLOVER_ALIAS
156+
mod.config :ilm_rollover_alias, :validate => :string
157157

158158
# appends “{now/d}-000001” by default for new index creation, subsequent rollover indices will increment based on this pattern i.e. “000002”
159159
# {now/d} is date math, and will insert the appropriate value automatically.

Diff for: lib/logstash/outputs/elasticsearch/ilm.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def setup_ilm
1212
end
1313

1414
def default_rollover_alias?(rollover_alias)
15-
rollover_alias == LogStash::Outputs::ElasticSearch::DEFAULT_ROLLOVER_ALIAS
15+
rollover_alias == default_ilm_rollover_alias
1616
end
1717

1818
def ilm_alias_set?

Diff for: lib/logstash/outputs/elasticsearch/template_manager.rb

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ class TemplateManager
33
# To be mixed into the elasticsearch plugin base
44
def self.install_template(plugin)
55
return unless plugin.manage_template
6-
if plugin.template.nil?
7-
plugin.logger.info("Using default mapping template")
8-
else
6+
if plugin.template
97
plugin.logger.info("Using mapping template from", :path => plugin.template)
8+
template = read_template_file(plugin.template)
9+
else
10+
plugin.logger.info("Using default mapping template")
11+
template = load_default_template(plugin.maximum_seen_major_version, plugin.ecs_compatibility)
1012
end
1113

12-
13-
template = get_template(plugin.template, plugin.maximum_seen_major_version)
1414
add_ilm_settings_to_template(plugin, template) if plugin.ilm_in_use?
1515
plugin.logger.info("Attempting to install template", :manage_template => template)
1616
install(plugin.client, template_name(plugin), template, plugin.template_overwrite)
@@ -19,9 +19,11 @@ def self.install_template(plugin)
1919
end
2020

2121
private
22-
def self.get_template(path, es_major_version)
23-
template_path = path || default_template_path(es_major_version)
22+
def self.load_default_template(es_major_version, ecs_compatibility)
23+
template_path = default_template_path(es_major_version, ecs_compatibility)
2424
read_template_file(template_path)
25+
rescue => e
26+
fail "Failed to load default template for Elasticsearch v#{es_major_version} with ECS #{ecs_compatibility}; caused by: #{e.message}"
2527
end
2628

2729
def self.install(client, template_name, template, template_overwrite)
@@ -46,9 +48,9 @@ def self.template_name(plugin)
4648
plugin.ilm_in_use? && !plugin.original_params.key?('template_name') ? plugin.ilm_rollover_alias : plugin.template_name
4749
end
4850

49-
def self.default_template_path(es_major_version)
51+
def self.default_template_path(es_major_version, ecs_compatibility=:disabled)
5052
template_version = es_major_version == 1 ? 2 : es_major_version
51-
default_template_name = "templates/ecs-disabled/elasticsearch-#{template_version}x.json"
53+
default_template_name = "templates/ecs-#{ecs_compatibility}/elasticsearch-#{template_version}x.json"
5254
::File.expand_path(default_template_name, ::File.dirname(__FILE__))
5355
end
5456

0 commit comments

Comments
 (0)