module Middleware

def self.get_process_type

Returns the process type if any
def self.get_process_type
  p_type = nil
  if ENV['HOSTNAME']
    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 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)

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)
  # Getting the process type
  p_type = Middleware::get_process_type
  # Separately handling 200 and non 200 as influx does not accept nil as a value
  if db_runtime && view_runtime
    # 200 requests
    ZuoraConnect.configuration.telegraf_client.write(ZuoraConnect.configuration.app_name_inbound,
        tags: {endpoint: endpoint_name, "content-type": content_type, method: method_name, status: status_code, process_type: p_type},
        values: {response_time: response_time, db_time: db_runtime, view_time: view_runtime})
  else
    # non 200 requests
    ZuoraConnect.configuration.telegraf_client.write(ZuoraConnect.configuration.app_name_inbound,
        tags: {endpoint: endpoint_name, "content-type": content_type, method: method_name, status: status_code, process_type: p_type},
        values: {response_time: response_time})
  end
end