pwwka 0.5.2 → 0.6.0

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
  SHA1:
3
- metadata.gz: ab27810515e8fe7cc03313345d0b5b6b04940f68
4
- data.tar.gz: 3d39d267557796bcca834314fc5331049359ba21
3
+ metadata.gz: 374c2180b982d19cd0dcc1d6dd73abf3d80f493b
4
+ data.tar.gz: fb315b10d90d96cbb250c034fdf4a8d2f1186ac3
5
5
  SHA512:
6
- metadata.gz: 7d31540a45dab75922eee680c4a36b5ef9abad467ca095f2bcd0d833c8a7f1ba4f9ab0c436f2d33ae103577f091b8382fb63eb2f1c238d75eec9a68def1eacb4
7
- data.tar.gz: e9fc173987511462d63866c54f9b402124c3bf3abf0c2e1e22664a24152c197e046985b89518f984c5f1d3f07ba928a29aa744a48bda23f13ad3e8ea399ce957
6
+ metadata.gz: c6fa81ed74d72ec9f7739d8e7e4cd6de94ef172b5e7d11900e3ec32f03cc3c7f13a466fefc1107d43668b29a4c123d60838839de4fd8222d2a2284b25befd2b9
7
+ data.tar.gz: 885daa6a5d3615f113064c5a906d608d76aa1916f6174665221c12dd9270dfb553c4d9c3bbc0a36879a3aec335daeec22a1132f7e7375ab69508faf477146dd8
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.1.0
1
+ 2.3.1
data/.travis.yml CHANGED
@@ -1,9 +1,8 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
  rvm:
4
- - 2.1.0
5
- - 2.2.0
6
- - 2.0.0
4
+ - 2.3.1
5
+ - 2.2.4
7
6
  - ruby-head
8
7
  services:
9
8
  - rabbitmq
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pwwka (0.5.2)
4
+ pwwka (0.6.0)
5
5
  activemodel
6
6
  activesupport
7
7
  bunny
@@ -10,30 +10,30 @@ PATH
10
10
  GEM
11
11
  remote: https://www.rubygems.org/
12
12
  specs:
13
- activemodel (4.2.4)
14
- activesupport (= 4.2.4)
13
+ activemodel (4.2.6)
14
+ activesupport (= 4.2.6)
15
15
  builder (~> 3.1)
16
- activesupport (4.2.4)
16
+ activesupport (4.2.6)
17
17
  i18n (~> 0.7)
18
18
  json (~> 1.7, >= 1.7.7)
19
19
  minitest (~> 5.1)
20
20
  thread_safe (~> 0.3, >= 0.3.4)
21
21
  tzinfo (~> 1.1)
22
- amq-protocol (1.9.2)
22
+ amq-protocol (2.0.1)
23
23
  builder (3.2.2)
24
- bunny (2.0.0)
25
- amq-protocol (>= 1.9.2)
24
+ bunny (2.3.1)
25
+ amq-protocol (>= 2.0.1)
26
26
  diff-lcs (1.2.5)
27
27
  i18n (0.7.0)
28
28
  json (1.8.3)
29
- minitest (5.8.1)
29
+ minitest (5.8.4)
30
30
  mono_logger (1.1.0)
31
31
  multi_json (1.11.2)
32
32
  rack (1.6.4)
33
33
  rack-protection (1.5.3)
34
34
  rack
35
- rake (10.4.2)
36
- redis (3.2.1)
35
+ rake (10.5.0)
36
+ redis (3.2.2)
37
37
  redis-namespace (1.5.2)
38
38
  redis (~> 3.0, >= 3.0.4)
39
39
  resque (1.25.2)
@@ -42,25 +42,25 @@ GEM
42
42
  redis-namespace (~> 1.3)
43
43
  sinatra (>= 0.9.2)
44
44
  vegas (~> 0.1.2)
45
- rspec (3.3.0)
46
- rspec-core (~> 3.3.0)
47
- rspec-expectations (~> 3.3.0)
48
- rspec-mocks (~> 3.3.0)
49
- rspec-core (3.3.2)
50
- rspec-support (~> 3.3.0)
51
- rspec-expectations (3.3.1)
45
+ rspec (3.4.0)
46
+ rspec-core (~> 3.4.0)
47
+ rspec-expectations (~> 3.4.0)
48
+ rspec-mocks (~> 3.4.0)
49
+ rspec-core (3.4.3)
50
+ rspec-support (~> 3.4.0)
51
+ rspec-expectations (3.4.0)
52
52
  diff-lcs (>= 1.2.0, < 2.0)
53
- rspec-support (~> 3.3.0)
54
- rspec-mocks (3.3.2)
53
+ rspec-support (~> 3.4.0)
54
+ rspec-mocks (3.4.1)
55
55
  diff-lcs (>= 1.2.0, < 2.0)
56
- rspec-support (~> 3.3.0)
57
- rspec-support (3.3.0)
58
- sinatra (1.4.6)
59
- rack (~> 1.4)
56
+ rspec-support (~> 3.4.0)
57
+ rspec-support (3.4.1)
58
+ sinatra (1.4.7)
59
+ rack (~> 1.5)
60
60
  rack-protection (~> 1.4)
61
61
  tilt (>= 1.3, < 3)
62
62
  thread_safe (0.3.5)
63
- tilt (2.0.1)
63
+ tilt (2.0.2)
64
64
  tzinfo (1.2.2)
65
65
  thread_safe (~> 0.1)
66
66
  vegas (0.1.11)
@@ -76,4 +76,4 @@ DEPENDENCIES
76
76
  rspec
77
77
 
78
78
  BUNDLED WITH
79
- 1.10.6
79
+ 1.12.5
data/README.md CHANGED
@@ -281,6 +281,14 @@ The transmitter will likewise log an error if you use the `_safely` methods:
281
281
  error "Error Transmitting Message on #{routing_key} -> #{payload}: #{e}"
282
282
  ```
283
283
 
284
+ If your payloads are large, you may not want to log them 2-3 times per message. In that case, you can adjust `payload_logging` in the configuration:
285
+
286
+ ```ruby
287
+ Pwwka.configuration.payload_logging = :info # The default - payloads appear at INFO and above log levels
288
+ Pwwka.configuration.payload_logging = :error # Only log payloads for ERROR or FATAL messages
289
+ Pwwka.configuration.payload_logging = :fatal # Only log payloads for FATAL messages
290
+ ```
291
+
284
292
  ### Manual monitoring
285
293
 
286
294
  RabbitMQ has a web interface for checking out the health of connections, channels, exchanges and queues. Access it via the Heroku add-ons page for Enigma.
@@ -4,7 +4,7 @@ module Pwwka
4
4
  class ConfigurationError < StandardError; end
5
5
  class Configuration
6
6
 
7
- attr_accessor :rabbit_mq_host
7
+ attr_accessor :rabbit_mq_host
8
8
  attr_accessor :topic_exchange_name
9
9
  attr_accessor :delayed_exchange_name
10
10
  attr_accessor :logger
@@ -22,6 +22,13 @@ module Pwwka
22
22
  600, 600, 600] # longer-term outage?
23
23
  end
24
24
 
25
+ def payload_logging
26
+ @payload_logging || :info
27
+ end
28
+
29
+ def payload_logging=(new_payload_logging_level)
30
+ @payload_logging = new_payload_logging_level
31
+ end
25
32
 
26
33
  def allow_delayed?
27
34
  options[:allow_delayed]
data/lib/pwwka/logging.rb CHANGED
@@ -7,5 +7,22 @@ module Pwwka
7
7
  Pwwka.configuration.logger
8
8
  end
9
9
 
10
+ LEVELS = {
11
+ fatal: 5,
12
+ error: 4,
13
+ warn: 3,
14
+ info: 2,
15
+ debug: 1,
16
+ }
17
+
18
+ def logf(format,params)
19
+ level = params.delete(:at) || :info
20
+ params[:payload] = params["payload"] if params["payload"]
21
+ params[:payload] = "[omitted]" if params[:payload] && LEVELS[Pwwka.configuration.payload_logging.to_sym] > LEVELS[level.to_sym]
22
+ message = format % params
23
+ logger.send(level,message)
24
+ end
25
+
26
+
10
27
  end
11
28
  end
@@ -27,9 +27,9 @@ module Pwwka
27
27
  payload = ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(payload))
28
28
  handler_klass.handle!(delivery_info, properties, payload)
29
29
  receiver.ack(delivery_info.delivery_tag)
30
- info "Processed Message on #{queue_name} -> #{payload}, #{delivery_info.routing_key}"
30
+ logf "Processed Message on %{queue_name} -> %{payload}, %{routing_key}", queue_name: queue_name, payload: payload, routing_key: delivery_info.routing_key
31
31
  rescue => e
32
- error "Error Processing Message on #{queue_name} -> #{payload}, #{delivery_info.routing_key}: #{e}"
32
+ logf "Error Processing Message on %{queue_name} -> %{payload}, %{routing_key}: %{exception}", queue_name: queue_name, payload: payload, routing_key: delivery_info.routing_key, exception: e, at: :error
33
33
  # no requeue
34
34
  receiver.nack(delivery_info.delivery_tag)
35
35
  end
@@ -47,7 +47,7 @@ module Pwwka
47
47
  queue = channel.queue(queue_name, durable: true)
48
48
  queue.bind(topic_exchange, routing_key: routing_key)
49
49
  queue
50
- end
50
+ end
51
51
  end
52
52
 
53
53
  def ack(delivery_tag)
@@ -48,11 +48,11 @@ module Pwwka
48
48
  else
49
49
  new.send_message!(payload, routing_key)
50
50
  end
51
- info "AFTER Transmitting Message on #{routing_key} -> #{payload}"
52
-
51
+ logf "AFTER Transmitting Message on %{routing_key} -> %{payload}",routing_key: routing_key, payload: payload
52
+ true
53
53
  rescue => e
54
54
 
55
- error "ERROR Transmitting Message on #{routing_key} -> #{payload}: #{e}"
55
+ logf "ERROR Transmitting Message on %{routing_key} -> %{payload}", routing_key: routing_key, payload: payload, at: :error
56
56
 
57
57
  case on_error
58
58
 
@@ -92,21 +92,21 @@ module Pwwka
92
92
  end
93
93
 
94
94
  def send_message!(payload, routing_key)
95
- info "START Transmitting Message on #{routing_key} -> #{payload}"
95
+ logf "START Transmitting Message on %{routing_key} -> %{payload}", routing_key: routing_key, payload: payload
96
96
  channel_connector.topic_exchange.publish(
97
97
  payload.to_json,
98
98
  routing_key: routing_key,
99
99
  persistent: true)
100
100
  channel_connector.connection_close
101
101
  # if it gets this far it has succeeded
102
- info "END Transmitting Message on #{routing_key} -> #{payload}"
102
+ logf "END Transmitting Message on %{routing_key} -> %{payload}", routing_key: routing_key, payload: payload
103
103
  true
104
104
  end
105
105
 
106
106
 
107
107
  def send_delayed_message!(payload, routing_key, delay_by = DEFAULT_DELAY_BY_MS)
108
108
  channel_connector.raise_if_delayed_not_allowed
109
- info "START Transmitting Delayed Message on #{routing_key} -> #{payload}"
109
+ logf "START Transmitting Delayed Message on %{routing_key} -> %{payload}", routing_key: routing_key, payload: payload
110
110
  channel_connector.create_delayed_queue
111
111
  channel_connector.delayed_exchange.publish(
112
112
  payload.to_json,
@@ -115,7 +115,7 @@ module Pwwka
115
115
  persistent: true)
116
116
  channel_connector.connection_close
117
117
  # if it gets this far it has succeeded
118
- info "END Transmitting Delayed Message on #{routing_key} -> #{payload}"
118
+ logf "END Transmitting Delayed Message on %{routing_key} -> %{payload}", routing_key: routing_key, payload: payload
119
119
  true
120
120
  end
121
121
 
data/lib/pwwka/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pwwka
2
- VERSION = '0.5.2'
2
+ VERSION = '0.6.0'
3
3
  end
data/spec/logging_spec.rb CHANGED
@@ -16,4 +16,54 @@ describe Pwwka::Logging do
16
16
  end
17
17
 
18
18
  end
19
+
20
+ describe "#logf" do
21
+ let(:logger) { double(Logger) }
22
+
23
+ before do
24
+ @original_logger = Pwwka.configuration.logger
25
+ Pwwka.configuration.logger = logger
26
+ allow(logger).to receive(:info)
27
+ allow(logger).to receive(:error)
28
+ end
29
+
30
+ after do
31
+ Pwwka.configuration.logger = @original_logger
32
+ Pwwka.configuration.payload_logging = @original_payload_logging
33
+ end
34
+ it "logs a printf-style string at info" do
35
+ ForLogging.logf("This is %{test} some %{data}", test: "a test of", data: "data and stuff", ignored: :hopefully)
36
+ expect(logger).to have_received(:info).with("This is a test of some data and stuff")
37
+ end
38
+
39
+ it "can log an not-info" do
40
+ ForLogging.logf("This is %{test} some %{data}", test: "a test of", data: "data and stuff", ignored: :hopefully, at: :error)
41
+ expect(logger).to have_received(:error).with("This is a test of some data and stuff")
42
+ expect(logger).not_to have_received(:info)
43
+ end
44
+
45
+ context "payload-stripping" do
46
+ [
47
+ :payload,
48
+ "payload",
49
+ ].each do |name|
50
+ it "will strip payload (given as a #{name.class}) if configured" do
51
+ Pwwka.configuration.payload_logging = :error
52
+ ForLogging.logf("This is the payload: %{payload}", name => { foo: "bar" })
53
+ ForLogging.logf("This is also the payload: %{payload}", name => { foo: "bar" }, at: :error)
54
+ expect(logger).to have_received(:info).with("This is the payload: [omitted]")
55
+ expect(logger).to have_received(:error).with("This is also the payload: {:foo=>\"bar\"}")
56
+ end
57
+
58
+ it "will strip payload (given as a #{name.class}) of errors, too" do
59
+ Pwwka.configuration.payload_logging = :fatal
60
+ ForLogging.logf("This is the payload: %{payload}", name => { foo: "bar" })
61
+ ForLogging.logf("This is also the payload: %{payload}", name => { foo: "bar" }, at: :error)
62
+ expect(logger).to have_received(:info).with("This is the payload: [omitted]")
63
+ expect(logger).to have_received(:error).with("This is also the payload: [omitted]")
64
+ end
65
+ end
66
+ end
67
+
68
+ end
19
69
  end
@@ -8,23 +8,32 @@ describe Pwwka::Receiver do
8
8
  end
9
9
  end
10
10
 
11
- let(:payload) { Hash[:this, "that"] }
11
+ let(:payload) { { "this" => "that" } }
12
12
  let(:routing_key) { "this.that" }
13
13
  let(:queue_name) { "receiver_test" }
14
+ let(:logger) { double(Logger) }
14
15
 
15
16
  describe "::subscribe" do
16
17
 
17
18
  before(:each) do
19
+ @original_logger = Pwwka.configuration.logger
20
+ Pwwka.configuration.logger = logger
21
+ allow(logger).to receive(:info)
22
+ allow(logger).to receive(:error)
18
23
  @receiver = Pwwka::Receiver.subscribe(HandyHandler, "receiver_test", block: false)
19
24
  end
20
25
 
21
26
  after(:each) do
22
- @receiver.test_teardown
27
+ Pwwka.configuration.logger = @original_logger
28
+ @receiver.test_teardown rescue nil
23
29
  end
24
30
 
25
- it "should receive the sent message" do
31
+ it "should receive the sent message and log about it" do
26
32
  expect(HandyHandler).to receive(:handle!).and_return("made it here")
27
33
  Pwwka::Transmitter.send_message!(payload, routing_key)
34
+ expect(logger).to have_received(:info).with(/START Transmitting.*#{Regexp.escape(payload.to_s)}/)
35
+ expect(logger).to have_received(:info).with(/END Transmitting.*#{Regexp.escape(payload.to_s)}/)
36
+ expect(logger).to have_received(:info).with(/AFTER Transmitting.*#{Regexp.escape(payload.to_s)}/)
28
37
  end
29
38
 
30
39
  it "should nack the sent message if an error is raised" do
@@ -32,6 +41,11 @@ describe Pwwka::Receiver do
32
41
  expect(@receiver).not_to receive(:ack)
33
42
  expect(@receiver).to receive(:nack).with(instance_of(Fixnum))
34
43
  Pwwka::Transmitter.send_message!(payload, routing_key)
44
+ @receiver.test_teardown # force the message to be processed and exception handled
45
+ expect(logger).to have_received(:info).with(/START Transmitting.*#{Regexp.escape(payload.to_s)}/)
46
+ expect(logger).to have_received(:info).with(/END Transmitting.*#{Regexp.escape(payload.to_s)}/)
47
+ expect(logger).to have_received(:info).with(/AFTER Transmitting.*#{Regexp.escape(payload.to_s)}/)
48
+ expect(logger).to have_received(:error).with(/Error Processing Message.*#{Regexp.escape(payload.to_s)}/)
35
49
  end
36
50
 
37
51
  end
data/spec/spec_helper.rb CHANGED
@@ -10,10 +10,13 @@ RSpec.configure do |config|
10
10
  c.syntax = :expect
11
11
  end
12
12
 
13
- end
13
+ config.before(:each) do
14
+ Pwwka.configure do |c|
15
+ c.topic_exchange_name = "topics-test"
16
+ c.logger = MonoLogger.new("/dev/null")
17
+ c.options[:allow_delayed] = true
18
+ end
19
+ end
14
20
 
15
- Pwwka.configure do |config|
16
- config.topic_exchange_name = "topics-test"
17
- config.logger = MonoLogger.new("/dev/null")
18
- config.options[:allow_delayed] = true
19
21
  end
22
+
@@ -10,10 +10,22 @@ describe Pwwka::Transmitter do
10
10
  after(:each) { @test_handler.purge_test_queue }
11
11
  after(:all) { @test_handler.test_teardown }
12
12
 
13
- let(:payload) { Hash[:this, "that"] }
13
+ let(:payload) { { "this" => "that" } }
14
14
  let(:routing_key) { "this.that.and.theother" }
15
15
  let(:exception) { RuntimeError.new('blow up')}
16
+ let(:logger) { double(Logger) }
17
+
18
+ before(:each) do
19
+ @original_logger = Pwwka.configuration.logger
20
+ Pwwka.configuration.logger = logger
21
+ allow(logger).to receive(:info)
22
+ allow(logger).to receive(:warn)
23
+ allow(logger).to receive(:error)
24
+ end
16
25
 
26
+ after(:each) do
27
+ Pwwka.configuration.logger = @original_logger
28
+ end
17
29
 
18
30
  describe "#send_message!" do
19
31
 
@@ -23,6 +35,8 @@ describe Pwwka::Transmitter do
23
35
  expect(success).to be_truthy
24
36
  received_payload = @test_handler.pop_message.payload
25
37
  expect(received_payload["this"]).to eq("that")
38
+ expect(logger).to have_received(:info).with("START Transmitting Message on #{routing_key} -> #{payload}")
39
+ expect(logger).to have_received(:info).with("END Transmitting Message on #{routing_key} -> #{payload}")
26
40
  end
27
41
 
28
42
  it "should deliver on the expected routing key" do
@@ -34,10 +48,12 @@ describe Pwwka::Transmitter do
34
48
  end
35
49
 
36
50
  it "should blow up if exception raised" do
37
- expect(Pwwka::ChannelConnector).to receive(:new).and_raise(exception)
51
+ expect_any_instance_of(Pwwka::ChannelConnector).to receive(:topic_exchange).and_raise(exception)
38
52
  expect {
39
53
  Pwwka::Transmitter.new.send_message!(payload, routing_key)
40
54
  }.to raise_error(exception)
55
+ expect(logger).to have_received(:info).with("START Transmitting Message on #{routing_key} -> #{payload}")
56
+ expect(logger).not_to have_received(:info).with("END Transmitting Message on #{routing_key} -> #{payload}")
41
57
  end
42
58
 
43
59
  end
@@ -53,6 +69,8 @@ describe Pwwka::Transmitter do
53
69
  expect(@test_handler.test_queue.message_count).to eq(1)
54
70
  received_payload = @test_handler.pop_message.payload
55
71
  expect(received_payload["this"]).to eq("that")
72
+ expect(logger).to have_received(:info).with("START Transmitting Delayed Message on #{routing_key} -> #{payload}")
73
+ expect(logger).to have_received(:info).with("END Transmitting Delayed Message on #{routing_key} -> #{payload}")
56
74
  end
57
75
 
58
76
  it "should deliver on the expected routing key" do
@@ -65,10 +83,12 @@ describe Pwwka::Transmitter do
65
83
  end
66
84
 
67
85
  it "should blow up if exception raised" do
68
- expect(Pwwka::ChannelConnector).to receive(:new).and_raise(exception)
86
+ expect_any_instance_of(Pwwka::ChannelConnector).to receive(:create_delayed_queue).and_raise(exception)
69
87
  expect {
70
88
  Pwwka::Transmitter.new.send_delayed_message!(payload, routing_key, 1)
71
89
  }.to raise_error(exception)
90
+ expect(logger).to have_received(:info).with("START Transmitting Delayed Message on #{routing_key} -> #{payload}")
91
+ expect(logger).not_to have_received(:info).with("END Transmitting Delayed Message on #{routing_key} -> #{payload}")
72
92
  end
73
93
 
74
94
  context "delayed not configured" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pwwka
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stitch Fix Engineering
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2015-12-16 00:00:00.000000000 Z
18
+ date: 2016-09-14 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: bunny
@@ -188,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
188
188
  version: '0'
189
189
  requirements: []
190
190
  rubyforge_project:
191
- rubygems_version: 2.2.0
191
+ rubygems_version: 2.6.6
192
192
  signing_key:
193
193
  specification_version: 4
194
194
  summary: Send and receive messages via RabbitMQ