prometheus.md 20.3 KB
Newer Older
1 2
# Prometheus integration

3
> [Introduced][ce-8935] in GitLab 9.0.
4

Joshua Lambert's avatar
Joshua Lambert committed
5
GitLab offers powerful integration with [Prometheus] for monitoring key metrics of your apps, directly within GitLab.
6
Metrics for each environment are retrieved from Prometheus, and then displayed
7 8
within the GitLab interface.

9
![Environment Dashboard](img/prometheus_dashboard.png)
10

11
There are two ways to set up Prometheus integration, depending on where your apps are running:
12 13 14

- 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).
15

16
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.
Joshua Lambert's avatar
Joshua Lambert committed
17 18 19 20

## Enabling Prometheus Integration

### Managed Prometheus on Kubernetes
21

Joshua Lambert's avatar
Joshua Lambert committed
22
> **Note**: [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/issues/28916) in GitLab 10.5
23

Joshua Lambert's avatar
Joshua Lambert committed
24
GitLab can seamlessly deploy and manage Prometheus on a [connected Kubernetes cluster](../clusters/index.md), making monitoring of your apps easy.
25

Joshua Lambert's avatar
Joshua Lambert committed
26
#### Requirements
27

28 29
- A [connected Kubernetes cluster](../clusters/index.md)
- Helm Tiller [installed by GitLab](../clusters/index.md#installing-applications)
30

Joshua Lambert's avatar
Joshua Lambert committed
31
#### Getting started
32

33
Once you have a connected Kubernetes cluster with Helm installed, deploying a managed Prometheus is as easy as a single click.
34

35
1. Go to the **Operations > Kubernetes** page to view your connected clusters
36 37
1. Select the cluster you would like to deploy Prometheus to
1. Click the **Install** button to deploy Prometheus to the cluster
38

39
![Managed Prometheus Deploy](img/prometheus_deploy.png)
40

Joshua Lambert's avatar
Joshua Lambert committed
41
#### About managed Prometheus deployments
42

43
Prometheus is deployed into the `gitlab-managed-apps` namespace, using the [official Helm chart](https://github.com/helm/charts/tree/master/stable/prometheus). Prometheus is only accessible within the cluster, with GitLab communicating through the [Kubernetes API](https://kubernetes.io/docs/concepts/overview/kubernetes-api/).
44

45
The Prometheus server will [automatically detect and monitor](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config) nodes, pods, and endpoints. To configure a resource to be monitored by Prometheus, simply set the following [Kubernetes annotations](https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/):
46

47 48 49
- `prometheus.io/scrape` to `true` to enable monitoring of the resource.
- `prometheus.io/port` to define the port of the metrics endpoint.
- `prometheus.io/path` to define the path of the metrics endpoint. Defaults to `/metrics`.
50

Joshua Lambert's avatar
Joshua Lambert committed
51
CPU and Memory consumption is monitored, but requires [naming conventions](prometheus_library/kubernetes.html#specifying-the-environment) in order to determine the environment. If you are using [Auto DevOps](../../../topics/autodevops/), this is handled automatically.
52

Joshua Lambert's avatar
Joshua Lambert committed
53
The [NGINX Ingress](../clusters/index.md#installing-applications) that is deployed by GitLab to clusters, is automatically annotated for monitoring providing key response metrics: latency, throughput, and error rates.
54

Joshua Lambert's avatar
Joshua Lambert committed
55
### Manual configuration of Prometheus
56

Joshua Lambert's avatar
Joshua Lambert committed
57
#### Requirements
58

59
Integration with Prometheus requires the following:
60

61
1. GitLab 9.0 or higher
Marcia Ramos's avatar
Marcia Ramos committed
62
1. Prometheus must be configured to collect one of the [supported metrics](prometheus_library/index.md)
63 64
1. Each metric must be have a label to indicate the environment
1. GitLab must have network connectivity to the Prometheus server
65

Joshua Lambert's avatar
Joshua Lambert committed
66
#### Getting started
67

68
Installing and configuring Prometheus to monitor applications is fairly straight forward.
69

70
1. [Install Prometheus](https://prometheus.io/docs/prometheus/latest/installation/)
Marcia Ramos's avatar
Marcia Ramos committed
71
1. Set up one of the [supported monitoring targets](prometheus_library/index.md)
72
1. Configure the Prometheus server to [collect their metrics](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config)
73

Joshua Lambert's avatar
Joshua Lambert committed
74
#### Configuration in GitLab
75

76 77 78
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.
79

80 81 82 83 84
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.
85

86
![Configure Prometheus Service](img/prometheus_service_configuration.png)
87

88 89
## Monitoring CI/CD Environments

90
Once configured, GitLab will attempt to retrieve performance metrics for any
91
environment which has had a successful deployment.
92

93
GitLab will automatically scan the Prometheus server for metrics from known servers like Kubernetes and NGINX, and attempt to identify individual environment. The supported metrics and scan process is detailed in our [Prometheus Metrics Library documentation](prometheus_library/index.md).
94

Joshua Lambert's avatar
Joshua Lambert committed
95
You can view the performance dashboard for an environment by [clicking on the monitoring button](../../../ci/environments.md#monitoring-environments).
96

97
### Adding additional metrics **(PREMIUM)**
98 99 100

> [Introduced](https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/3799) in [GitLab Premium](https://about.gitlab.com/pricing/) 10.6.

101 102 103 104
Custom metrics can be monitored by adding them on the Prometheus integration page. Once saved, they will be displayed on the environment performance dashboard provided that either:

- A [connected Kubernetes cluster](../clusters/index.md#adding-and-removing-clusters) with the environment scope of `*` is used and [Prometheus installed on the cluster](#enabling-prometheus-integration), or
- Prometheus is [manually configured](#manual-configuration-of-prometheus).
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

![Add New Metric](img/prometheus_add_metric.png)

A few fields are required:

- **Name**: Chart title
- **Type**: Type of metric. Metrics of the same type will be shown together.
- **Query**: Valid [PromQL query](https://prometheus.io/docs/prometheus/latest/querying/basics/).
- **Y-axis label**: Y axis title to display on the dashboard.
- **Unit label**: Query units, for example `req / sec`. Shown next to the value.

Multiple metrics can be displayed on the same chart if the fields **Name**, **Type**, and **Y-axis label** match between metrics. For example, a metric with **Name** `Requests Rate`, **Type** `Business`, and **Y-axis label** `rec / sec` would display on the same chart as a second metric with the same values. A **Legend label** is suggested if this feature used.

#### Query Variables

GitLab supports a limited set of [CI variables](../../../ci/variables/README.html) in the Prometheus query. This is particularly useful for identifying a specific environment, for example with `CI_ENVIRONMENT_SLUG`. The supported variables are:

- CI_ENVIRONMENT_SLUG
- KUBE_NAMESPACE

To specify a variable in a query, enclose it in curly braces with a leading percent. For example: `%{ci_environment_slug}`.

127
### Defining custom dashboards per project
128

129
> [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/issues/59974) in GitLab 12.1.
130

131 132
By default, all projects include a GitLab-defined Prometheus dashboard, which
includes a few key metrics, but you can also define your own custom dashboards.
133

134 135 136
NOTE: **Note:**
The custom metrics as defined below do not support alerts, unlike
[additional metrics](#adding-additional-metrics-premium).
137

138
Dashboards have several components:
139

140 141
- Panel groups, which comprise panels.
- Panels, which support one or more metrics.
142

143
To configure a custom dashboard:
144

145 146 147
1. Create a YAML file with the `.yml` extension under your repository's root
   directory inside `.gitlab/dashboards/`. For example, create
   `.gitlab/dashboards/prom_alerts.yml` with the following contents:
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
   ```yaml
   dashboard: 'Dashboard Title'
   panel_groups:
     - group: 'Group Title'
       panels:
         - type: area-chart
           title: "Chart Title"
           y_label: "Y-Axis"
           metrics:
             - id: metric_of_ages
               query_range: 'http_requests_total'
               label: "Metric of Ages"
               unit: "count"
   ```

   The above sample dashboard would display a single area chart. Each file should
   define the layout of the dashboard and the Prometheus queries used to populate
   data.

1. Save the file, commit, and push to your repository.
1. Navigate to your project's **Operations > Metrics** and choose the custom
   dashboard from the dropdown.

NOTE: **Note:**
Configuration files nested under subdirectories of `.gitlab/dashboards` are not
supported and will not be available in the UI.

The following tables outline the details of expected properties.

**Dashboard properties:**

| Property | Type | Required | Description |
181
| ------ | ------ | ------ | ------ |
182 183 184 185
| `dashboard` | string | yes | Heading for the dashboard. Only one dashboard should be defined per file. |
| `panel_groups` | array | yes | The panel groups which should be on the dashboard. |

**Panel group (`panel_groups`) properties:**
186

187
| Property | Type | Required | Description |
188 189
| ------ | ------ | ------ | ------ |
| `group` | string | required | Heading for the panel group. |
190
| `priority` | number | optional, defaults to order in file | Order to appear on the dashboard. Higher number means higher priority, which will be higher on the page. Numbers do not need to be consecutive. |
191 192
| `panels` | array | required | The panels which should be in the panel group. |

193 194 195
**Panel (`panels`) properties:**

| Property | Type | Required | Description |
196
| ------ | ------ | ------ | ------- |
197
| `type` | enum | no, defaults to `area-chart` | Specifies the chart type to use. |
198 199 200 201 202 203 204 205
| `title` | string | yes | Heading for the panel. |
| `y_label` | string | no, but highly encouraged | Y-Axis label for the panel. |
| `weight` | number | no, defaults to order in file | Order to appear within the grouping. Lower number means higher priority, which will be higher on the page. Numbers do not need to be consecutive. |
| `metrics` | array | yes | The metrics which should be displayed in the panel. |

**Metrics (`metrics`) properties:**

| Property | Type | Required | Description |
206
| ------ | ------ | ------ | ------ |
207 208 209 210 211
| `id` | string | no | Used for associating dashboard metrics with database records. Must be unique across dashboard configuration files. Required for [alerting](#setting-up-alerts-for-prometheus-metrics-ultimate) (support not yet enabled, see [relevant issue](https://gitlab.com/gitlab-org/gitlab-ce/issues/60319)). |
| `unit` | string | yes | Defines the unit of the query's return data. |
| `label` | string | no, but highly encouraged | Defines the legend-label for the query. Should be unique within the panel's metrics. |
| `query` | string | yes if `query_range` is not defined | Defines the Prometheus query to be used to populate the chart/panel. If defined, the `query` endpoint of the [Prometheus API](https://prometheus.io/docs/prometheus/latest/querying/api/) will be utilized. |
| `query_range` | string | yes if `query` is not defined | Defines the Prometheus query to be used to populate the chart/panel. If defined, the `query_range` endpoint of the [Prometheus API](https://prometheus.io/docs/prometheus/latest/querying/api/) will be utilized. |
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
#### Panel types for dashboards

The below panel types are supported in monitoring dashboards.

##### Area

To add an area panel type to a dashboard, look at the following sample dashboard file:

```yaml
dashboard: 'Dashboard Title'
panel_groups:
  - group: 'Group Title'
    panels:
      - type: area-chart
        title: "Chart Title"
        y_label: "Y-Axis"
        metrics:
          - id: 10
            query_range: 'http_requests_total'
            label: "Metric of Ages"
            unit: "count"
```

Note the following properties:

| Property | Type | Required | Description |
| ------ | ------ | ------ | ------ |
| type | string | no | Type of panel to be rendered. Optional for area panel types |
| query_range | yes | required | For area panel types, you must use a [range query](https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries) |

![area panel type](img/prometheus_dashboard_area_panel_type.png)

##### Single Stat

To add a single stat panel type to a dashboard, look at the following sample dashboard file:

```yaml
dashboard: 'Dashboard Title'
panel_groups:
  - group: 'Group Title'
    panels:
      - title: "Single Stat"
        type: "single-stat"
        metrics:
        - id: 10
          query: 'max(go_memstats_alloc_bytes{job="prometheus"})'
          unit: MB
          label: "Total"
```

Note the following properties:

| Property | Type | Required | Description |
| ------ | ------ | ------ | ------ |
| type | string | yes | Type of panel to be rendered. For single stat panel types, set to `single-stat` |
| query | string | yes | For single stat panel types, you must use an [instant query](https://prometheus.io/docs/prometheus/latest/querying/api/#instant-queries) |

![single stat panel type](img/prometheus_dashboard_single_stat_panel_type.png)

272
### Setting up alerts for Prometheus metrics **(ULTIMATE)**
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

#### Managed Prometheus instances

> [Introduced](https://gitlab.com/gitlab-org/gitlab-ee/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).

For managed Prometheus instances using auto configuration, alerts for metrics [can be configured](#adding-additional-metrics-premium) directly in the performance dashboard.

To set an alert, click on the alarm icon in the top right corner of the metric you want to create the alert for. A dropdown
will appear, with options to set the threshold and operator. Click **Add** to save and activate the alert.

![Adding an alert](img/prometheus_alert.png)

To remove the alert, click back on the alert icon for the desired metric, and click **Delete**.

#### External Prometheus instances

> [Introduced](https://gitlab.com/gitlab-org/gitlab-ee/issues/9258) in [GitLab Ultimate](https://about.gitlab.com/pricing/) 11.8.

For manually configured Prometheus servers, a notify endpoint is provided to use with Prometheus webhooks. If you have manual configuration enabled, an **Alerts** section is added to **Settings > Integrations > Prometheus**. This contains the *URL* and *Authorization Key*. The **Reset Key** button will invalidate the key and generate a new one.

![Prometheus service configuration of Alerts](img/prometheus_service_alerts.png)

To send GitLab alert notifications, copy the *URL* and *Authorization Key* into the [`webhook_configs`](https://prometheus.io/docs/alerting/configuration/#webhook_config) section of your Prometheus Alertmanager configuration:

```yaml
receivers:
  name: gitlab
  webhook_configs:
  - http_config:
      bearer_token: 9e1cbfcd546896a9ea8be557caf13a76
    send_resolved: true
    url: http://192.168.178.31:3001/root/manual_prometheus/prometheus/alerts/notify.json
  ...
```

308
### Taking action on incidents **(ULTIMATE)**
309 310 311

> [Introduced](https://gitlab.com/gitlab-org/gitlab-ee/issues/4925) in [GitLab Ultimate](https://about.gitlab.com/pricing/) 11.11.

312
Alerts can be used to trigger actions, like open an issue automatically (enabled by default since `12.1`). To configure the actions:
313 314 315 316 317 318 319

1. Navigate to your project's **Settings > Operations > Incidents**.
1. Enable the option to create issues.
1. Choose the [issue template](../description_templates.md) to create the issue from.
1. Optionally, select whether to send an email notification to the developers of the project.
1. Click **Save changes**.

320 321 322 323 324 325 326 327 328
Once enabled, an issue will be opened automatically when an alert is triggered which contains values extracted from [alert's payload](https://prometheus.io/docs/alerting/configuration/#webhook_config
):

- Issue author: `GitLab Alert Bot`
- Issue title: Extract from `annotations/title`, `annotations/summary` or `labels/alertname`
- Alert `Summary`: A list of properties
  - `starts_at`: Alert start time via `startsAt`
  - `full_query`: Alert query extracted from `generatorURL`
  - Optional list of attached annotations extracted from `annotations/*`
329
- Alert [GFM](../../markdown.md): GitLab Flavored Markdown from `annotations/gitlab_incident_markdown`
330

331
To further customize the issue, you can add labels, mentions, or any other supported [quick action](../quick_actions.md) in the selected issue template, which will apply to all incidents. To limit quick actions or other information to only specific types of alerts, use the `annotations/gitlab_incident_markdown` field.
332

333 334
Since [version 12.2](https://gitlab.com/gitlab-org/gitlab-ce/issues/63373), GitLab will tag each incident issue with the `incident` label automatically. If the label does not yet exist, it will be created automatically as well.

335 336
If the metric exceeds the threshold of the alert for over 5 minutes, an email will be sent to all [Maintainers and Owners](../../permissions.md#project-members-permissions) of the project.

337
## Determining the performance impact of a merge
Fatih Acet's avatar
Fatih Acet committed
338

339
> [Introduced][ce-10408] in GitLab 9.2.
340
> GitLab 9.3 added the [numeric comparison](https://gitlab.com/gitlab-org/gitlab-ce/issues/27439) of the 30 minute averages.
341
> Requires [Kubernetes](prometheus_library/kubernetes.md) metrics
Fatih Acet's avatar
Fatih Acet committed
342

Joshua Lambert's avatar
Joshua Lambert committed
343
Developers can view the performance impact of their changes within the merge
344
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
345
indicates when the current changes were deployed, with up to 30 minutes of
346
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
347
each commit has been deployed.
Fatih Acet's avatar
Fatih Acet committed
348

349
Once merged and the target branch has been redeployed, the metrics will switch
350
to show the new environments this revision has been deployed to.
351

352 353
Performance data will be available for the duration it is persisted on the
Prometheus server.
354 355

![Merge Request with Performance Impact](img/merge_request_performance.png)
Fatih Acet's avatar
Fatih Acet committed
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
## Embedding metric charts within Gitlab Flavored Markdown

> [Introduced][ce-29691] in GitLab 12.2.
> Requires [Kubernetes](prometheus_library/kubernetes.md) metrics.

It is possible to display metrics charts within [GitLab Flavored Markdown](../../markdown.md#gitlab-flavored-markdown-gfm).

To display a metric chart, include a link of the form `https://<root_url>/<project>/environments/<environment_id>/metrics`.

The following requirements must be met for the metric to unfurl:

- The `<environment_id>` must correspond to a real environment.
- Prometheus must be monitoring the environment.
- The GitLab instance must be configured to receive data from the environment.
- The user must be allowed access to the monitoring dashboard for the environment ([Reporter or higher](../../permissions.md)).
- The dashboard must have data within the last 8 hours.

 If all of the above are true, then the metric will unfurl as seen below:

![Embedded Metrics](img/embed_metrics.png)

378 379
## Troubleshooting

Joshua Lambert's avatar
Joshua Lambert committed
380
If the "No data found" screen continues to appear, it could be due to:
381

382
- No successful deployments have occurred to this environment.
383 384
- 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
385
  [run a query](prometheus_library/kubernetes.html#metrics-supported), replacing `$CI_ENVIRONMENT_SLUG`
386
  with the name of your environment.
387

388
[autodeploy]: ../../../ci/autodeploy/index.md
389
[kubernetes]: https://kubernetes.io
390
[kube]: ./kubernetes.md
391
[prometheus-k8s-sd]: https://prometheus.io/docs/operating/configuration/#<kubernetes_sd_config>
392 393
[prometheus]: https://prometheus.io
[gitlab-prometheus-k8s-monitor]: ../../../administration/monitoring/prometheus/index.md#configuring-prometheus-to-monitor-kubernetes
394 395 396
[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
397
[ci-environment-slug]: ../../../ci/variables/#predefined-environment-variables
398
[ce-8935]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/8935
Fatih Acet's avatar
Fatih Acet committed
399
[ce-10408]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/10408
400
[ce-29691]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/29691
401
[promgldocs]: ../../../administration/monitoring/prometheus/index.md