<commit_before>require 'prometheus/client'
require 'prometheus/client/formats/text'

module Fluent
  module Prometheus
    class AlreadyRegisteredError < StandardError; end

    def self.parse_labels_elements(conf)
      labels = conf.elements.select { |e| e.name == 'labels' }
      if labels.size > 1
        raise ConfigError, "labels section must have at most 1"
      end

      base_labels = {}
      unless labels.empty?
        labels.first.each do |key, value|
          labels.first.has_key?(key)
          base_labels[key.to_sym] = value
        end
      end

      base_labels
    end

    def self.parse_metrics_elements(conf, registry, labels = {})
      metrics = []
      conf.elements.select { |element|
        element.name == 'metric'
      }.each { |element|
        case element['type']
        when 'summary'
          metrics << Fluent::Prometheus::Summary.new(element, registry, labels)
        when 'gauge'
          metrics << Fluent::Prometheus::Gauge.new(element, registry, labels)
        when 'counter'
          metrics << Fluent::Prometheus::Counter.new(element, registry, labels)
        else
          raise ConfigError, "type option must be 'counter', 'gauge' or 'summary'"
        end
      }
      metrics
    end

    def self.placeholder_expander(log)
      # Use internal class in order to expand placeholder
      if defined?(Fluent::Filter) # for v0.12, built-in PlaceholderExpander
        begin
          require 'fluent/plugin/filter_record_transformer'
          return Fluent::RecordTransformerFilter::PlaceholderExpander.new(log: log)
        rescue LoadError => e
          raise ConfigError, "cannot find filter_record_transformer plugin: #{e.message}"
        end
      else # for v0.10, use PlaceholderExapander in fluent-plugin-record-reformer plugin
        begin
          require 'fluent/plugin/out_record_reformer.rb'
          return Fluent::RecordReformerOutput::PlaceholderExpander.new(log: log)
        rescue LoadError => e<commit_msg>explicitly specify v0.14 RecordTransformerFilter class<commit_after>require 'prometheus/client'
require 'prometheus/client/formats/text'

module Fluent
  module Prometheus
    class AlreadyRegisteredError < StandardError; end

    def self.parse_labels_elements(conf)
      labels = conf.elements.select { |e| e.name == 'labels' }
      if labels.size > 1
        raise ConfigError, "labels section must have at most 1"
      end

      base_labels = {}
      unless labels.empty?
        labels.first.each do |key, value|
          labels.first.has_key?(key)
          base_labels[key.to_sym] = value
        end
      end

      base_labels
    end

    def self.parse_metrics_elements(conf, registry, labels = {})
      metrics = []
      conf.elements.select { |element|
        element.name == 'metric'
      }.each { |element|
        case element['type']
        when 'summary'
          metrics << Fluent::Prometheus::Summary.new(element, registry, labels)
        when 'gauge'
          metrics << Fluent::Prometheus::Gauge.new(element, registry, labels)
        when 'counter'
          metrics << Fluent::Prometheus::Counter.new(element, registry, labels)
        else
          raise ConfigError, "type option must be 'counter', 'gauge' or 'summary'"
        end
      }
      metrics
    end

    def self.placeholder_expander(log)
      # Use internal class in order to expand placeholder
      if defined?(Fluent::Filter) # for v0.12, built-in PlaceholderExpander
        begin
          require 'fluent/plugin/filter_record_transformer'
          if defined?(Fluent::Plugin::RecordTransformerFilter::PlaceholderExpander)
            # for v0.14
            return Fluent::Plugin::RecordTransformerFilter::PlaceholderExpander.new(log: log)
          else
            # for v0.12
            return Fluent::RecordTransformerFilter::PlaceholderExpander.new(log: log)
          end
        rescue LoadError => e
          raise ConfigError, "cannot find filter_record_transformer plugin: #{e.message}"
        end
      else # for v0.10, use PlaceholderExapander in fluent-plugin-record-reformer plugin
        begin
          require 'fluent/plugin/out_record_reformer.rb'
          return Fluent::RecordReformerOutput::PlaceholderExpander.new(log: log)
        rescue LoadError => e
{'fraction_non_alphanumeric': 0.09315323707498836, 'fraction_numerical': 0.005822077317186773, 'mean_word_length': 2.5791666666666666, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7534300', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1183, 'n_words': 298}