class Metrico::Point

Constants

ESCAPES

Attributes

fields[RW]
name[RW]
tags[RW]

Public Class Methods

new(name, fields, tags = {}) click to toggle source
# File lib/metrico/point.rb, line 5
def initialize(name, fields, tags = {})
  @name = measurement(name)
  @fields = escape_fields(fields)
  tags = { hostname: Metrico.config.hostname }.merge(tags)
  @tags = escape_tags(tags)
end

Public Instance Methods

to_s() click to toggle source

docs.influxdata.com/influxdb/v1.3/write_protocols/line_protocol_tutorial/ InfluxDB Line Protocol weather,location=us-midwest temperature=82 1465839830100400200

|    -------------------- --------------  |
|             |             |             |
|             |             |             |

-----------——–-———-———+ |measurement|,tag_set| |field_set| |timestamp| -----------——–-———-———+

# File lib/metrico/point.rb, line 21
def to_s
  str = "#{name}"
  str << ",#{tags}" if tags
  str << " #{fields}"
  str << " #{timestamp}"
  str
end

Private Instance Methods

escape(string, type) click to toggle source
# File lib/metrico/point.rb, line 69
        def escape(string, type)
  # rubocop:disable Layout/AlignParameters
  string = string.encode(
    'UTF-8'.freeze,
    'UTF-8'.freeze,
    invalid: :replace,
    undef: :replace,
    replace: ''.freeze
  )
  
  ESCAPES[type].each do |char|
    string = string.gsub(char) { "\\#{char}" }
  end
  string
end
escape_fields(fields) click to toggle source
# File lib/metrico/point.rb, line 50
        def escape_fields(fields)
  return if fields.nil?
  fields.map do |k, v|
    key = escape(k.to_s, :field_key)
    val = escape_value(v)
    "#{key}=#{val}"
  end.join(",".freeze)
end
escape_tags(tags) click to toggle source
# File lib/metrico/point.rb, line 37
        def escape_tags(tags)
  return if tags.nil?

  tags = tags.map do |k, v|
    key = escape(k.to_s, :tag_key)
    val = escape(v.to_s, :tag_value)

    "#{key}=#{val}" unless key == "".freeze || val == "".freeze
  end.compact

  tags.join(",") unless tags.empty?
end
escape_value(value) click to toggle source
# File lib/metrico/point.rb, line 59
        def escape_value(value)
  if value.is_a?(String)
    '"'.freeze + escape(value, :field_value) + '"'.freeze
  elsif value.is_a?(Integer)
    "#{value}i"
  else
    value.to_s
  end
end
measurement(name) click to toggle source
# File lib/metrico/point.rb, line 29
        def measurement(name)
  escape("#{Metrico.config.app_name}:#{name}", :measurement)
end
timestamp() click to toggle source
# File lib/metrico/point.rb, line 33
        def timestamp
  (Time.now.to_f * 1_000_000_000).to_i
end