Commit f09786f1 authored by James Lopez's avatar James Lopez

Merge branch '42639-move-helper' into 'master'

Move custom metrics helpers out of EE

See merge request gitlab-org/gitlab!28813
parents fe74f803 b8df77c9
# frozen_string_literal: true
module CustomMetricsHelper
def custom_metrics_data(project, metric)
custom_metrics_path = project.namespace.becomes(::Namespace)
{
'custom-metrics-path' => url_for([custom_metrics_path, project, metric]),
'metric-persisted' => metric.persisted?.to_s,
'edit-project-service-path' => edit_project_service_path(project, PrometheusService),
'validate-query-path' => validate_query_project_prometheus_metrics_path(project),
'title' => metric.title.to_s,
'query' => metric.query.to_s,
'y-label' => metric.y_label.to_s,
'unit' => metric.unit.to_s,
'group' => metric.group.to_s,
'legend' => metric.legend.to_s
}
end
end
...@@ -18,6 +18,10 @@ module EnvironmentsHelper ...@@ -18,6 +18,10 @@ module EnvironmentsHelper
} }
end end
def custom_metrics_available?(project)
can?(current_user, :admin_project, project)
end
def metrics_data(project, environment) def metrics_data(project, environment)
{ {
"settings-path" => edit_project_service_path(project, 'prometheus'), "settings-path" => edit_project_service_path(project, 'prometheus'),
...@@ -39,7 +43,10 @@ module EnvironmentsHelper ...@@ -39,7 +43,10 @@ module EnvironmentsHelper
"has-metrics" => "#{environment.has_metrics?}", "has-metrics" => "#{environment.has_metrics?}",
"prometheus-status" => "#{environment.prometheus_status}", "prometheus-status" => "#{environment.prometheus_status}",
"external-dashboard-url" => project.metrics_setting_external_dashboard_url, "external-dashboard-url" => project.metrics_setting_external_dashboard_url,
"environment-state" => "#{environment.state}" "environment-state" => "#{environment.state}",
"custom-metrics-path" => project_prometheus_metrics_path(project),
"validate-query-path" => validate_query_project_prometheus_metrics_path(project),
"custom-metrics-available" => "#{custom_metrics_available?(project)}"
} }
end end
......
...@@ -13,7 +13,7 @@ There are two ways to set up Prometheus integration, depending on where your app ...@@ -13,7 +13,7 @@ There are two ways to set up Prometheus integration, depending on where your app
- For deployments on Kubernetes, GitLab can automatically [deploy and manage Prometheus](#managed-prometheus-on-kubernetes). - For deployments on Kubernetes, GitLab can automatically [deploy and manage Prometheus](#managed-prometheus-on-kubernetes).
- For other deployment targets, simply [specify the Prometheus server](#manual-configuration-of-prometheus). - For other deployment targets, simply [specify the Prometheus server](#manual-configuration-of-prometheus).
Once enabled, GitLab will automatically detect metrics from known services in the [metric library](#monitoring-cicd-environments). You are also able to [add your own metrics](#adding-additional-metrics-premium) as well. Once enabled, GitLab will automatically detect metrics from known services in the [metric library](#monitoring-cicd-environments). You can also [add your own metrics](#adding-custom-metrics).
## Enabling Prometheus Integration ## Enabling Prometheus Integration
...@@ -132,9 +132,10 @@ GitLab will automatically scan the Prometheus server for metrics from known serv ...@@ -132,9 +132,10 @@ GitLab will automatically scan the Prometheus server for metrics from known serv
You can view the performance dashboard for an environment by [clicking on the monitoring button](../../../ci/environments.md#monitoring-environments). You can view the performance dashboard for an environment by [clicking on the monitoring button](../../../ci/environments.md#monitoring-environments).
### Adding additional metrics **(PREMIUM)** ### Adding custom metrics
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/3799) in [GitLab Premium](https://about.gitlab.com/pricing/) 10.6. > - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/3799) in [GitLab Premium](https://about.gitlab.com/pricing/) 10.6.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/28527) to [GitLab Core](https://about.gitlab.com/pricing/) 12.10.
Custom metrics can be monitored by adding them on the monitoring dashboard page. Once saved, they will be displayed on the environment performance dashboard provided that either: Custom metrics can be monitored by adding them on the monitoring dashboard page. Once saved, they will be displayed on the environment performance dashboard provided that either:
...@@ -191,8 +192,8 @@ You may create a new file from scratch or duplicate a GitLab-defined Prometheus ...@@ -191,8 +192,8 @@ You may create a new file from scratch or duplicate a GitLab-defined Prometheus
dashboard. dashboard.
NOTE: **Note:** NOTE: **Note:**
The custom metrics as defined below do not support alerts, unlike The metrics as defined below do not support alerts, unlike
[additional metrics](#adding-additional-metrics-premium). [custom metrics](#adding-custom-metrics).
#### Adding a new dashboard to your project #### Adding a new dashboard to your project
...@@ -654,9 +655,9 @@ Data from Prometheus charts on the metrics dashboard can be downloaded as CSV. ...@@ -654,9 +655,9 @@ Data from Prometheus charts on the metrics dashboard can be downloaded as CSV.
#### Managed Prometheus instances #### Managed Prometheus instances
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/6590) in [GitLab Ultimate](https://about.gitlab.com/pricing/) 11.2 for [custom metrics](#adding-additional-metrics-premium), and 11.3 for [library metrics](prometheus_library/metrics.md). > [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/6590) in [GitLab Ultimate](https://about.gitlab.com/pricing/) 11.2 for [custom metrics](#adding-custom-metrics), and 11.3 for [library metrics](prometheus_library/metrics.md).
For managed Prometheus instances using auto configuration, alerts for metrics [can be configured](#adding-additional-metrics-premium) directly in the performance dashboard. For managed Prometheus instances using auto configuration, alerts for metrics [can be configured](#adding-custom-metrics) directly in the performance dashboard.
To set an alert: To set an alert:
......
# frozen_string_literal: true
module EE
module CustomMetricsHelper
def custom_metrics_data(project, metric)
custom_metrics_path = project.namespace.becomes(::Namespace)
{
'custom-metrics-path' => url_for([custom_metrics_path, project, metric]),
'metric-persisted' => metric.persisted?.to_s,
'edit-project-service-path' => edit_project_service_path(project, PrometheusService),
'validate-query-path' => validate_query_project_prometheus_metrics_path(project),
'title' => metric.title.to_s,
'query' => metric.query.to_s,
'y-label' => metric.y_label.to_s,
'unit' => metric.unit.to_s,
'group' => metric.group.to_s,
'legend' => metric.legend.to_s
}
end
end
end
...@@ -26,15 +26,8 @@ module EE ...@@ -26,15 +26,8 @@ module EE
super.merge(ee_environments_folder_list_view_data) super.merge(ee_environments_folder_list_view_data)
end end
def custom_metrics_available?(project)
can?(current_user, :admin_project, project)
end
def metrics_data(project, environment) def metrics_data(project, environment)
ee_metrics_data = { ee_metrics_data = {
"custom-metrics-path" => project_prometheus_metrics_path(project),
"validate-query-path" => validate_query_project_prometheus_metrics_path(project),
"custom-metrics-available" => "#{custom_metrics_available?(project)}",
"alerts-endpoint" => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json), "alerts-endpoint" => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json),
"prometheus-alerts-available" => "#{can?(current_user, :read_prometheus_alerts, project)}" "prometheus-alerts-available" => "#{can?(current_user, :read_prometheus_alerts, project)}"
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
.col-lg-3 .col-lg-3
%p %p
= s_('PrometheusService|Custom metrics require Prometheus installed on a cluster with environment scope "*" OR a manually configured Prometheus to be available.') = s_('PrometheusService|Custom metrics require Prometheus installed on a cluster with environment scope "*" OR a manually configured Prometheus to be available.')
= link_to s_('PrometheusService|More information'), help_page_path('user/project/integrations/prometheus', anchor: 'adding-additional-metrics-premium'), target: '_blank', rel: "noopener noreferrer" = link_to s_('PrometheusService|More information'), help_page_path('user/project/integrations/prometheus', anchor: 'adding-custom-metrics'), target: '_blank', rel: "noopener noreferrer"
.col-lg-9 .col-lg-9
.card.custom-monitored-metrics.js-panel-custom-monitored-metrics{ data: { active_custom_metrics: project_prometheus_metrics_path(project), environments_data: environments_list_data, service_active: "#{@service.active}" } } .card.custom-monitored-metrics.js-panel-custom-monitored-metrics{ data: { active_custom_metrics: project_prometheus_metrics_path(project), environments_data: environments_list_data, service_active: "#{@service.active}" } }
......
...@@ -26,9 +26,6 @@ describe EnvironmentsHelper do ...@@ -26,9 +26,6 @@ describe EnvironmentsHelper do
it 'returns additional configuration' do it 'returns additional configuration' do
expect(subject).to include( expect(subject).to include(
'custom-metrics-path' => project_prometheus_metrics_path(project),
'validate-query-path' => validate_query_project_prometheus_metrics_path(project),
'custom-metrics-available' => 'true',
'alerts-endpoint' => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json), 'alerts-endpoint' => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json),
'prometheus-alerts-available' => 'true', 'prometheus-alerts-available' => 'true',
'logs_path' => project_logs_path(project, environment_name: environment.name) 'logs_path' => project_logs_path(project, environment_name: environment.name)
...@@ -72,22 +69,4 @@ describe EnvironmentsHelper do ...@@ -72,22 +69,4 @@ describe EnvironmentsHelper do
) )
end end
end end
describe '#custom_metrics_available?' do
subject { helper.custom_metrics_available?(project) }
before do
project.add_maintainer(user)
allow(helper).to receive(:current_user).and_return(user)
allow(helper).to receive(:can?)
.with(user, :admin_project, project)
.and_return(true)
end
it 'returns true' do
expect(subject).to eq(true)
end
end
end end
...@@ -34,7 +34,10 @@ describe EnvironmentsHelper do ...@@ -34,7 +34,10 @@ describe EnvironmentsHelper do
'has-metrics' => "#{environment.has_metrics?}", 'has-metrics' => "#{environment.has_metrics?}",
'prometheus-status' => "#{environment.prometheus_status}", 'prometheus-status' => "#{environment.prometheus_status}",
'external-dashboard-url' => nil, 'external-dashboard-url' => nil,
'environment-state' => environment.state 'environment-state' => environment.state,
'custom-metrics-path' => project_prometheus_metrics_path(project),
'validate-query-path' => validate_query_project_prometheus_metrics_path(project),
'custom-metrics-available' => 'true'
) )
end end
...@@ -58,4 +61,22 @@ describe EnvironmentsHelper do ...@@ -58,4 +61,22 @@ describe EnvironmentsHelper do
it { is_expected.to include('environment-state' => 'stopped') } it { is_expected.to include('environment-state' => 'stopped') }
end end
end end
describe '#custom_metrics_available?' do
subject { helper.custom_metrics_available?(project) }
before do
project.add_maintainer(user)
allow(helper).to receive(:current_user).and_return(user)
allow(helper).to receive(:can?)
.with(user, :admin_project, project)
.and_return(true)
end
it 'returns true' do
expect(subject).to eq(true)
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment