Sha256: 754f27112f2ddb46d3823d5087d9fa0aac58f84b5229e50c877fb26c3fd7c7b7

Contents?: true

Size: 1.97 KB

Versions: 7

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true
require 'json'
require_relative 'base_metrics_parser'

class NewRelicMetricsParser < BaseMetricsParser
  def initialize(metric_prefix, router, tag)
    @metric_prefix = metric_prefix
    @router = router
    @tag = tag
  end

  def format_data(cleaned_data = [], prefix = '', separator = '')
    hash_data_array = []
    data_hash = {}
    data_array = []
    cleaned_data.each do |interim_data|
      metrics_hash = {}
      if interim_data =~ /{/ && interim_data =~ /}/
        attributes = {}
        metric_name, additional_dims, metric_val_and_time = interim_data.match(/(.*){(.*)}(.*)/i).captures
        if additional_dims =~ /,/
          additional_dims.split(/,/).map do |interim_data|
            attributes[interim_data.split(/=/)[0]] = interim_data.split(/=/)[1].gsub(/"/, '') if interim_data =~ /=/
          end
        else
          attribute_name, attribute_value = additional_dims.split('=')
          attributes[attribute_name] = attribute_value.delete_prefix('"').delete_suffix('"')
        end
        if metric_val_and_time =~ / /
          metrics_hash['name'] = prefix + separator + metric_name
          metrics_hash['value'] =
              metric_val_and_time.strip.split[0] =~ /^\S*\.\S*$/ ? metric_val_and_time.strip.split[0].to_f : metric_val_and_time.strip.split[0].to_i
          metrics_hash['timestamp'] = metric_val_and_time.strip.split[1].to_i
          metrics_hash['attributes'] = attributes
        end
      else
        metrics_hash['name'], metrics_hash['value'], metrics_hash['timestamp'] = interim_data.split
        metrics_hash['name'] = prefix + separator + metrics_hash['name']
        metrics_hash['value'] =
            metrics_hash['value'] =~ /^\S*\.\S*$/ ? metrics_hash['value'].to_f : metrics_hash['value'].to_i
        metrics_hash['timestamp'] = metrics_hash['timestamp'].to_i
      end
      data_array << metrics_hash
    end
    data_hash["metrics"] = data_array
    hash_data_array.push(data_hash)
    hash_data_array
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fluent-plugin-jfrog-metrics-0.2.6 lib/fluent/plugin/newrelic_metrics_parser.rb
fluent-plugin-jfrog-metrics-0.2.5 lib/fluent/plugin/newrelic_metrics_parser.rb
fluent-plugin-jfrog-metrics-0.2.4 lib/fluent/plugin/newrelic_metrics_parser.rb
fluent-plugin-jfrog-metrics-0.2.3 lib/fluent/plugin/newrelic_metrics_parser.rb
fluent-plugin-jfrog-metrics-0.2.2 lib/fluent/plugin/newrelic_metrics_parser.rb
fluent-plugin-jfrog-metrics-0.2.1 lib/fluent/plugin/newrelic_metrics_parser.rb
fluent-plugin-jfrog-metrics-0.2.0 lib/fluent/plugin/newrelic_metrics_parser.rb