Class: Fluent::Plugin::ElasticsearchOutputDataStream

Inherits:
ElasticsearchOutput show all
Defined in:
lib/fluent/plugin/out_elasticsearch_data_stream.rb

Constant Summary collapse

INVALID_START_CHRACTERS =
["-", "_", "+", "."]
INVALID_CHARACTERS =
["\\", "/", "*", "?", "\"", "<", ">", "|", " ", ",", "#", ":"]

Constants inherited from ElasticsearchOutput

Fluent::Plugin::ElasticsearchOutput::DEFAULT_BUFFER_TYPE, Fluent::Plugin::ElasticsearchOutput::DEFAULT_ELASTICSEARCH_VERSION, Fluent::Plugin::ElasticsearchOutput::DEFAULT_POLICY_ID, Fluent::Plugin::ElasticsearchOutput::DEFAULT_RELOAD_AFTER, Fluent::Plugin::ElasticsearchOutput::DEFAULT_TYPE_NAME, Fluent::Plugin::ElasticsearchOutput::DEFAULT_TYPE_NAME_ES_7x, Fluent::Plugin::ElasticsearchOutput::TARGET_BULK_BYTES

Constants included from ElasticsearchTLS

Fluent::Plugin::ElasticsearchTLS::DEFAULT_VERSION, Fluent::Plugin::ElasticsearchTLS::SUPPORTED_TLS_VERSIONS

Constants included from ElasticsearchIndexLifecycleManagement

Fluent::Plugin::ElasticsearchIndexLifecycleManagement::ILM_DEFAULT_POLICY_PATH

Constants included from ElasticsearchConstants

Fluent::Plugin::ElasticsearchConstants::BODY_DELIMITER, Fluent::Plugin::ElasticsearchConstants::CREATE_OP, Fluent::Plugin::ElasticsearchConstants::ID_FIELD, Fluent::Plugin::ElasticsearchConstants::INDEX_OP, Fluent::Plugin::ElasticsearchConstants::TIMESTAMP_FIELD, Fluent::Plugin::ElasticsearchConstants::UPDATE_OP, Fluent::Plugin::ElasticsearchConstants::UPSERT_OP

Instance Attribute Summary

Attributes inherited from ElasticsearchOutput

#alias_indexes, #api_key_header, #compressable_connection, #ssl_version_options, #template_names

Instance Method Summary collapse

Methods inherited from ElasticsearchOutput

#backend_options, #client, #cloud_client, #compression, #compression_strategy, #configure_routing_key_name, #connection_options_description, #convert_compat_id_key, #convert_numeric_time_into_string, #create_meta_config_map, #create_time_parser, #detect_es_major_version, #dry_run?, #expand_placeholders, #flatten_record, #get_connection_options, #get_escaped_userinfo, #get_parent_of, #gzip, #handle_last_seen_es_major_version, #initialize, #inject_chunk_id_to_record_if_needed, #is_existing_connection, #parse_time, #placeholder?, #placeholder_substitution_needed_for_template?, #process_message, #remove_keys, #send_bulk, #setup_api_key, #split_request?, #split_request_size_check?, #split_request_size_uncheck?, #template_installation, #template_installation_actual, #update_body

Methods included from ElasticsearchTLS

included, #set_tls_minmax_version_config

Methods included from ElasticsearchIndexLifecycleManagement

#default_policy_payload, #get_ilm_policy, #ilm_policy_exists?, #ilm_policy_put, #setup_ilm, #verify_ilm_working, #xpack_info

Methods included from ElasticsearchIndexTemplate

#create_rollover_alias, #get_custom_template, #get_template, #get_template_name, #indexcreation, #inject_ilm_settings_to_template, #retry_operate, #rollover_alias_payload, #template_custom_install, #template_exists?, #template_install, #template_put, #templates_hash_install

Constructor Details

This class inherits a constructor from Fluent::Plugin::ElasticsearchOutput

Instance Method Details

#append_record_to_messages(op, meta, header, record, msgs) ⇒ Object



207
208
209
210
211
212
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 207

def append_record_to_messages(op, meta, header, record, msgs)
  header[CREATE_OP] = meta
  msgs << @dump_proc.call(header) << BODY_DELIMITER
  msgs << @dump_proc.call(record) << BODY_DELIMITER
  msgs
end

#client_library_versionObject



153
154
155
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 153

def client_library_version
  Elasticsearch::VERSION
end

#configure(conf) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 17

def configure(conf)
  super

  begin
    require 'elasticsearch/api'
    require 'elasticsearch/xpack'
  rescue LoadError
    raise Fluent::ConfigError, "'elasticsearch/api', 'elasticsearch/xpack' are required for <@elasticsearch_data_stream>."
  end

  # ref. https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-create-data-stream.html
  unless placeholder?(:data_stream_name_placeholder, @data_stream_name)
    validate_data_stream_name
  else
    @use_placeholder = true
    @data_stream_names = []
  end

  @client = client
  unless @use_placeholder
    begin
      @data_stream_names = [@data_stream_name]
      create_ilm_policy(@data_stream_name)
      create_index_template(@data_stream_name)
      create_data_stream(@data_stream_name)
    rescue => e
      raise Fluent::ConfigError, "Failed to create data stream: <#{@data_stream_name}> #{e.message}"
    end
  end
end

#create_data_stream(name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 117

def create_data_stream(name)
  return if data_stream_exist?(name)
  params = {
    "name": name
  }
  retry_operate(@max_retry_putting_template,
                @fail_on_putting_template_retry_exceed,
                @catch_transport_exception_on_retry) do
    @client.indices.create_data_stream(params)
  end
end

#create_ilm_policy(name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 69

def create_ilm_policy(name)
  return if data_stream_exist?(name)
  params = {
    policy_id: "#{name}_policy",
    body: File.read(File.join(File.dirname(__FILE__), "default-ilm-policy.json"))
  }
  retry_operate(@max_retry_putting_template,
                @fail_on_putting_template_retry_exceed,
                @catch_transport_exception_on_retry) do
    @client.xpack.ilm.put_policy(params)
  end
end

#create_index_template(name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 82

def create_index_template(name)
  return if data_stream_exist?(name)
  body = {
    "index_patterns" => ["#{name}*"],
    "data_stream" => {},
    "template" => {
      "settings" => {
        "index.lifecycle.name" => "#{name}_policy"
      }
    }
  }
  params = {
    name: name,
    body: body
  }
  retry_operate(@max_retry_putting_template,
                @fail_on_putting_template_retry_exceed,
                @catch_transport_exception_on_retry) do
    @client.indices.put_index_template(params)
  end
end

#data_stream_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 104

def data_stream_exist?(name)
  params = {
    "name": name
  }
  begin
    response = @client.indices.get_data_stream(params)
    return (not response.is_a?(Elasticsearch::Transport::Transport::Errors::NotFound))
  rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
    log.info "Specified data stream does not exist. Will be created: <#{e}>"
    return false
  end
end

#lowercase_only?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 137

def lowercase_only?
  @data_stream_name.downcase == @data_stream_name
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 157

def multi_workers_ready?
  true
end

#not_dots?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 149

def not_dots?
  not (@data_stream_name == "." or @data_stream_name == "..")
end

#retry_stream_retryable?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 214

def retry_stream_retryable?
  @buffer.storable?
end

#start_with_valid_characters?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 145

def start_with_valid_characters?
  not (INVALID_START_CHRACTERS.each.any? do |v| @data_stream_name.start_with?(v) end)
end

#valid_characters?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 141

def valid_characters?
  not (INVALID_CHARACTERS.each.any? do |v| @data_stream_name.include?(v) end)
end

#valid_data_stream_name?Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 129

def valid_data_stream_name?
  lowercase_only? and
    valid_characters? and
    start_with_valid_characters? and
    not_dots? and
    @data_stream_name.bytes.size <= 255
end

#validate_data_stream_nameObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 48

def validate_data_stream_name
  unless valid_data_stream_name?
    unless start_with_valid_characters?
      if not_dots?
        raise Fluent::ConfigError, "'data_stream_name' must not start with #{INVALID_START_CHRACTERS.join(",")}: <#{@data_stream_name}>"
      else
        raise Fluent::ConfigError, "'data_stream_name' must not be . or ..: <#{@data_stream_name}>"
      end
    end
    unless valid_characters?
      raise Fluent::ConfigError, "'data_stream_name' must not contain invalid characters #{INVALID_CHARACTERS.join(",")}: <#{@data_stream_name}>"
    end
    unless lowercase_only?
      raise Fluent::ConfigError, "'data_stream_name' must be lowercase only: <#{@data_stream_name}>"
    end
    if @data_stream_name.bytes.size > 255
      raise Fluent::ConfigError, "'data_stream_name' must not be longer than 255 bytes: <#{@data_stream_name}>"
    end
  end
end

#write(chunk) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/fluent/plugin/out_elasticsearch_data_stream.rb', line 161

def write(chunk)
  data_stream_name = @data_stream_name
  if @use_placeholder
    data_stream_name = extract_placeholders(@data_stream_name, chunk)
    unless @data_stream_names.include?(data_stream_name)
      begin
        create_ilm_policy(data_stream_name)
        create_index_template(data_stream_name)
        create_data_stream(data_stream_name)
        @data_stream_names << data_stream_name
      rescue => e
        raise Fluent::ConfigError, "Failed to create data stream: <#{data_stream_name}> #{e.message}"
      end
    end
  end

  bulk_message = ""
  headers = {
    CREATE_OP => {}
  }
  tag = chunk..tag
  chunk.msgpack_each do |time, record|
    next unless record.is_a? Hash

    begin
      record.merge!({"@timestamp" => Time.at(time).iso8601(@time_precision)})
      bulk_message = append_record_to_messages(CREATE_OP, {}, headers, record, bulk_message)
    rescue => e
      router.emit_error_event(tag, time, record, e)
    end
  end

  params = {
    index: data_stream_name,
    body: bulk_message
  }
  begin
    response = @client.bulk(params)
    if response['errors']
      log.error "Could not bulk insert to Data Stream: #{data_stream_name} #{response}"
    end
  rescue => e
    log.error "Could not bulk insert to Data Stream: #{data_stream_name} #{e.message}"
  end
end