2018-09-04 05:42:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class PrometheusMetric < ApplicationRecord
|
2018-08-31 12:53:50 -04:00
|
|
|
belongs_to :project, validate: true, inverse_of: :prometheus_metrics
|
|
|
|
|
|
|
|
enum group: {
|
|
|
|
# built-in groups
|
2018-12-17 18:48:55 -05:00
|
|
|
nginx_ingress_vts: -1,
|
2018-08-31 12:53:50 -04:00
|
|
|
ha_proxy: -2,
|
|
|
|
aws_elb: -3,
|
|
|
|
nginx: -4,
|
|
|
|
kubernetes: -5,
|
2018-12-17 18:48:55 -05:00
|
|
|
nginx_ingress: -6,
|
2018-08-31 12:53:50 -04:00
|
|
|
|
|
|
|
# custom/user groups
|
|
|
|
business: 0,
|
|
|
|
response: 1,
|
|
|
|
system: 2
|
|
|
|
}
|
|
|
|
|
2018-12-21 06:11:58 -05:00
|
|
|
GROUP_DETAILS = {
|
|
|
|
# built-in groups
|
|
|
|
nginx_ingress_vts: {
|
|
|
|
group_title: _('Response metrics (NGINX Ingress VTS)'),
|
|
|
|
required_metrics: %w(nginx_upstream_responses_total nginx_upstream_response_msecs_avg),
|
|
|
|
priority: 10
|
|
|
|
}.freeze,
|
|
|
|
nginx_ingress: {
|
|
|
|
group_title: _('Response metrics (NGINX Ingress)'),
|
|
|
|
required_metrics: %w(nginx_ingress_controller_requests nginx_ingress_controller_ingress_upstream_latency_seconds_sum),
|
|
|
|
priority: 10
|
|
|
|
}.freeze,
|
|
|
|
ha_proxy: {
|
|
|
|
group_title: _('Response metrics (HA Proxy)'),
|
|
|
|
required_metrics: %w(haproxy_frontend_http_requests_total haproxy_frontend_http_responses_total),
|
|
|
|
priority: 10
|
|
|
|
}.freeze,
|
|
|
|
aws_elb: {
|
|
|
|
group_title: _('Response metrics (AWS ELB)'),
|
|
|
|
required_metrics: %w(aws_elb_request_count_sum aws_elb_latency_average aws_elb_httpcode_backend_5_xx_sum),
|
|
|
|
priority: 10
|
|
|
|
}.freeze,
|
|
|
|
nginx: {
|
|
|
|
group_title: _('Response metrics (NGINX)'),
|
|
|
|
required_metrics: %w(nginx_server_requests nginx_server_requestMsec),
|
|
|
|
priority: 10
|
|
|
|
}.freeze,
|
|
|
|
kubernetes: {
|
|
|
|
group_title: _('System metrics (Kubernetes)'),
|
|
|
|
required_metrics: %w(container_memory_usage_bytes container_cpu_usage_seconds_total),
|
|
|
|
priority: 5
|
|
|
|
}.freeze,
|
|
|
|
|
|
|
|
# custom/user groups
|
|
|
|
business: {
|
|
|
|
group_title: _('Business metrics (Custom)'),
|
|
|
|
priority: 0
|
|
|
|
}.freeze,
|
|
|
|
response: {
|
|
|
|
group_title: _('Response metrics (Custom)'),
|
|
|
|
priority: -5
|
|
|
|
}.freeze,
|
|
|
|
system: {
|
|
|
|
group_title: _('System metrics (Custom)'),
|
|
|
|
priority: -10
|
|
|
|
}.freeze
|
|
|
|
}.freeze
|
|
|
|
|
2018-08-31 12:53:50 -04:00
|
|
|
validates :title, presence: true
|
|
|
|
validates :query, presence: true
|
|
|
|
validates :group, presence: true
|
|
|
|
validates :y_label, presence: true
|
|
|
|
validates :unit, presence: true
|
|
|
|
|
2018-09-04 05:50:58 -04:00
|
|
|
validates :project, presence: true, unless: :common?
|
|
|
|
validates :project, absence: true, if: :common?
|
2018-08-31 12:53:50 -04:00
|
|
|
|
|
|
|
scope :common, -> { where(common: true) }
|
|
|
|
|
2018-12-21 06:11:58 -05:00
|
|
|
def priority
|
|
|
|
group_details(group).fetch(:priority)
|
|
|
|
end
|
2018-08-31 12:53:50 -04:00
|
|
|
|
|
|
|
def group_title
|
2018-12-21 06:11:58 -05:00
|
|
|
group_details(group).fetch(:group_title)
|
2018-08-31 12:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def required_metrics
|
2018-12-21 06:11:58 -05:00
|
|
|
group_details(group).fetch(:required_metrics, []).map(&:to_s)
|
2018-08-31 12:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_query_metric
|
|
|
|
Gitlab::Prometheus::Metric.new(id: id, title: title, required_metrics: required_metrics, weight: 0, y_label: y_label, queries: queries)
|
|
|
|
end
|
|
|
|
|
|
|
|
def queries
|
|
|
|
[
|
|
|
|
{
|
|
|
|
query_range: query,
|
|
|
|
unit: unit,
|
|
|
|
label: legend,
|
|
|
|
series: query_series
|
2018-09-04 17:09:29 -04:00
|
|
|
}.compact
|
2018-08-31 12:53:50 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def query_series
|
|
|
|
case legend
|
|
|
|
when 'Status Code'
|
2018-09-05 09:35:08 -04:00
|
|
|
[{
|
2018-08-31 12:53:50 -04:00
|
|
|
label: 'status_code',
|
|
|
|
when: [
|
|
|
|
{ value: '2xx', color: 'green' },
|
|
|
|
{ value: '4xx', color: 'orange' },
|
|
|
|
{ value: '5xx', color: 'red' }
|
|
|
|
]
|
2018-09-05 09:35:08 -04:00
|
|
|
}]
|
2018-08-31 12:53:50 -04:00
|
|
|
end
|
|
|
|
end
|
2018-12-21 06:11:58 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def group_details(group)
|
|
|
|
GROUP_DETAILS.fetch(group.to_sym)
|
|
|
|
end
|
2018-08-31 12:53:50 -04:00
|
|
|
end
|