Class: Opower::TimeSeries::Metric

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

Overview

Represents a metric that can be inserted into OpenTSDB instance through a [TSDBClient] object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Metric

Initializer for the Metric class.

Parameters:

  • config (Hash) (defaults to: {})

    configuration hash consisting of the following values:

Options Hash (config):

  • :name (String)

    The metric name (required)

  • :value (String)

    The metric value (required)

  • :timestamp (String, Integer, Timestamp)

    The timestamp in either epoch or a TimeStamp object.

  • :tags (Array)

    Array of tags to set for this metric. (tag_key => value)



18
19
20
21
22
23
24
25
# File 'lib/time_series/metric.rb', line 18

def initialize(config = {})
  validate(config, [:name, :value])

  @name = config[:name]
  @value = config[:value]
  @timestamp = config[:timestamp] ||  Time.now.to_i
  @tags = config[:tags] || {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/time_series/metric.rb', line 7

def name
  @name
end

#tagsObject (readonly)

Returns the value of attribute tags.



7
8
9
# File 'lib/time_series/metric.rb', line 7

def tags
  @tags
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



7
8
9
# File 'lib/time_series/metric.rb', line 7

def timestamp
  @timestamp
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/time_series/metric.rb', line 7

def value
  @value
end

Instance Method Details

#to_sString

Converts the metric into the format required for use by ‘put` to insert into OpenTSDB.

Returns:

  • (String)

    put string



30
31
32
33
34
35
# File 'lib/time_series/metric.rb', line 30

def to_s
  result = ''
  # Format the string for OpenTSDB
  @tags.each { |key, value| result += "#{key}=#{value} " }
  [@name, @timestamp, @value, result.rstrip].join(' ')
end