Class: Telegraf::Agent

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

Constant Summary collapse

DEFAULT_CONNECTION =
'udp://localhost:8094'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = nil, logger: nil, tags: {}) ⇒ Agent

Returns a new instance of Agent.



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

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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
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, **kwargs) ⇒ Object



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

def write(*args, **kwargs)
  write!(*args, **kwargs)
rescue StandardError => 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
33
34
# File 'lib/telegraf/agent.rb', line 23

def write!(data, series: nil, tags: nil, values: nil)
  tags = tags.merge(@tags) unless @tags.empty?

  if values
    data = [{series: series || data.to_s, tags: tags, values: values}]
  end

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