class Sumologic::Metrics::MetricBatch
A batch of `metric`s to be sent to the API
Public Class Methods
new(max_metric_count)
click to toggle source
# File lib/sumologic/metrics/metric_batch.rb, line 11 def initialize(max_metric_count) @metrics = [] @max_metric_count = max_metric_count @size = 0 end
Public Instance Methods
<<(metric)
click to toggle source
# File lib/sumologic/metrics/metric_batch.rb, line 17 def <<(metric) if metric.too_big? logger.error('a metric exceeded the maximum allowed size') else @metrics << metric @size += metric.size + 2 # One byte for new line end end
clear()
click to toggle source
# File lib/sumologic/metrics/metric_batch.rb, line 30 def clear @metrics.clear @json = 0 end
full?()
click to toggle source
# File lib/sumologic/metrics/metric_batch.rb, line 26 def full? item_count_exhausted? || size_exhausted? end
to_s()
click to toggle source
# File lib/sumologic/metrics/metric_batch.rb, line 35 def to_s @metrics.join("\n") end
Private Instance Methods
item_count_exhausted?()
click to toggle source
# File lib/sumologic/metrics/metric_batch.rb, line 44 def item_count_exhausted? @metrics.length >= @max_metric_count end
size_exhausted?()
click to toggle source
We consider the max size here as just enough to leave room for one more metric of the largest size possible. This is a shortcut that allows us to use a native Ruby `Queue` that doesn't allow peeking. The tradeoff here is that we might fit in less metrics than possible into a batch.
The alternative is to use our own `Queue` implementation that allows peeking, and to consider the next metric size when calculating whether the metric can be accomodated in this batch.
# File lib/sumologic/metrics/metric_batch.rb, line 56 def size_exhausted? @size >= (MAX_BYTES - Defaults::Metric::MAX_BYTES) end