Skip to content

Commit 261abff

Browse files
committed
Allow date math in pattern
1 parent 8e57da4 commit 261abff

File tree

6 files changed

+61
-16
lines changed

6 files changed

+61
-16
lines changed

lib/logstash/outputs/elasticsearch/common.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def maybe_create_write_alias
433433
end
434434

435435
def write_alias_target
436-
"#{ilm_write_alias}-#{ilm_pattern}"
436+
"<#{ilm_write_alias}-#{ilm_pattern}>"
437437
end
438438

439439
def write_alias_payload
@@ -446,6 +446,8 @@ def write_alias_payload
446446
}
447447
end
448448

449+
450+
449451
def policy_payload
450452
policy_path = ::File.expand_path(ILM_POLICY_PATH, ::File.dirname(__FILE__))
451453
LogStash::Json.load(::IO.read(policy_path))

lib/logstash/outputs/elasticsearch/common_configs.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def self.included(mod)
149149
# Write alias used for indexing data. If write alias doesn't exist, Logstash will create it and map it to the relevant index
150150
mod.config :ilm_write_alias, :validate => :string, :default => 'logstash'
151151

152-
# appends “000001” by default for new index creation, subsequent rollover indices will increment based on this pattern i.e. “000002”
153-
mod.config :ilm_pattern, :validate => :string, :default => '000001'
152+
# appends “{now/d}-000001” by default for new index creation, subsequent rollover indices will increment based on this pattern i.e. “000002”
153+
mod.config :ilm_pattern, :validate => :string, :default => '{now/d}-000001'
154154

155155
# ILM policy to use, if undefined the default policy will be used.
156156
mod.config :ilm_policy, :validate => :string, :default => 'logstash-policy'

lib/logstash/outputs/elasticsearch/http_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def write_alias_exists?(name)
362362
# Create a new write alias
363363
def write_alias_put(alias_name, alias_definition)
364364
logger.info("Creating write alias #{alias_name}")
365-
@pool.put(alias_name, nil, LogStash::Json.dump(alias_definition))
365+
@pool.put(CGI::escape(alias_name), nil, LogStash::Json.dump(alias_definition))
366366
end
367367

368368
def get_xpack_info

lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ def format_url(url, path_and_query=nil)
103103
end
104104

105105
request_uri.query = new_query_parts.join("&") unless new_query_parts.empty?
106-
107-
request_uri.path = "#{request_uri.path}/#{parsed_path_and_query.path}".gsub(/\/{2,}/, "/")
108-
106+
107+
request_uri.path = "#{request_uri.path}/#{parsed_path_and_query.raw_path}".gsub(/\/{2,}/, "/")
108+
109109
request_uri
110110
end
111111

spec/es_spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def doc_type
2525
end
2626
end
2727

28+
def todays_date
29+
Time.now.strftime("%Y.%m.%d")
30+
end
31+
2832
def mapping_name
2933
if ESHelper.es_version_satisfies?(">=7")
3034
"_doc"

spec/integration/outputs/ilm_spec.rb

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@
120120
res[index_written] += 1
121121
end
122122
expect(indexes_written.count).to eq(3)
123-
expect(indexes_written["#{expected_index}-000001"]).to eq(3)
124-
expect(indexes_written["#{expected_index}-000002"]).to eq(3)
125-
expect(indexes_written["#{expected_index}-000003"]).to eq(3)
123+
expect(indexes_written["#{expected_index}-#{todays_date}-000001"]).to eq(3)
124+
expect(indexes_written["#{expected_index}-#{todays_date}-000002"]).to eq(3)
125+
expect(indexes_written["#{expected_index}-#{todays_date}-000003"]).to eq(3)
126126
end
127127
end
128128

@@ -161,7 +161,7 @@
161161
res[index_written] += 1
162162
end
163163
expect(indexes_written.count).to eq(1)
164-
expect(indexes_written["#{expected_index}-000001"]).to eq(6)
164+
expect(indexes_written["#{expected_index}-#{todays_date}-000001"]).to eq(6)
165165
end
166166
end
167167
end
@@ -255,6 +255,18 @@
255255

256256

257257
context 'when using the default policy' do
258+
context 'with a custom pattern' do
259+
let (:settings) { super.merge("ilm_pattern" => "000001")}
260+
it 'should create a write alias' do
261+
expect(@es.indices.exists_alias(index: "logstash")).to be_falsey
262+
subject.register
263+
sleep(1)
264+
expect(@es.indices.exists_alias(index: "logstash")).to be_truthy
265+
expect(@es.get_alias(name: "logstash")).to include("logstash-000001")
266+
end
267+
end
268+
269+
258270
it 'should install it if it is not present' do
259271
expect{get_policy(@es, LogStash::Outputs::ElasticSearch::DEFAULT_POLICY)}.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
260272
subject.register
@@ -263,14 +275,14 @@
263275
end
264276

265277
it 'should create the default write alias' do
266-
267278
expect(@es.indices.exists_alias(index: "logstash")).to be_falsey
268279
subject.register
269280
sleep(1)
270281
expect(@es.indices.exists_alias(index: "logstash")).to be_truthy
271-
expect(@es.get_alias(name: "logstash")).to include("logstash-000001")
282+
expect(@es.get_alias(name: "logstash")).to include("logstash-#{todays_date}-000001")
272283
end
273284

285+
274286
it 'should ingest into a single index' do
275287
subject.register
276288

@@ -301,7 +313,7 @@
301313
end
302314

303315
expect(indexes_written.count).to eq(1)
304-
expect(indexes_written["logstash-000001"]).to eq(6)
316+
expect(indexes_written["logstash-#{todays_date}-000001"]).to eq(6)
305317
end
306318
end
307319

@@ -333,6 +345,33 @@
333345
end
334346
end
335347

348+
context 'when using a time based policy' do
349+
let (:ilm_policy_name) {"new_one"}
350+
let (:settings) { super.merge("ilm_policy" => ilm_policy_name)}
351+
let (:policy) {{
352+
"policy" => {
353+
"phases"=> {
354+
"hot" => {
355+
"actions" => {
356+
"rollover" => {
357+
"max_age" => "1d"
358+
}
359+
}
360+
}
361+
}
362+
}}}
363+
364+
before do
365+
expect{get_policy(@es, LogStash::Outputs::ElasticSearch::DEFAULT_POLICY)}.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
366+
put_policy(@es,ilm_policy_name, policy)
367+
end
368+
369+
it 'should not install the default policy if it is not used' do
370+
subject.register
371+
sleep(1)
372+
expect{get_policy(@es, LogStash::Outputs::ElasticSearch::DEFAULT_POLICY)}.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
373+
end
374+
end
336375
context 'with the default template' do
337376
let(:expected_index) { "logstash" }
338377

@@ -341,7 +380,7 @@
341380
subject.register
342381
sleep(1)
343382
expect(@es.indices.exists_alias(index: expected_index)).to be_truthy
344-
expect(@es.get_alias(name: expected_index)).to include("#{expected_index}-000001")
383+
expect(@es.get_alias(name: expected_index)).to include("#{expected_index}-#{todays_date}-000001")
345384
end
346385

347386
it 'should write the ILM settings into the template' do
@@ -382,7 +421,7 @@
382421
subject.register
383422
sleep(1)
384423
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_truthy
385-
expect(@es.get_alias(name: ilm_write_alias)).to include("#{ilm_write_alias}-000001")
424+
expect(@es.get_alias(name: ilm_write_alias)).to include("#{ilm_write_alias}-#{todays_date}-000001")
386425
end
387426

388427
it 'should write the ILM settings into the template' do

0 commit comments

Comments
 (0)