class TsdMetrics::JsonFormattingSink

Public Class Methods

new(outputStream) click to toggle source
# File lib/tsd_metrics/json_formatting_sink.rb, line 20
def initialize(outputStream)
  @outputStream = outputStream
end

Public Instance Methods

receive(tsdMetricEvent) click to toggle source
# File lib/tsd_metrics/json_formatting_sink.rb, line 23
def receive(tsdMetricEvent)
  hash = {
    time: Time.now.utc.iso8601(3),
    name: "aint.metrics",
    level: "info",
    data: {
      version: "2e",
      gauges: proxyValuesProperty(tsdMetricEvent.gauges),
      timers: proxyValuesProperty(tsdMetricEvent.timers),
      counters: proxyValuesProperty(tsdMetricEvent.counters),
      annotations: tsdMetricEvent.annotations
    }
  }
  haveMetrics = [:gauges, :timers, :counters].any? do |metricType|
    hash[:data][metricType].length > 0
  end
  # The timestamp annotations are always present, but we're looking for
  # any further annotations.
  haveMetrics = true if hash[:data][:annotations].length > 2

  return unless haveMetrics

  hash[:data][:annotations][:initTimestamp] = hash[:data][:annotations][:initTimestamp].utc.iso8601(3)
  hash[:data][:annotations][:finalTimestamp] = hash[:data][:annotations][:finalTimestamp].utc.iso8601(3)
  @outputStream.write(hash.to_json)
end
record(tsdMetricEvent) click to toggle source
# File lib/tsd_metrics/json_formatting_sink.rb, line 50
def record(tsdMetricEvent)
  receive(tsdMetricEvent)
end

Private Instance Methods

proxyValuesProperty(hash) click to toggle source
# File lib/tsd_metrics/json_formatting_sink.rb, line 56
def proxyValuesProperty(hash)
  hash.merge(hash) do |key, val, _|
    {values: val}
  end
end