2017-03-15 00:04:00 -04:00
# Prometheus integration
2017-03-16 14:12:54 -04:00
> [Introduced][ce-8935] in GitLab 9.0.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
GitLab offers powerful integration with [Prometheus] for monitoring your apps.
Metrics are retrieved from the configured Prometheus server, and then displayed
within the GitLab interface.
Each project can be configured with its own specific Prometheus server, see the
[configuration ](#configuration ) section for more details. If you have a single
Prometheus server which monitors all of your infrastructure, you can pre-fill
the settings page with a default template. To configure the template, see the
[Services templates ](services_templates.md ) document.
2017-03-15 00:04:00 -04:00
## Requirements
Integration with Prometheus requires the following:
2017-03-15 05:37:07 -04:00
1. GitLab 9.0 or higher
2017-05-21 13:49:23 -04:00
1. The [Kubernetes integration must be enabled][kube] on your project
2017-03-15 05:37:07 -04:00
1. Your app must be deployed on [Kubernetes][]
1. Prometheus must be configured to collect Kubernetes metrics
1. Each metric must be have a label to indicate the environment
1. GitLab must have network connectivity to the Prometheus sever
There are a few steps necessary to set up integration between Prometheus and
GitLab.
## Configuring Prometheus to collect Kubernetes metrics
In order for Prometheus to collect Kubernetes metrics, you first must have a
Prometheus server up and running. You have two options here:
2017-03-15 00:04:00 -04:00
2017-03-16 14:12:54 -04:00
- If you installed Omnibus GitLab inside of Kubernetes, you can simply use the
[bundled version of Prometheus][promgldocs]. In that case, follow the info in the
[Omnibus GitLab section ](#configuring-omnibus-gitlab-prometheus-to-monitor-kubernetes )
below.
- If you are using GitLab.com or installed GitLab outside of Kubernetes, you
will likely need to run a Prometheus server within the Kubernetes cluster.
Once installed, the easiest way to monitor Kubernetes is to simply use
Prometheus' support for [Kubernetes Service Discovery][prometheus-k8s-sd].
In that case, follow the instructions on
[configuring your own Prometheus server within Kubernetes ](#configuring-your-own-prometheus-server-within-kubernetes ).
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
### Configuring Omnibus GitLab Prometheus to monitor Kubernetes
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
With Omnibus GitLab running inside of Kubernetes, you can leverage the bundled
version of Prometheus to collect the required metrics.
2017-03-15 00:04:00 -04:00
2017-03-16 14:12:54 -04:00
1. Read how to configure the bundled Prometheus server in the
[Administration guide][gitlab-prometheus-k8s-monitor].
1. Now that Prometheus is configured, proceed on
[configuring the Prometheus project service in GitLab ](#configuration-in-gitlab ).
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
### Configuring your own Prometheus server within Kubernetes
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
Setting up and configuring Prometheus within Kubernetes is quick and painless.
The Prometheus project provides an [official Docker image][prometheus-docker-image]
which we can use as a starting point.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
To get started quickly, we have provided a [sample YML file][prometheus-yml]
that can be used as a template. This file will create a `prometheus` **Namespace** ,
**Service**, **Deployment** , and **ConfigMap** in Kubernetes. You can upload
2017-03-15 13:02:44 -04:00
this file to the Kubernetes dashboard using ** + Create** at the top right.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
![Deploy Prometheus ](img/prometheus_yaml_deploy.png )
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
Or use `kubectl` :
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
```bash
kubectl apply -f path/to/prometheus.yml
```
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
Once deployed, you should see the Prometheus service, deployment, and
pod start within the `prometheus` namespace. The server will begin to collect
metrics from each Kubernetes Node in the cluster, based on the configuration
provided in the template.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
Since GitLab is not running within Kubernetes, the template provides external
network access via a `NodePort` running on `30090` . This method allows access
to be controlled using provider firewall rules, like within Google Compute Engine.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
Since a `NodePort` does not automatically have firewall rules created for it,
one will need to be created manually to allow access. In GCP/GKE, you will want
to confirm the Node that the Prometheus pod is running on. This can be done
either by looking at the Pod in the Kubernetes dashboard, or by running:
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
```bash
kubectl describe pods -n prometheus
```
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
Next on GKE, we need to get the `tag` of the Node or VM Instance, so we can
create an accurate firewall rule. The easiest way to do this is to go into the
Google Cloud Platform Compute console and select the VM instance that matches
the name of the Node gathered from the step above. In this case, the node tag
needed is `gke-prometheus-demo-5d5ada10-node` . Also make a note of the
**External IP**, which will be the IP address the Prometheus server is reachable
on.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
![GCP Node Detail ](img/prometheus_gcp_node_name.png )
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
Armed with the proper Node tag, the firewall rule can now be created
specifically for this node. To create the firewall rule, open the Google Cloud
Platform Networking console, and select **Firewall Rules** .
2017-03-15 00:04:00 -04:00
Create a new rule:
2017-03-15 05:37:07 -04:00
- Specify the source IP range to match your desired access list, which should
include your GitLab server. A sample of GitLab.com's IP address range is
available [in this issue][gitlab.com-ip-range], but note that GitLab.com's IPs
are subject to change without prior notification.
- Allowed protocol and port should be `tcp:30090` .
- The target tags should match the Node tag identified earlier in this step.
![GCP Firewall Rule ](img/prometheus_gcp_firewall_rule.png )
---
2017-03-15 13:02:44 -04:00
Now that Prometheus is configured, proceed to
[configure the Prometheus project service in GitLab ](##configuration-in-gitlab ).
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
## Configuration in GitLab
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
The actual configuration of Prometheus integration within GitLab is very simple.
All you will need is the DNS or IP address of the Prometheus server you'd like
to integrate with.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
1. Navigate to the [Integrations page ](project_services.md#accessing-the-project-services )
1. Click the **Prometheus** service
1. Provide the base URL of the your server, for example `http://prometheus.example.com/` .
The **Test Settings** button can be used to confirm connectivity from GitLab
to the Prometheus server.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
![Configure Prometheus Service ](img/prometheus_service_configuration.png )
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
## Metrics and Labels
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
GitLab retrieves performance data from two metrics, `container_cpu_usage_seconds_total`
and `container_memory_usage_bytes` . These metrics are collected from the
Kubernetes pods via Prometheus, and report CPU and Memory utilization of each
container or Pod running in the cluster.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
In order to isolate and only display relevant metrics for a given environment
however, GitLab needs a method to detect which pods are associated. To do that,
GitLab will specifically request metrics that have an `environment` tag that
matches the [$CI_ENVIRONMENT_SLUG][ci-environment-slug].
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
If you are using [GitLab Auto-Deploy][autodeploy] and one of the methods of
configuring Prometheus above, the `environment` will be automatically added.
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
### GitLab Prometheus queries
2017-03-15 00:04:00 -04:00
2017-03-15 05:37:07 -04:00
The queries utilized by GitLab are shown in the following table.
2017-03-15 00:04:00 -04:00
| Metric | Query |
2017-03-15 05:37:07 -04:00
| ------ | ----- |
2017-03-28 10:03:02 -04:00
| Average Memory (MB) | `(sum(container_memory_usage_bytes{container_name!="POD",environment="$CI_ENVIRONMENT_SLUG"}) / count(container_memory_usage_bytes{container_name!="POD",environment="$CI_ENVIRONMENT_SLUG"})) /1024/1024` |
| Average CPU Utilization (%) | `sum(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="$CI_ENVIRONMENT_SLUG"}[2m])) / count(container_cpu_usage_seconds_total{container_name!="POD",environment="$CI_ENVIRONMENT_SLUG"}) * 100` |
2017-03-15 00:04:00 -04:00
2017-03-15 13:02:44 -04:00
## Monitoring CI/CD Environments
2017-03-16 14:12:54 -04:00
Once configured, GitLab will attempt to retrieve performance metrics for any
2017-05-21 13:49:23 -04:00
environment which has had a successful deployment.
2017-03-15 13:02:44 -04:00
2017-05-21 13:49:23 -04:00
[Learn more about monitoring environments. ](../../../ci/environments.md#monitoring-environments )
2017-03-15 13:02:44 -04:00
2017-05-21 13:49:23 -04:00
## Determining the performance impact of a merge
2017-05-09 00:15:34 -04:00
2017-05-19 03:02:46 -04:00
> [Introduced][ce-10408] in GitLab 9.2.
2017-06-19 07:19:36 -04:00
> GitLab 9.3 added the [numeric comparison](https://gitlab.com/gitlab-org/gitlab-ce/issues/27439) of the 30 minute averages.
2017-05-09 00:15:34 -04:00
2017-05-21 13:49:23 -04:00
Developers can view the performance impact of their changes within the merge
2017-06-19 07:19:36 -04:00
request workflow. When a source branch has been deployed to an environment, a sparkline and numeric comparison of the average memory consumption will appear. On the sparkline, a dot
2017-05-21 13:49:23 -04:00
indicates when the current changes were deployed, with up to 30 minutes of
2017-06-19 07:19:36 -04:00
performance data displayed before and after. The comparison shows the difference between the 30 minute average before and after the deployment. This information is updated after
2017-05-21 13:49:23 -04:00
each commit has been deployed.
2017-05-09 00:15:34 -04:00
2017-06-19 07:19:36 -04:00
Once merged and the target branch has been redeployed, the metrics will switch
2017-05-21 13:49:23 -04:00
to show the new environments this revision has been deployed to.
2017-05-19 03:02:46 -04:00
2017-05-21 13:49:23 -04:00
Performance data will be available for the duration it is persisted on the
Prometheus server.
2017-05-19 03:02:46 -04:00
![Merge Request with Performance Impact ](img/merge_request_performance.png )
2017-05-09 00:15:34 -04:00
2017-03-15 00:04:00 -04:00
## Troubleshooting
2017-04-06 17:42:56 -04:00
If the "Attempting to load performance data" screen continues to appear, it could be due to:
2017-03-16 14:12:54 -04:00
2017-03-15 13:02:44 -04:00
- No successful deployments have occurred to this environment.
2017-03-16 14:12:54 -04:00
- Prometheus does not have performance data for this environment, or the metrics
are not labeled correctly. To test this, connect to the Prometheus server and
[run a query ](#gitlab-prometheus-queries ), replacing `$CI_ENVIRONMENT_SLUG`
with the name of your environment.
2017-03-15 13:02:44 -04:00
2017-03-15 05:37:07 -04:00
[autodeploy]: ../../../ci/autodeploy/index.md
2017-03-15 00:04:00 -04:00
[kubernetes]: https://kubernetes.io
2017-05-21 13:49:23 -04:00
[kube]: ./kubernetes.md
2017-03-15 00:04:00 -04:00
[prometheus-k8s-sd]: https://prometheus.io/docs/operating/configuration/#< kubernetes_sd_config >
2017-03-15 05:37:07 -04:00
[prometheus]: https://prometheus.io
[gitlab-prometheus-k8s-monitor]: ../../../administration/monitoring/prometheus/index.md#configuring-prometheus-to-monitor-kubernetes
2017-03-15 00:04:00 -04:00
[prometheus-docker-image]: https://hub.docker.com/r/prom/prometheus/
[prometheus-yml]:samples/prometheus.yml
[gitlab.com-ip-range]: https://gitlab.com/gitlab-com/infrastructure/issues/434
[ci-environment-slug]: https://docs.gitlab.com/ce/ci/variables/#predefined-variables-environment-variables
2017-03-15 05:37:07 -04:00
[ce-8935]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/8935
2017-05-09 00:15:34 -04:00
[ce-10408]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/10408
2017-03-15 05:37:07 -04:00
[promgldocs]: ../../../administration/monitoring/prometheus/index.md