app/models/zuora_connect/telegraf.rb



module ZuoraConnect
  class Telegraf
    attr_accessor :host

    def initialize 
      self.connect
    end

    def connect
      Rails.logger.debug(self.format_metric_log('Telegraf','Need new connection')) if ZuoraConnect.configuration.telegraf_debug
      uri = URI.parse(ZuoraConnect.configuration.telegraf_endpoint)
      self.host = UDPSocket.new.tap do |socket|
        socket.connect uri.host, uri.port
      end
    rescue => ex
      self.host = nil
      Rails.logger.warn(self.format_metric_log('Telegraf', 'Failed to connect'))
      Rails.logger.warn(self.format_metric_log('Telegraf', ex.class))
      Rails.logger.warn(self.format_metric_log('Telegraf', ex.message))
    end

    def write(direction: 'Default', tags: {}, values: {})
      time = Benchmark.measure do |bench|
        # To avoid writing metrics from rspec tests
        if ENV['DEIS_APP'] || true
          # Getting the process type
          p_type = self.get_process_type
          app_instance = Thread.current[:appinstance].present? ? Thread.current[:appinstance].id : 0
          tags = tags.merge({ app_name: ZuoraConnect.configuration.app_name, process_type: p_type, app_instance: app_instance})

          if direction == :inbound && ZuoraConnect.configuration.enable_inbound_metrics_flag
            series = ZuoraConnect.configuration.influxdb_series_name_inbound
            self.write_udp(series: series, tags: tags, values: values)
          elsif direction == :outbound && ZuoraConnect.configuration.enable_outbound_metrics_flag
            series = ZuoraConnect.configuration.influxdb_series_name_outbound
            self.write_udp(series: series, tags: tags, values: values)
          else
            self.write_udp(series: direction, tags: tags, values: values)
          end
        end
      end
      if ZuoraConnect.configuration.telegraf_debug
        Rails.logger.debug(self.format_metric_log('Telegraf', tags.to_s))
        Rails.logger.debug(self.format_metric_log('Telegraf', values.to_s))
        Rails.logger.debug(self.format_metric_log('Telegraf', "Writing '#{direction.capitalize}': #{time.real.round(5)} ms"))
      end
    end


    def write_udp(series: '', tags: {}, values: {})
      return if !values.present?

      self.host.write InfluxDB::PointValue.new({series: series, tags: tags, values: values}).dump 
    rescue => ex
      self.connect
      Rails.logger.warn(self.format_metric_log('Telegraf','Failed to write udp'))
      Rails.logger.warn(self.format_metric_log('Telegraf', ex.class))
      Rails.logger.warn(self.format_metric_log('Telegraf', ex.message))
    end

    # Returns the process type if any
    def get_process_type
      p_type = 'Unknown'
      if ENV['HOSTNAME'] && ENV['DEIS_APP']
        temp = ENV['HOSTNAME'].split(ENV['DEIS_APP'])[1]
        temp = temp.split(/(-[0-9a-zA-Z]{5})$/)[0] # remove the 5 char hash
        p_type = temp[1, temp.rindex("-")-1]
      end
      return p_type
    end

    def format_metric_log(message, dump = nil)
      message_color, dump_color = "1;91", "0;1"
      log_entry = "  \e[#{message_color}m#{message}\e[0m   "
      log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump
      log_entry
    end
  end
end