Class: SemanticLogger::Metrics::Udp

Inherits:
Subscriber show all
Defined in:
lib/semantic_logger/metrics/udp.rb

Instance Attribute Summary collapse

Attributes inherited from Subscriber

#application, #formatter, #host

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#level

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:

Example:

subscriber = SemanticLogger::Metrics::Udp.new(server: 'localhost:8125')
SemanticLogger.on_metric(subscriber)

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
# File 'lib/semantic_logger/metrics/udp.rb', line 27

def initialize(options = {}, &block)
  options    = options.dup
  @server    = options.delete(:server)
  @udp_flags = options.delete(:udp_flags) || 0
  raise(ArgumentError, 'Missing mandatory argument: :server') unless @server

  super(options, &block)
  reopen
end

Instance Attribute Details

#separatorObject

Returns the value of attribute separator.



5
6
7
# File 'lib/semantic_logger/metrics/udp.rb', line 5

def separator
  @separator
end

#serverObject

Returns the value of attribute server.



5
6
7
# File 'lib/semantic_logger/metrics/udp.rb', line 5

def server
  @server
end

#socketObject (readonly)

Returns the value of attribute socket.



6
7
8
# File 'lib/semantic_logger/metrics/udp.rb', line 6

def socket
  @socket
end

#udp_flagsObject

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

#closeObject

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

#flushObject

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

#reopenObject

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