Class: LoggregatorEmitter::Emitter

Inherits:
Object
  • Object
show all
Defined in:
lib/loggregator_emitter/emit.rb

Constant Summary collapse

UDP_SEND_ERROR =
StandardError.new("Error sending message via UDP")
MAX_MESSAGE_BYTE_SIZE =
(9 * 1024) - 512
TRUNCATED_STRING =
"TRUNCATED"
MAX_TAG_LENGTH =
256
MAX_TAGS =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loggregator_server, origin, source_type, source_instance = nil) ⇒ Emitter

Returns a new instance of Emitter.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/loggregator_emitter/emit.rb', line 17

def initialize(loggregator_server, origin, source_type, source_instance = nil)
  @host, @port = loggregator_server.split(/:([^:]*$)/)

  raise ArgumentError, "Must provide valid loggregator server: #{loggregator_server}" if !valid_hostname || !valid_port
  @host = ::Resolv.getaddresses(@host).first
  raise ArgumentError, "Must provide valid loggregator server: #{loggregator_server}" unless @host

  raise ArgumentError, "Must provide a valid origin" unless origin
  raise ArgumentError, "Must provide valid source_type: #{source_type}" unless source_type

  raise ArgumentError, "source_type must be a 3-character string" unless source_type.is_a? String
  raise ArgumentError, "Custom Source String must be 3 characters" unless source_type.size == 3
  @origin = origin
  @source_type = source_type

  @source_instance = source_instance && source_instance.to_s
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/loggregator_emitter/emit.rb', line 10

def host
  @host
end

Instance Method Details

#emit(app_id, message, tags = nil) ⇒ Object



35
36
37
# File 'lib/loggregator_emitter/emit.rb', line 35

def emit(app_id, message, tags = nil)
  emit_message(app_id, message, LogMessage::MessageType::OUT, tags)
end

#emit_container_metric(app_id, instanceIndex, cpu, memory, disk, tags = nil) ⇒ Object



55
56
57
58
59
# File 'lib/loggregator_emitter/emit.rb', line 55

def emit_container_metric(app_id, instanceIndex, cpu, memory, disk, tags = nil)
  return unless app_id && instanceIndex && cpu && memory && disk

  send_protobuffer(create_container_metric_envelope(app_id, instanceIndex, cpu, memory, disk, tags))
end

#emit_counter(name, delta, tags = nil) ⇒ Object



49
50
51
52
53
# File 'lib/loggregator_emitter/emit.rb', line 49

def emit_counter(name, delta, tags = nil)
  return unless name && delta

  send_protobuffer(create_counter_envelope(name, delta, tags))
end

#emit_error(app_id, message, tags = nil) ⇒ Object



39
40
41
# File 'lib/loggregator_emitter/emit.rb', line 39

def emit_error(app_id, message, tags = nil)
  emit_message(app_id, message, LogMessage::MessageType::ERR, tags)
end

#emit_value_metric(name, value, unit, tags = nil) ⇒ Object



43
44
45
46
47
# File 'lib/loggregator_emitter/emit.rb', line 43

def emit_value_metric(name, value, unit, tags = nil)
  return unless name && value && unit

  send_protobuffer(create_value_metric_envelope(name, value, unit, tags))
end