Class: Fluent::MetricsBackend

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

Direct Known Subclasses

MetricsBackendGraphite, MetricsBackendStatsd

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, headers = []) ⇒ MetricsBackend

Returns a new instance of MetricsBackend.



92
93
94
95
96
97
# File 'lib/fluent/metrics_backends.rb', line 92

def initialize(url = nil,headers = [])
  @output_buffer = []
  @connection_parameters = {}
  set_connection_parameters(url, headers)
  @connection = SocketLike.new(@connection_parameters)
end

Instance Method Details

#buffer?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/fluent/metrics_backends.rb', line 120

def buffer?
  not @output_buffer.empty?
end

#buffer_append(data) ⇒ Object



157
158
159
# File 'lib/fluent/metrics_backends.rb', line 157

def buffer_append(data)
  @output_buffer << serialize(data)
end

#buffer_append_array_of_hashes(data) ⇒ Object



153
154
155
# File 'lib/fluent/metrics_backends.rb', line 153

def buffer_append_array_of_hashes(data)
  @output_buffer += data.map {|e| serialize_entry(e,e['time']) }
end

#buffer_append_array_of_values(data) ⇒ Object



149
150
151
# File 'lib/fluent/metrics_backends.rb', line 149

def buffer_append_array_of_values(data)
  @output_buffer += data.map {|e| serialize_entry(e[0],e[1]) }
end

#buffer_append_entry(entry, time) ⇒ Object



161
162
163
# File 'lib/fluent/metrics_backends.rb', line 161

def buffer_append_entry(entry,time)
  @output_buffer << serialize_entry(entry,time)
end

#buffer_dumpObject



99
100
101
102
103
104
# File 'lib/fluent/metrics_backends.rb', line 99

def buffer_dump
  # Allw this to be overridable to facitatte formats such as
  # multiliine formats, or things JSON where there are
  # punctuation differences depending upon position.
  @output_buffer.join("\n") + "\n"
end

#buffer_flushObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fluent/metrics_backends.rb', line 106

def buffer_flush

  begin
    @connection.open
    @connection.write(buffer_dump)
  rescue
    raise
  ensure
    @connection.close
  end

  @output_buffer = []
end

#get_connection_parametersObject



88
89
90
# File 'lib/fluent/metrics_backends.rb', line 88

def get_connection_parameters
  @connection_parameters
end

#get_connection_parameters_defaultsObject



72
73
74
75
# File 'lib/fluent/metrics_backends.rb', line 72

def get_connection_parameters_defaults
  # This should be overriden in any any subclass.
  return {}
end

#serialize(data) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/fluent/metrics_backends.rb', line 139

def serialize(data)
  if data.is_a?(Hash)
    return serialize_hash(data)
  elsif data.is_a(Array)
    return serialize_array(data)
  else
    raise ArgumentError, 'serialize method input must be of Hash or Array type'
  end
end

#serialize_array(data) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/fluent/metrics_backends.rb', line 128

def serialize_array(data)
  return if data.empty?
  if data[0].is_a?(Hash)
    return serialize_array_of_hashes(data)
  elsif data[0].is_a?(Array)
    return serialize_array_of_arrays(data)
  else
    raise ArgumentError, 'serialize_array method input must be of Hash or Array type'
  end
end

#serialize_entry(entry, time) ⇒ Object

Raises:

  • (NoMethodError)


124
125
126
# File 'lib/fluent/metrics_backends.rb', line 124

def serialize_entry(entry,time)
  raise NoMethodError, 'The serialize_entry method has not been specified.'
end

#set_connection_parameters(url, headers = []) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/fluent/metrics_backends.rb', line 77

def set_connection_parameters(url,headers = [])
  @connection_parameters.merge!(get_connection_parameters_defaults)
  @connection_parameters.merge!(
    Hash[['proto','host','port','path'].zip(
      url.match(/^([^:]*):\/\/([^:\/]*):?(\d*)(\/.*)?/).to_a.map {|e| e.nil? or e.empty? ? nil : e }[1..-1]
    )]
  ) unless url.nil? or url.empty?
  @connection_parameters['headers'] ||= []
  @connection_parameters['headers'] += headers
end