logstash-filter-memcached 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a5b909f6459b8a3d4ae405b3d9e076ed4c1bb7eab344dbc5f4a8a98c55268d0
4
- data.tar.gz: 4dbf8017bcf03a1f6462da74df69b1ca8485b57df2c585f772cf38a158858c94
3
+ metadata.gz: c0dac09d5a353628d2b9507a8eb576ef034128eb603edd1f4445640c9ac06e44
4
+ data.tar.gz: 408ca2ab17dfb94cf400c0ffd2675b7e250879d05bdefeb6ab19454c2cc908af
5
5
  SHA512:
6
- metadata.gz: bda94d2ee65f231e5a40bc8ffa4b32e8c04126694e74f66b1aee3e8a2e33cc56ca96707fb5cf2e1f29c8fd48e6a51d02e98ed70a2bcd5286cc9e2c7f2c1bc6a7
7
- data.tar.gz: d094cebcab25b085d0f8aca5d6611fb7654e7d9f5ee56f7c64250998a427742b39c1ee35afdffdc0b6c05341b55d81329445423369096a68cf92f737fc62e07c
6
+ metadata.gz: 621ae0fb00da753e3268c04c2cd8d6c11e1f85b56348ca5334385a565daa174726291bdd9f15543b3f1f82169dcce2e7386707defa38d920f9cc502676d0772c
7
+ data.tar.gz: da42210db0f5dfb326a48fb67acd63223bf866c9c8a1e3a397dafa892078b913b8bce3f37a9d194d8496a19a79cab8cb1d5b0553ff3ed383a69152018693a156
data/CHANGELOG.md CHANGED
@@ -1,2 +1,6 @@
1
+ ## 0.1.1
2
+ - The plugin common options (e.g., `add_field`, `add_tag`, etc.) are now correctly only invoked when the plugin successfully gets one or more values from, or sets one or more values to memcached (#4)
3
+ - Fix links to argument types in documentation (#3)
4
+
1
5
  ## 0.1.0
2
- - Plugin created with the logstash plugin generator
6
+ - Initial release
@@ -81,10 +81,10 @@ class LogStash::Filters::Memcached < LogStash::Filters::Base
81
81
  end # def register
82
82
 
83
83
  def filter(event)
84
- do_set(event)
85
- do_get(event)
84
+ set_success = do_set(event)
85
+ get_success = do_get(event)
86
86
 
87
- filter_matched(event)
87
+ filter_matched(event) if (set_success || get_success)
88
88
  end # def filter
89
89
 
90
90
  def close
@@ -94,7 +94,7 @@ class LogStash::Filters::Memcached < LogStash::Filters::Base
94
94
  private
95
95
 
96
96
  def do_get(event)
97
- return unless @get && !@get.empty?
97
+ return false unless @get && !@get.empty?
98
98
 
99
99
  event_fields_by_memcached_key = @get.each_with_object({}) do |(memcached_key_template, event_field), memo|
100
100
  memcached_key = event.sprintf(memcached_key_template)
@@ -104,19 +104,23 @@ class LogStash::Filters::Memcached < LogStash::Filters::Base
104
104
  memcached_keys = event_fields_by_memcached_key.keys
105
105
  cache_hits_by_memcached_key = cache.get_multi(memcached_keys)
106
106
 
107
+ cache_hits = 0
107
108
  event_fields_by_memcached_key.each do |memcached_key, event_field|
108
109
  value = cache_hits_by_memcached_key[memcached_key]
109
110
  if value.nil?
110
111
  logger.trace("cache:get miss", context(key: memcached_key))
111
112
  else
112
113
  logger.trace("cache:get hit", context(key: memcached_key, value: value))
114
+ cache_hits += 1
113
115
  event.set(event_field, value)
114
116
  end
115
117
  end
118
+
119
+ return cache_hits > 0
116
120
  end
117
121
 
118
122
  def do_set(event)
119
- return unless @set && !@set.empty?
123
+ return false unless @set && !@set.empty?
120
124
 
121
125
  values_by_memcached_key = @set.each_with_object({}) do |(event_field, memcached_key_template), memo|
122
126
  memcached_key = event.sprintf(memcached_key_template)
@@ -125,12 +129,16 @@ class LogStash::Filters::Memcached < LogStash::Filters::Base
125
129
  memo[memcached_key] = value unless value.nil?
126
130
  end
127
131
 
132
+ return false if values_by_memcached_key.empty?
133
+
128
134
  cache.multi do
129
135
  values_by_memcached_key.each do |memcached_key, value|
130
136
  logger.trace("cache:set", context(key: memcached_key, value: value))
131
137
  cache.set(memcached_key, value)
132
138
  end
133
139
  end
140
+
141
+ return true
134
142
  end
135
143
 
136
144
  def establish_connection
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'logstash-filter-memcached'
3
- s.version = '0.1.0'
3
+ s.version = '0.1.1'
4
4
  s.licenses = ['Apache-2.0']
5
5
  s.summary = 'A Logstash filter plugin for interacting with memcached'
6
6
  s.homepage = 'https://github.com/yaauie/logstash-filter-memcached'
@@ -5,16 +5,16 @@ require "logstash/filters/memcached"
5
5
  LogStash::Logging::Logger::configure_logging("TRACE")
6
6
 
7
7
  describe LogStash::Filters::Memcached do
8
- subject { described_class.new(config) }
8
+ subject(:memcached_filter) { described_class.new(config) }
9
9
  let(:cache) { double('memcached') }
10
10
  before(:each) do
11
- allow(subject).to receive(:establish_connection).and_return(cache)
12
- allow(subject).to receive(:close)
13
- subject.register
11
+ allow(memcached_filter).to receive(:establish_connection).and_return(cache)
12
+ allow(memcached_filter).to receive(:close)
13
+ memcached_filter.register
14
14
  end
15
15
 
16
16
  after(:each) do
17
- subject.close
17
+ memcached_filter.close
18
18
  end
19
19
 
20
20
  describe "#get" do
@@ -29,9 +29,26 @@ describe LogStash::Filters::Memcached do
29
29
 
30
30
  it "retrieves data from memcache" do
31
31
  expect(cache).to receive(:get_multi).with(["success/true/answer"]).and_return({"success/true/answer" => "42"})
32
- subject.filter(event)
32
+ expect(memcached_filter).to receive(:filter_matched)
33
+ memcached_filter.filter(event)
33
34
  expect(event.get("ultimate")).to eq("answer" => "42")
34
35
  end
36
+
37
+ context 'when memcached does not hold the value' do
38
+ before do
39
+ expect(cache).to receive(:get_multi).with(["success/true/answer"]).and_return({"success/true/answer" => nil})
40
+ end
41
+
42
+ it 'does not invoke `filter_matched`' do
43
+ expect(memcached_filter).to_not receive(:filter_matched)
44
+ memcached_filter.filter(event)
45
+ end
46
+
47
+ it 'does not populate the value' do
48
+ memcached_filter.filter(event)
49
+ expect(event.include?("ultimate")).to be false
50
+ end
51
+ end
35
52
  end
36
53
 
37
54
  describe "#set" do
@@ -42,12 +59,37 @@ describe LogStash::Filters::Memcached do
42
59
  "set" => { "[answer]" => "success/%{success}/answer" },
43
60
  }
44
61
  end
45
- let(:data) { { "answer" => "42", "success" => "true" } }
46
- before(:each) { allow(cache).to receive(:multi) {|&b| b.call } }
62
+ before(:each) do
63
+ allow(cache).to receive(:multi) {|&b| b.call }
64
+ allow(cache).to receive(:set)
65
+ end
66
+
67
+ context 'when the event includes the value to set' do
68
+ let(:data) { { "answer" => "42", "success" => "true" } }
69
+
70
+ it "sets data on memcached" do
71
+ expect(cache).to receive(:set).with("success/true/answer", "42")
72
+ memcached_filter.filter(event)
73
+ end
74
+
75
+ it 'invokes `filter_matched`' do
76
+ expect(memcached_filter).to receive(:filter_matched)
77
+ memcached_filter.filter(event)
78
+ end
79
+ end
80
+
81
+ context 'when the event does not include the value being set' do
82
+ let(:data) { { "success" => "true" } }
83
+
84
+ it 'does not set data on memcached' do
85
+ expect(cache).to_not receive(:set)
86
+ memcached_filter.filter(event)
87
+ end
47
88
 
48
- it "sets data on memcache" do
49
- expect(cache).to receive(:set).with("success/true/answer", "42")
50
- subject.filter(event)
89
+ it 'does not invoke `filter_matched`' do
90
+ expect(memcached_filter).to_not receive(:filter_matched)
91
+ memcached_filter.filter(event)
92
+ end
51
93
  end
52
94
  end
53
95
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-filter-memcached
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ry Biesemeyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-12 00:00:00.000000000 Z
11
+ date: 2019-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement