Class: SemanticLogger::Metrics::Udp
- Inherits:
-
Subscriber
- Object
- Base
- Subscriber
- SemanticLogger::Metrics::Udp
- Defined in:
- lib/semantic_logger/metrics/udp.rb
Instance Attribute Summary collapse
-
#separator ⇒ Object
Returns the value of attribute separator.
-
#server ⇒ Object
Returns the value of attribute server.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#udp_flags ⇒ Object
Returns the value of attribute udp_flags.
Attributes inherited from Subscriber
#application, #formatter, #host
Attributes inherited from Base
Instance Method Summary collapse
- #call(log) ⇒ Object
-
#close ⇒ Object
Close is called during shutdown, or with reopen.
-
#flush ⇒ Object
Flush is called by the semantic_logger during shutdown.
-
#initialize(options = {}, &block) ⇒ Udp
constructor
Write metrics to in JSON format to Udp.
-
#reopen ⇒ Object
After forking an active process call #reopen to re-open open the handles to resources.
Methods inherited from Subscriber
Methods inherited from Base
#fast_tag, #level, #level=, #measure, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload
Constructor Details
#initialize(options = {}, &block) ⇒ Udp
Write metrics to in JSON format to Udp
Parameters:
server: [String]
Host name and port to write UDP messages to
Example:
localhost:8125
udp_flags: [Integer]
Should be a bitwise OR of Socket::MSG_* constants.
Default: 0
Limitations:
-
UDP packet size is limited by the connected network and any routers etc that the message has to traverse. See en.wikipedia.org/wiki/Maximum_transmission_unit
Example:
subscriber = SemanticLogger::Metrics::Udp.new(server: 'localhost:8125')
SemanticLogger.on_metric(subscriber)
27 28 29 30 31 32 33 34 35 |
# File 'lib/semantic_logger/metrics/udp.rb', line 27 def initialize( = {}, &block) = .dup @server = .delete(:server) @udp_flags = .delete(:udp_flags) || 0 raise(ArgumentError, 'Missing mandatory argument: :server') unless @server super(, &block) reopen end |
Instance Attribute Details
#separator ⇒ Object
Returns the value of attribute separator.
5 6 7 |
# File 'lib/semantic_logger/metrics/udp.rb', line 5 def separator @separator end |
#server ⇒ Object
Returns the value of attribute server.
5 6 7 |
# File 'lib/semantic_logger/metrics/udp.rb', line 5 def server @server end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
6 7 8 |
# File 'lib/semantic_logger/metrics/udp.rb', line 6 def socket @socket end |
#udp_flags ⇒ Object
Returns the value of attribute udp_flags.
5 6 7 |
# File 'lib/semantic_logger/metrics/udp.rb', line 5 def udp_flags @udp_flags end |
Instance Method Details
#call(log) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/semantic_logger/metrics/udp.rb', line 46 def call(log) metric = log.metric if duration = log.duration @statsd.timing(metric, duration) else amount = (log.metric_amount || 1).round if amount < 0 amount.times { @statsd.decrement(metric) } else amount.times { @statsd.increment(metric) } end end @socket.send(data, udp_flags) end |
#close ⇒ Object
Close is called during shutdown, or with reopen
67 68 69 |
# File 'lib/semantic_logger/metrics/udp.rb', line 67 def close @socket.close if @socket end |
#flush ⇒ Object
Flush is called by the semantic_logger during shutdown.
62 63 64 |
# File 'lib/semantic_logger/metrics/udp.rb', line 62 def flush @socket.flush if @socket end |
#reopen ⇒ Object
After forking an active process call #reopen to re-open open the handles to resources
39 40 41 42 43 44 |
# File 'lib/semantic_logger/metrics/udp.rb', line 39 def reopen close @socket = UDPSocket.new host, port = server.split(':') @socket.connect(host, port) end |