class ZuoraConnect::MetricsMiddleware

def process_prometheus_metric(type: 'none', metrics: {})

def process_prometheus_metric(type: 'none', metrics: {})
  return if metrics.blank?
  prometheus = Prometheus::Client.registry
  most_recent_aggregation = {}
  if Prometheus::Client.config.data_store.is_a?(Prometheus::Client::DataStores::DirectFileStore)
    most_recent_aggregation[:aggregation] = :most_recent
  end
  metrics.each do |key, value|
    next if %w[app_name url].include?(key.to_s)
    if value.is_a?(Hash)
      process_prometheus_metric(type: key.to_s, metrics: value)
    else
      gauge_name = key.to_s.downcase.gsub(/[^a-z0-9_]/, '_')
      gauge = prometheus.get(gauge_name.to_sym) || prometheus.gauge(
        gauge_name.to_sym,
        docstring: "#{key} metric",
        labels: %i(type name),
        preset_labels: { type: type, name: ZuoraObservability::Env.app_name },
        store_settings: most_recent_aggregation
      )
      gauge.set(value)
    end
  end
end