Class: TelegrafAgent

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

Constant Summary collapse

ConnectionError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, logger: nil) ⇒ TelegrafAgent

Initialize an agent

Parameters:

  • opts (Hash)


16
17
18
19
# File 'lib/telegraf_agent.rb', line 16

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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/telegraf_agent.rb', line 9

def logger
  @logger
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/telegraf_agent.rb', line 9

def uri
  @uri
end

Instance Method Details

#write(series, tags:, values:) ⇒ Object

Send data to telegraf

Parameters:

  • series (String)

    Series name

  • opts (Hash)


27
28
29
30
31
32
33
# File 'lib/telegraf_agent.rb', line 27

def write(series, tags:, values:)
  data = ::InfluxDB::PointValue.new(series: series, tags: tags, values: values).dump
  logger&.debug("Sending data to #{uri}: #{data}")
  connection.write(data)
rescue
  disconnect!
end