Method: ZuoraConnect::MetricsMiddleware#process_prometheus_metric

Defined in:
lib/middleware/metrics_middleware.rb

#process_prometheus_metric(type: 'none', metrics: {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/middleware/metrics_middleware.rb', line 101

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