class Fluent::Plugin::Prometheus::ExpandBuilder

Public Class Methods

build(placeholder, log:) click to toggle source
# File lib/fluent/plugin/prometheus/placeholder_expander.rb, line 5
def self.build(placeholder, log:)
  new(log: log).build(placeholder)
end
new(log:) click to toggle source
# File lib/fluent/plugin/prometheus/placeholder_expander.rb, line 9
def initialize(log:)
  @log = log
end

Public Instance Methods

build(placeholder_values) click to toggle source
# File lib/fluent/plugin/prometheus/placeholder_expander.rb, line 13
def build(placeholder_values)
  placeholders = {}
  placeholder_values.each do |key, value|
    case value
    when Array
      size = value.size
      value.each_with_index do |v, i|
        placeholders["${#{key}[#{i}]}"] = v
        placeholders["${#{key}[#{i - size}]}"] = v
      end
    when Hash
      value.each do |k, v|
        placeholders[%(${#{key}["#{k}"]})] = v
      end
    else
      if key == 'tag'
        placeholders.merge!(build_tag(value))
      else
        placeholders["${#{key}}"] = value
      end
    end
  end

  Fluent::Plugin::Prometheus::ExpandBuilder::PlaceholderExpander.new(@log, placeholders)
end

Private Instance Methods

build_tag(tag) click to toggle source
# File lib/fluent/plugin/prometheus/placeholder_expander.rb, line 41
def build_tag(tag)
  tags = tag.split('.')

  placeholders = { '${tag}' => tag }

  size = tags.size

  tags.each_with_index do |v, i|
    placeholders["${tag_parts[#{i}]}"] = v
    placeholders["${tag_parts[#{i - size}]}"] = v
  end

  tag_prefix(tags).each_with_index do |v, i|
    placeholders["${tag_prefix[#{i}]}"] = v
  end

  tag_suffix(tags).each_with_index do |v, i|
    placeholders["${tag_suffix[#{i}]}"] = v
  end

  placeholders
end
tag_prefix(tags) click to toggle source
# File lib/fluent/plugin/prometheus/placeholder_expander.rb, line 64
def tag_prefix(tags)
  tags = tags.dup
  return [] if tags.empty?

  ret = [tags.shift]
  tags.each.with_index(1) do |tag, i|
    ret[i] = "#{ret[i-1]}.#{tag}"
  end
  ret
end
tag_suffix(tags) click to toggle source
# File lib/fluent/plugin/prometheus/placeholder_expander.rb, line 75
def tag_suffix(tags)
  return [] if tags.empty?

  tags = tags.dup.reverse
  ret = [tags.shift]
  tags.each.with_index(1) do |tag, i|
    ret[i] = "#{tag}.#{ret[i-1]}"
  end
  ret
end