class ZuoraConnect::AppInstanceBase

def self.write_to_telegraf(endpoint_name: nil, method_name: nil, status_code: nil, response_time: nil, db_runtime: nil, view_runtime: nil, content_type: nil, direction: nil, error_type: nil, app_instance: nil, function_name: nil)

Write to telegraf
def self.write_to_telegraf(endpoint_name: nil, method_name: nil, status_code: nil, response_time: nil, db_runtime: nil, view_runtime: nil, content_type: nil, direction: nil, error_type: nil, app_instance: nil, function_name: nil)
  # To avoid writing metrics from rspec tests
  if ENV['DEIS_APP']
    # Getting the process type
    p_type = ZuoraConnect::AppInstanceBase.get_process_type
    if direction == "inbound" && ZuoraConnect.configuration.enable_inbound_metrics_flag
      Thread.current[:appinstance].present? ? app_instance = Thread.current[:appinstance].id : app_instance = 0
      # Separately handling 200 and non 200 as influx does not accept nil as a value
      if db_runtime && view_runtime
        # 200 requests
        begin
          ZuoraConnect.configuration.telegraf_client.write(ZuoraConnect.configuration.influxdb_series_name_inbound,
              tags: {"app_name": "#{ZuoraConnect.configuration.app_name}", "controller_action": endpoint_name, "content-type": content_type, method: method_name, status: status_code, process_type: p_type, "app_instance": app_instance},
              values: {response_time: response_time, db_time: db_runtime, view_time: view_runtime})
        rescue => e
          raise e
        end
      else
        # non 200 requests
        begin
          ZuoraConnect.configuration.telegraf_client.write(ZuoraConnect.configuration.influxdb_series_name_inbound,
              tags: {"app_name": "#{ZuoraConnect.configuration.app_name}", "controller_action": endpoint_name, "content-type": content_type, method: method_name, status: status_code, process_type: p_type, "app_instance": app_instance},
              values: {response_time: response_time})
        rescue=> e
          raise e
        end
      end
    elsif direction == "outbound" && ZuoraConnect.configuration.enable_outbound_metrics_flag
      # if there is an error
      if error_type
        begin
          ZuoraConnect.configuration.telegraf_client.write(ZuoraConnect.configuration.influxdb_series_name_outbound,
              tags: {"app_name": "#{ZuoraConnect.configuration.app_name}", endpoint: endpoint_name, status: status_code, process_type: p_type, "app_instance": app_instance, "function_name": function_name, method: method_name, "error_type": error_type},
              values: {response_time: response_time})
        rescue => e
          raise e
        end
      else
        begin
          ZuoraConnect.configuration.telegraf_client.write(ZuoraConnect.configuration.influxdb_series_name_outbound,
              tags: {"app_name": "#{ZuoraConnect.configuration.app_name}", endpoint: endpoint_name, status: status_code, process_type: p_type, "app_instance": app_instance, "function_name": function_name, method: method_name},
              values: {response_time: response_time})
        rescue => e
          raise e
        end
      end
    end
  end
end