Sha256: 9af7fe2cd939578571c973c0ad109f51a8ca05abe2c7a1165dab227786909ee8

Contents?: true

Size: 608 Bytes

Versions: 5

Compression:

Stored size: 608 Bytes

Contents

# frozen_string_literal: true

class Async::App::Metrics::Serializer
  def initialize(prefix:)
    @prefix = prefix
  end

  def serialize(metrics)
    metrics.flat_map { metric_line(_1) }
           .compact
           .join("\n")
           .then { "#{_1}\n" }
  end

  def metric_name(value) = "#{@prefix}_#{value[:name]}_#{value[:suffix]}"

  def metric_labels(value) = value[:labels].map { |tag, tag_value| "#{tag}=#{tag_value.to_s.inspect}" }.join(",")

  def metric_line(value)
    labels = metric_labels(value)

    "#{metric_name(value)}{#{labels}} #{value[:value]}" if value.key?(:value)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
async-tools-0.2.7 lib/async/app/metrics/serializer.rb
async-tools-0.2.6 lib/async/app/metrics/serializer.rb
async-tools-0.2.5 lib/async/app/metrics/serializer.rb
async-tools-0.2.4 lib/async/app/metrics/serializer.rb
async-tools-0.2.2 lib/async/app/metrics/serializer.rb