Class: Telegraf::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/telegraf/agent.rb

Constant Summary collapse

DEFAULT_URI =
'udp://localhost:8094'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = DEFAULT_CONNECTION, logger: nil) ⇒ Agent

Returns a new instance of Agent.



10
11
12
13
# File 'lib/telegraf/agent.rb', line 10

def initialize(uri = DEFAULT_CONNECTION, logger: nil)
  @uri = URI.parse(uri)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/telegraf/agent.rb', line 8

def logger
  @logger
end

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/telegraf/agent.rb', line 7

def uri
  @uri
end

Instance Method Details

#write(*args) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/telegraf/agent.rb', line 15

def write(*args)
  write!(*args)
rescue => e
  logger&.error('telegraf') do
    e.to_s + e.backtrace.join("\n")
  end
end

#write!(data, series: nil, tags: nil, values: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/telegraf/agent.rb', line 23

def write!(data, series: nil, tags: nil, values: nil)
  if values
    data = [{series: series || data.to_s, tags: tags, values: values}]
  end

  socket = connect @uri
  socket.write dump data
ensure
  socket&.close
end