logjam_agent 0.21.1 → 0.22.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: ea9a62d060e64b9e7436c8f1035b2e4be80abab1
4
- data.tar.gz: 670e2bc6959c2e333ea179e0c3a8d7cf942d8053
3
+ metadata.gz: e060a1fcd946ce3bc723bb408a90a08c16e650fb
4
+ data.tar.gz: 6aba597f82bca08b2ba2d6e67969d2602909f902
5
5
  SHA512:
6
- metadata.gz: a4bfe1a2517ac4f1ae323d00bc1ac0a625b7736381ce748ecdc4adac671be39d7be1737b6025a97a66fcdf42507b740076af052dc4dbc2c974b5da1ee52a5349
7
- data.tar.gz: 71ba01d7b5a98417c12775ca842f050c02bed3ce518ba84f85ba27173e57e06692b8ac0fc020e06454db4906f237d2941bdfd01f4176e9b10f81f194273dd56f
6
+ metadata.gz: 36af34ac3b7cfae7f7e031fb24c2e14c8792c356688660b1df9f52cfcf837e81122ae341c5959f4462f5f657b302326a548ba3f499eb7d2c839368d168219653
7
+ data.tar.gz: 64c1a12ef9d4cce56fa863b5e002c46e2ace95c4f744df8199124c849f4b9590cdc1e099453409dee25cba82d368c04bcadae06c1b85862346f1363c176677a8
data/README.md CHANGED
@@ -39,13 +39,17 @@ module LogjamAgent
39
39
  # self.application_revision = "f494e11afa0738b279517a2a96101a952052da5d"
40
40
 
41
41
  # Configure request data forwarder for ZeroMQ. Default options as given below.
42
- # the host parameter can be a comma separted list of zmq connection specifictions,
43
- # where the protocol prefix and port suffix are optional.
42
+ # The host parameter can be a comma separted list of zmq connection specifictions,
43
+ # where the protocol prefix and port suffix are optional. req_port, rcv_timeo and
44
+ # snd_timeo options only apply for sychronous messages (LogjamAgent.event).
44
45
  add_forwarder(:zmq,
45
- :host => "localhost",
46
- :port => 9605,
47
- :linger => 1000,
48
- :snd_hwm => 100)
46
+ :host => "localhost",
47
+ :port => 9605,
48
+ :req_port => 9604,
49
+ :linger => 1000,
50
+ :snd_hwm => 100,
51
+ :rcv_timeo => 5000,
52
+ :snd_timeo => 5000)
49
53
 
50
54
  # Configure request data forwarder for AMQP.
51
55
  # add_forwarder(:amqp, :host => "message.broker.at.your.org")
@@ -126,7 +130,7 @@ LogjamAgent.error_handler = lambda {|exception| ... }
126
130
 
127
131
  The MIT License
128
132
 
129
- Copyright (c) 2013 - 2015 Stefan Kaes
133
+ Copyright (c) 2013 - 2016 Stefan Kaes
130
134
 
131
135
  Permission is hereby granted, free of charge, to any person obtaining a copy
132
136
  of this software and associated documentation files (the "Software"), to deal
@@ -1,3 +1,3 @@
1
1
  module LogjamAgent
2
- VERSION = "0.21.1"
2
+ VERSION = "0.22.0"
3
3
  end
@@ -4,6 +4,8 @@ module LogjamAgent
4
4
 
5
5
  include Util
6
6
 
7
+ SEQUENCE_START = 0
8
+
7
9
  def initialize(*args)
8
10
  opts = args.extract_options!
9
11
  @app = args[0] || LogjamAgent.application_name
@@ -11,17 +13,30 @@ module LogjamAgent
11
13
  @app_env = "#{@app}-#{@env}"
12
14
  @config = default_options.merge!(opts)
13
15
  @config[:host] = "localhost" if @config[:host].blank?
14
- @connection_specs = @config[:host].split(',').map do |host|
16
+ @sequence = SEQUENCE_START
17
+ end
18
+
19
+ def push_connection_specs
20
+ @push_connection_specs ||= @config[:host].split(',').map do |host|
15
21
  augment_connection_spec(host, @config[:port])
16
22
  end
17
- @sequence = 0
23
+ end
24
+
25
+ def req_connection_specs
26
+ @req_connection_specs ||= @config[:host].split(',').sort_by{rand}.map do |host|
27
+ augment_connection_spec(host, @config[:req_port])
28
+ end
18
29
  end
19
30
 
20
31
  def default_options
21
32
  {
33
+ :req_port => 9604,
22
34
  :port => 9605,
23
35
  :linger => 1000,
24
36
  :snd_hwm => 100,
37
+ :rcv_hwm => 100,
38
+ :rcv_timeo => 5000,
39
+ :snd_timeo => 5000
25
40
  }
26
41
  end
27
42
 
@@ -40,22 +55,53 @@ module LogjamAgent
40
55
  end
41
56
  end
42
57
 
43
- def socket
44
- return @socket if @socket
45
- @socket = self.class.context.socket(ZMQ::PUSH)
46
- at_exit { reset }
47
- @socket.setsockopt(ZMQ::LINGER, @config[:linger])
48
- @socket.setsockopt(ZMQ::SNDHWM, @config[:snd_hwm])
49
- @connection_specs.each do |spec|
50
- @socket.connect(spec)
58
+ def push_socket
59
+ return @push_socket if @push_socket
60
+ @push_socket = self.class.context.socket(ZMQ::PUSH)
61
+ at_exit { reset_push_socket }
62
+ @push_socket.setsockopt(ZMQ::LINGER, @config[:linger])
63
+ @push_socket.setsockopt(ZMQ::SNDHWM, @config[:snd_hwm])
64
+ push_connection_specs.each do |spec|
65
+ @push_socket.connect(spec)
66
+ end
67
+ @push_socket
68
+ end
69
+ alias socket push_socket
70
+
71
+ def req_socket
72
+ return @req_socket if @req_socket
73
+ @req_socket = self.class.context.socket(ZMQ::REQ)
74
+ at_exit { reset_req_socket }
75
+ @req_socket.setsockopt(ZMQ::LINGER, @config[:linger])
76
+ @req_socket.setsockopt(ZMQ::SNDHWM, @config[:snd_hwm])
77
+ @req_socket.setsockopt(ZMQ::RCVHWM, @config[:rcv_hwm])
78
+ @req_socket.setsockopt(ZMQ::RCVTIMEO, @config[:rcv_timeo])
79
+ @req_socket.setsockopt(ZMQ::SNDTIMEO, @config[:snd_timeo])
80
+ # @req_socket.setsockopt(ZMQ::REQ_CORRELATE, 1)
81
+ # @req_socket.setsockopt(ZMQ::REQ_RELAXED, 1)
82
+ req_connection_specs.each do |spec|
83
+ @req_socket.connect(spec)
84
+ end
85
+ @req_socket
86
+ end
87
+
88
+ def reset_push_socket
89
+ if @push_socket
90
+ @push_socket.close
91
+ @push_socket = nil
92
+ end
93
+ end
94
+
95
+ def reset_req_socket
96
+ if @req_socket
97
+ @req_socket.close
98
+ @req_socket = nil
51
99
  end
52
- @socket
53
100
  end
54
101
 
55
102
  def reset
56
- return unless @socket
57
- @socket.close
58
- @socket = nil
103
+ reset_push_socket
104
+ reset_req_socket
59
105
  end
60
106
 
61
107
  def forward(data, options={})
@@ -65,7 +111,11 @@ module LogjamAgent
65
111
  key += ".#{engine}"
66
112
  end
67
113
  msg = LogjamAgent.encode_payload(data)
68
- publish(app_env, key, msg)
114
+ if options[:sync]
115
+ send_receive(app_env, key, msg)
116
+ else
117
+ publish(app_env, key, msg)
118
+ end
69
119
  rescue => error
70
120
  reraise_expectation_errors!
71
121
  raise ForwardingError.new(error.message)
@@ -74,13 +124,42 @@ module LogjamAgent
74
124
  def publish(app_env, key, data)
75
125
  info = pack_info(@sequence = next_fixnum(@sequence))
76
126
  parts = [app_env, key, data, info]
77
- if socket.send_strings(parts, ZMQ::DONTWAIT) < 0
78
- raise "ZMQ error: #{ZMQ::Util.error_string}"
127
+ if push_socket.send_strings(parts, ZMQ::DONTWAIT) < 0
128
+ raise "ZMQ error on publishing: #{ZMQ::Util.error_string}"
79
129
  end
80
130
  end
81
131
 
82
132
  private
83
133
 
134
+ def log_warning(message)
135
+ LogjamAgent.error_handler.call ForwardingWarning.new(message)
136
+ end
137
+
138
+ def send_receive(app_env, key, data)
139
+ # we don't need sequencing for synchronous calls
140
+ info = pack_info(SEQUENCE_START)
141
+ request_parts = [app_env, key, data, info]
142
+ answer_parts = []
143
+ # we retry a few times relying on zeromq lib to pick servers to talk to
144
+ 3.times do
145
+ if req_socket.send_strings(request_parts) < 0
146
+ log_warning "ZMQ error on sending: #{ZMQ::Util.error_string}"
147
+ reset_req_socket
148
+ next
149
+ end
150
+ if req_socket.recv_strings(answer_parts) < 0
151
+ log_warning "ZMQ error on receiving: #{ZMQ::Util.error_string}"
152
+ reset_req_socket
153
+ next
154
+ end
155
+ return if answer_parts.first == "200 OK"
156
+ answer_parts.clear
157
+ end
158
+ # if synchronous publishing fails, we just fall back to async
159
+ log_warning "could not publish sychronously, falling back to async"
160
+ publish(app_env, key, data)
161
+ end
162
+
84
163
  if defined?(Mocha)
85
164
  def reraise_expectation_errors! #:nodoc:
86
165
  raise if $!.is_a?(Mocha::ExpectationError)
data/lib/logjam_agent.rb CHANGED
@@ -50,6 +50,7 @@ end
50
50
  module LogjamAgent
51
51
 
52
52
  class ForwardingError < StandardError; end
53
+ class ForwardingWarning < StandardError; end
53
54
 
54
55
  mattr_accessor :logger
55
56
  self.logger = nil
@@ -241,10 +242,11 @@ module LogjamAgent
241
242
  fields = {
242
243
  :label => label,
243
244
  :started_at => Time.now.iso8601,
244
- :host => hostname
245
+ :host => hostname,
246
+ :uuid => generate_uuid
245
247
  }
246
248
  fields.merge!(extra_fields)
247
- forwarder.forward(fields, :routing_key => events_routing_key)
249
+ forwarder.forward(fields, :routing_key => events_routing_key, :sync => true)
248
250
  end
249
251
 
250
252
  def self.add_forwarder(type, *args)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logjam_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.1
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Kaes