Module: ActiveMeasure::Serializer::ClassMethods

Defined in:
lib/active_measure/serializer.rb

Instance Method Summary collapse

Instance Method Details

#deserialize(serialized_metric) ⇒ Hash

Deserialize a metric from a serialized string

Examples:

‘company.create;company=acme,environment=production:1|ms’


{ metric: ‘company.create’, value: 1.0, type: ‘ms’, tags: { ‘company’ => ‘acme’, ‘environment’ => ‘production’ } }


Parameters:

  • serialized_metric (String)

    The metric string to deserialize

Returns:

  • (Hash)

    Deserialized metric as a hash with keys: :metric, :value, :type, and :tags



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_measure/serializer.rb', line 30

def deserialize(serialized_metric)
  metric, tag_string, value_and_type = serialized_metric.split(';')
  value, type = value_and_type.split('|')

  tags = {}
  tag_string.split(',').each do |tag|
    key, value = tag.split('=')
    tags[key] = value
  end

  { metric: metric, value: value.to_f, type: type, tags: tags }
rescue => e
  raise "Failed to deserialize metric: #{e.message}"
end

#serialize(metric, value, type: 'c', tags: {}) ⇒ String

Serialize a metric into a string

Examples:

‘company.create’


1


‘ms’


{ company: ‘acme’, environment: ‘production’ }


‘company.create;company=acme,environment=production:1|ms’


Parameters:

  • metric (String)

    The metric to serialize

  • value (Numeric)

    The value to serialize

  • type (String) (defaults to: 'c')

    The type of metric (defaults to ‘c’)

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

    The tags to serialize

Returns:

  • (String)

    Serialized metric string



20
21
22
23
# File 'lib/active_measure/serializer.rb', line 20

def serialize(metric, value, type: 'c', tags: {})
  tag_string = tags.map { |key, val| "#{key}=#{val}" }.join(',')
  "#{metric};#{tag_string}:#{value}|#{type}"
end