diff options
Diffstat (limited to 'lib/gitlab/metrics/dashboard/stages')
11 files changed, 0 insertions, 565 deletions
diff --git a/lib/gitlab/metrics/dashboard/stages/base_stage.rb b/lib/gitlab/metrics/dashboard/stages/base_stage.rb index c2a8a88108f..b869a633030 100644 --- a/lib/gitlab/metrics/dashboard/stages/base_stage.rb +++ b/lib/gitlab/metrics/dashboard/stages/base_stage.rb @@ -5,8 +5,6 @@ module Gitlab module Dashboard module Stages class BaseStage - include Gitlab::Metrics::Dashboard::Defaults - attr_reader :project, :dashboard, :params def initialize(project, dashboard, params) diff --git a/lib/gitlab/metrics/dashboard/stages/cluster_endpoint_inserter.rb b/lib/gitlab/metrics/dashboard/stages/cluster_endpoint_inserter.rb deleted file mode 100644 index 56a82d1df46..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/cluster_endpoint_inserter.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - class ClusterEndpointInserter < BaseStage - def transform! - verify_params - end - - private - - def error!(message) - raise Errors::DashboardProcessingError, message - end - - def query_type(metric) - metric[:query] ? :query : :query_range - end - - def query_for_metric(metric) - query = metric[query_type(metric)] - - raise Errors::MissingQueryError, 'Each "metric" must define one of :query or :query_range' unless query - - query - end - - def verify_params - raise Errors::DashboardProcessingError, _('Cluster is required for Stages::ClusterEndpointInserter') unless params[:cluster] - raise Errors::DashboardProcessingError, _('Cluster type must be specified for Stages::ClusterEndpointInserter') unless params[:cluster_type] - end - end - end - end - end -end diff --git a/lib/gitlab/metrics/dashboard/stages/common_metrics_inserter.rb b/lib/gitlab/metrics/dashboard/stages/common_metrics_inserter.rb deleted file mode 100644 index 62479ed6de4..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/common_metrics_inserter.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - class CommonMetricsInserter < BaseStage - # For each metric in the dashboard config, attempts to - # find a corresponding database record. If found, - # includes the record's id in the dashboard config. - def transform! - common_metrics = ::PrometheusMetricsFinder.new(common: true).execute - - for_metrics do |metric| - metric_record = common_metrics.find { |m| m.identifier == metric[:id] } - metric[:metric_id] = metric_record.id if metric_record - end - end - end - end - end - end -end diff --git a/lib/gitlab/metrics/dashboard/stages/custom_dashboard_metrics_inserter.rb b/lib/gitlab/metrics/dashboard/stages/custom_dashboard_metrics_inserter.rb deleted file mode 100644 index 5ed4466f440..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/custom_dashboard_metrics_inserter.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - # Acts on metrics which have been ingested from source controlled dashboards - class CustomDashboardMetricsInserter < BaseStage - # For each metric in the dashboard config, attempts to - # find a corresponding database record. If found, includes - # the record's id in the dashboard config. - def transform! - database_metrics = ::PrometheusMetricsFinder.new(common: false, group: :custom, project: project).execute - - for_metrics do |metric| - metric_record = database_metrics.find { |m| m.identifier == metric[:id] } - metric[:metric_id] = metric_record.id if metric_record - end - end - end - end - end - end -end diff --git a/lib/gitlab/metrics/dashboard/stages/custom_metrics_details_inserter.rb b/lib/gitlab/metrics/dashboard/stages/custom_metrics_details_inserter.rb deleted file mode 100644 index 06cfa5cc58e..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/custom_metrics_details_inserter.rb +++ /dev/null @@ -1,40 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - class CustomMetricsDetailsInserter < BaseStage - def transform! - dashboard[:panel_groups].each do |panel_group| - next unless panel_group - - has_custom_metrics = custom_group_titles.include?(panel_group[:group]) - panel_group[:has_custom_metrics] = has_custom_metrics - - panel_group[:panels].each do |panel| - next unless panel - - panel[:metrics].each do |metric| - next unless metric - - metric[:edit_path] = has_custom_metrics ? edit_path(metric) : nil - end - end - end - end - - private - - def custom_group_titles - @custom_group_titles ||= Enums::PrometheusMetric.custom_group_details.values.map { |group_details| group_details[:group_title] } - end - - def edit_path(metric) - Gitlab::Routing.url_helpers.edit_project_prometheus_metric_path(project, metric[:metric_id]) - end - end - end - end - end -end diff --git a/lib/gitlab/metrics/dashboard/stages/custom_metrics_inserter.rb b/lib/gitlab/metrics/dashboard/stages/custom_metrics_inserter.rb deleted file mode 100644 index 3b49eb1c837..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/custom_metrics_inserter.rb +++ /dev/null @@ -1,109 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - class CustomMetricsInserter < BaseStage - # Inserts project-specific metrics into the dashboard - # config. If there are no project-specific metrics, - # this will have no effect. - def transform! - custom_metrics = PrometheusMetricsFinder.new(project: project, ordered: true).execute - custom_metrics = Gitlab::Utils.stable_sort_by(custom_metrics) { |metric| -metric.priority } - - custom_metrics.each do |project_metric| - group = find_or_create_panel_group(dashboard[:panel_groups], project_metric) - panel = find_or_create_panel(group[:panels], project_metric) - find_or_create_metric(panel[:metrics], project_metric) - end - end - - private - - # Looks for a panel_group corresponding to the - # provided metric object. If unavailable, inserts one. - # @param panel_groups [Array<Hash>] - # @param metric [PrometheusMetric] - def find_or_create_panel_group(panel_groups, metric) - panel_group = find_panel_group(panel_groups, metric) - return panel_group if panel_group - - panel_group = new_panel_group(metric) - panel_groups << panel_group - - panel_group - end - - # Looks for a panel corresponding to the provided - # metric object. If unavailable, inserts one. - # @param panels [Array<Hash>] - # @param metric [PrometheusMetric] - def find_or_create_panel(panels, metric) - panel = find_panel(panels, metric) - return panel if panel - - panel = new_panel(metric) - panels << panel - - panel - end - - # Looks for a metric corresponding to the provided - # metric object. If unavailable, inserts one. - # @param metrics [Array<Hash>] - # @param metric [PrometheusMetric] - def find_or_create_metric(metrics, metric) - target_metric = find_metric(metrics, metric) - return target_metric if target_metric - - target_metric = new_metric(metric) - metrics << target_metric - - target_metric - end - - def find_panel_group(panel_groups, metric) - return unless panel_groups - - panel_groups.find { |group| group[:group] == metric.group_title } - end - - def find_panel(panels, metric) - return unless panels - - panel_identifiers = [DEFAULT_PANEL_TYPE, metric.title, metric.y_label] - panels.find { |panel| panel.values_at(:type, :title, :y_label) == panel_identifiers } - end - - def find_metric(metrics, metric) - return unless metrics - return unless metric.identifier - - metrics.find { |m| m[:id] == metric.identifier } - end - - def new_panel_group(metric) - { - group: metric.group_title, - panels: [] - } - end - - def new_panel(metric) - { - type: DEFAULT_PANEL_TYPE, - title: metric.title, - y_label: metric.y_label, - metrics: [] - } - end - - def new_metric(metric) - metric.to_metric_hash - end - end - end - end - end -end diff --git a/lib/gitlab/metrics/dashboard/stages/grafana_formatter.rb b/lib/gitlab/metrics/dashboard/stages/grafana_formatter.rb deleted file mode 100644 index 03370ae7370..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/grafana_formatter.rb +++ /dev/null @@ -1,151 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - class GrafanaFormatter < BaseStage - include Gitlab::Utils::StrongMemoize - - CHART_TYPE = 'area-chart' - PROXY_PATH = 'api/v1/query_range' - - # Reformats the specified panel in the Gitlab - # dashboard-yml format - def transform! - validate_input! - - new_dashboard = formatted_dashboard - - dashboard.clear - dashboard.merge!(new_dashboard) - end - - private - - def validate_input! - ::Grafana::Validator.new( - grafana_dashboard, - datasource, - panel, - query_params - ).validate! - rescue ::Grafana::Validator::Error => e - raise ::Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError, e.message - end - - def formatted_dashboard - { panel_groups: [{ panels: [formatted_panel] }] } - end - - def formatted_panel - { - title: panel[:title], - type: CHART_TYPE, - y_label: '', # Grafana panels do not include a Y-Axis label - metrics: panel[:targets].map.with_index do |target, idx| - formatted_metric(target, idx) - end - } - end - - def formatted_metric(metric, idx) - { - id: "#{metric[:legendFormat]}_#{idx}", - query_range: format_query(metric), - label: replace_variables(metric[:legendFormat]) - }.compact - end - - # Panel specified by the url from the Grafana dashboard - def panel - strong_memoize(:panel) do - grafana_dashboard[:dashboard][:panels].find do |panel| - query_params[:panelId] ? matching_panel?(panel) : valid_panel?(panel) - end - end - end - - # Determines whether a given panel is the one - # specified by the linked grafana url - def matching_panel?(panel) - panel[:id].to_s == query_params[:panelId] - end - - # Determines whether any given panel has the potenial - # to return valid results from grafana/prometheus - def valid_panel?(panel) - ::Grafana::Validator - .new(grafana_dashboard, datasource, panel, query_params) - .valid? - end - - # Grafana url query parameters. Includes information - # on which panel to select and time range. - def query_params - strong_memoize(:query_params) do - Gitlab::Metrics::Dashboard::Url.parse_query(grafana_url) - end - end - - # Reformats query for compatibility with prometheus api. - def format_query(metric) - expression = remove_new_lines(metric[:expr]) - expression = replace_variables(expression) - replace_global_variables(expression, metric) - end - - # Accomodates instance-defined Grafana variables. - # These are variables defined by users, and values - # must be provided in the query parameters. - def replace_variables(expression) - return expression unless grafana_dashboard[:dashboard][:templating] - - grafana_dashboard[:dashboard][:templating][:list] - .sort_by { |variable| variable[:name].length } - .each do |variable| - variable_value = query_params[:"var-#{variable[:name]}"] - - expression = expression.gsub("$#{variable[:name]}", variable_value) - expression = expression.gsub("[[#{variable[:name]}]]", variable_value) - expression = expression.gsub("{{#{variable[:name]}}}", variable_value) - end - - expression - end - - # Replaces Grafana global built-in variables with values. - # Only $__interval and $__from and $__to are supported. - # - # See https://grafana.com/docs/reference/templating/#global-built-in-variables - def replace_global_variables(expression, metric) - expression = expression.gsub('$__interval', metric[:interval]) if metric[:interval] - expression = expression.gsub('$__from', query_params[:from]) - expression.gsub('$__to', query_params[:to]) - end - - # Removes new lines from expression. - def remove_new_lines(expression) - expression.gsub(/\R+/, '') - end - - # Grafana datasource object corresponding to the - # specified dashboard - def datasource - params[:datasource] - end - - # The specified Grafana dashboard - def grafana_dashboard - params[:grafana_dashboard] - end - - # The URL specifying which Grafana panel to embed - def grafana_url - params[:grafana_url] - end - end - end - end - end -end diff --git a/lib/gitlab/metrics/dashboard/stages/metric_endpoint_inserter.rb b/lib/gitlab/metrics/dashboard/stages/metric_endpoint_inserter.rb deleted file mode 100644 index d885d978524..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/metric_endpoint_inserter.rb +++ /dev/null @@ -1,56 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - class MetricEndpointInserter < BaseStage - def transform! - raise Errors::DashboardProcessingError, _('Environment is required for Stages::MetricEndpointInserter') unless params[:environment] - - for_metrics do |metric| - metric[:prometheus_endpoint_path] = endpoint_for_metric(metric) - end - end - - private - - def endpoint_for_metric(metric) - if params[:sample_metrics] - Gitlab::Routing.url_helpers.sample_metrics_project_environment_path( - project, - params[:environment], - identifier: metric[:id] - ) - else - Gitlab::Routing.url_helpers.prometheus_api_project_environment_path( - project, - params[:environment], - proxy_path: query_type(metric), - query: query_for_metric(metric) - ) - end - end - - def query_type(metric) - if metric[:query] - ::Prometheus::ProxyService::PROMETHEUS_QUERY_API.to_sym - else - ::Prometheus::ProxyService::PROMETHEUS_QUERY_RANGE_API.to_sym - end - end - - def query_for_metric(metric) - query = metric[query_type(metric)] - - raise Errors::MissingQueryError, 'Each "metric" must define one of :query or :query_range' unless query - - # We need to remove any newlines since our UrlBlocker does not allow - # multiline URLs. - query.to_s.squish - end - end - end - end - end -end diff --git a/lib/gitlab/metrics/dashboard/stages/panel_ids_inserter.rb b/lib/gitlab/metrics/dashboard/stages/panel_ids_inserter.rb deleted file mode 100644 index 239b5161256..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/panel_ids_inserter.rb +++ /dev/null @@ -1,61 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - class PanelIdsInserter < BaseStage - # For each panel within given dashboard inserts panel_id unique in scope of the dashboard - def transform! - missing_panel_groups! unless dashboard[:panel_groups] - - for_panels_group_with_panels do |panel_group, panel| - id = generate_panel_id(panel_group, panel) - remove_panel_ids! && break if duplicated_panel_id?(id) - - insert_panel_id(id, panel) - end - rescue ActiveModel::UnknownAttributeError => error - remove_panel_ids! - Gitlab::ErrorTracking.log_exception(error) - end - - private - - def generate_panel_id(group, panel) - ::PerformanceMonitoring::PrometheusPanel.new(panel.with_indifferent_access).id(group[:group]) - end - - def insert_panel_id(id, panel) - track_inserted_panel_ids(id, panel) - panel[:id] = id - end - - def track_inserted_panel_ids(id, panel) - panel_ids[id] = panel - end - - def duplicated_panel_id?(id) - panel_ids.key?(id) - end - - def remove_panel_ids! - panel_ids.each_value { |panel| panel.delete(:id) } - end - - def panel_ids - @_panel_ids ||= {} - end - - def for_panels_group_with_panels - for_panel_groups do |panel_group| - for_panels_in(panel_group) do |panel| - yield panel_group, panel - end - end - end - end - end - end - end -end diff --git a/lib/gitlab/metrics/dashboard/stages/track_panel_type.rb b/lib/gitlab/metrics/dashboard/stages/track_panel_type.rb deleted file mode 100644 index 71da779d16c..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/track_panel_type.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - class TrackPanelType < BaseStage - def transform! - for_panel_groups do |panel_group| - for_panels_in(panel_group) do |panel| - track_panel_type(panel) - end - end - end - - private - - def track_panel_type(panel) - panel_type = panel[:type] - - Gitlab::Tracking.event('MetricsDashboard::Chart', 'chart_rendered', label: panel_type) - end - end - end - end - end -end diff --git a/lib/gitlab/metrics/dashboard/stages/variable_endpoint_inserter.rb b/lib/gitlab/metrics/dashboard/stages/variable_endpoint_inserter.rb deleted file mode 100644 index b3ce0b79675..00000000000 --- a/lib/gitlab/metrics/dashboard/stages/variable_endpoint_inserter.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Metrics - module Dashboard - module Stages - class VariableEndpointInserter < BaseStage - VARIABLE_TYPE_METRIC_LABEL_VALUES = 'metric_label_values' - - def transform! - raise Errors::DashboardProcessingError, _('Environment is required for Stages::VariableEndpointInserter') unless params[:environment] - - for_variables do |variable_name, variable| - if variable.is_a?(Hash) && variable[:type] == VARIABLE_TYPE_METRIC_LABEL_VALUES - variable[:options][:prometheus_endpoint_path] = endpoint_for_variable(variable.dig(:options, :series_selector)) - end - end - end - - private - - def endpoint_for_variable(series_selector) - Gitlab::Routing.url_helpers.prometheus_api_project_environment_path( - project, - params[:environment], - proxy_path: ::Prometheus::ProxyService::PROMETHEUS_SERIES_API, - match: Array(series_selector) - ) - end - end - end - end - end -end |