Class: Metrics::Formatters::L2Met::Lines

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/metrics/formatters/l2met.rb

Overview

Responsible for taking a prefix, and an array of measurements, and returning an array of log lines that are limited to 1024 characters per line.

Constant Summary collapse

MAX_LEN =
1024.freeze
DELIMITER =
' '.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, measurements) ⇒ Lines

Returns a new instance of Lines.



49
50
51
52
53
# File 'lib/metrics/formatters/l2met.rb', line 49

def initialize(source, measurements)
  @source = "source=#{source}"
  @measurements = measurements
  @max = MAX_LEN - @source.length + DELIMITER.length * 2
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



47
48
49
# File 'lib/metrics/formatters/l2met.rb', line 47

def max
  @max
end

#measurementsObject (readonly)

Returns the value of attribute measurements.



46
47
48
# File 'lib/metrics/formatters/l2met.rb', line 46

def measurements
  @measurements
end

#sourceObject (readonly)

Returns the value of attribute source.



45
46
47
# File 'lib/metrics/formatters/l2met.rb', line 45

def source
  @source
end

Instance Method Details

#each(&block) ⇒ Object



55
56
57
# File 'lib/metrics/formatters/l2met.rb', line 55

def each(&block)
  measurements.each(&block)
end

#linesObject

Groups the array of measurements into an array of log lines, each line prefixed with the source.

Example

# This:
['measure#rack.request=1', ..., 'measure#rack.request.time=200ms']

# Into this:
['source=app measure#rack.request=1', 'source=app measure#rack.request.time=200ms', ...]

Returns an Array.



71
72
73
74
75
76
77
78
79
# File 'lib/metrics/formatters/l2met.rb', line 71

def lines
  total = 0
  chunk { |measurement|
    total += measurement.length + DELIMITER.length * 2
    total / max
  }.map { |_, line|
    [source, line].join(DELIMITER)
  }
end