|
| 1 | +# Use Labels in Swift Prometheus |
| 2 | + |
| 3 | +Learn how to use Labels in Prometheus, the benefits of doing so, and how to avoid common mistakes. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Prometheus Collectors have a name and they may have labels. Labels specify the metrics further. For |
| 8 | +example you might have a ``Counter`` with the name `http_responses_total`. You may now add a label |
| 9 | +`code` that specifies the http status response code. This way you are able to query how many http |
| 10 | +responses were sent. But you can also filter this by response code. |
| 11 | + |
| 12 | +Read more about the benefits of using Labels in the [Prometheus best practices documentation][prometheus-use-labels]. |
| 13 | + |
| 14 | +> Note: The naming between Prometheus and Swift-Metrics is a bit confusing. Swift Metrics calls a |
| 15 | +> metric's name its label and they call a metric's labels dimensions. In this article, when we |
| 16 | +> refer to labels, we mean the additional properties that can be added to a metrics name. |
| 17 | +> |
| 18 | +> | Framework | Metric name | Additional infos | |
| 19 | +> |---------------|-------------|------------------| |
| 20 | +> | swift-metrics | `label` | `dimensions` | |
| 21 | +> | Prometheus | `name` | `labels` | |
| 22 | +
|
| 23 | +Please be aware that the ``PrometheusCollectorRegistry`` will create a seperate metric for each |
| 24 | +unique label pair, even though the metric name might be the same. This means that in the example |
| 25 | +below, we will have two independent metrics: |
| 26 | + |
| 27 | +```swift |
| 28 | +let counter200 = registry.makeCounter(name: "http_responses_total", labels: ["code": "200"]) |
| 29 | +let counter400 = registry.makeCounter(name: "http_responses_total", labels: ["code": "400"]) |
| 30 | + |
| 31 | +// handling response code |
| 32 | +swift responseCode { |
| 33 | +case .ok: |
| 34 | + counter200.increment() |
| 35 | +case .badRequest: |
| 36 | + counter400.increment() |
| 37 | +default: |
| 38 | + break |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +> Important: Please note, that all metrics with the same name, **must** use the same label names. |
| 43 | +
|
| 44 | +Prometheus requires that for the same metric name all labels names must be the same. Swift |
| 45 | +Prometheus enforces this by crashing if the label names or the metric type does not match a |
| 46 | +previously registered metric with the same name. |
| 47 | + |
| 48 | +#### Examples: |
| 49 | + |
| 50 | +The example below crashes as we try to create a ``Counter`` named `"http_responses_total"` with a |
| 51 | +label `"code"` after a ``Counter`` with the same name without labels was created earlier. |
| 52 | + |
| 53 | +```swift |
| 54 | +let counter = registry.makeCounter(name: "http_responses_total") |
| 55 | +let counter200 = registry.makeCounter( // 💥 crash |
| 56 | + name: "http_responses_total", |
| 57 | + labels: ["code": "200"] |
| 58 | +) |
| 59 | +``` |
| 60 | + |
| 61 | +The example below crashes as we try to create a ``Counter`` named `"http_responses_total"` with a |
| 62 | +label `"version"` after a ``Counter`` with the same name but different label name `"code"` was |
| 63 | +created earlier. |
| 64 | + |
| 65 | +```swift |
| 66 | +let counter200 = registry.makeCounter( |
| 67 | + name: "http_responses_total", |
| 68 | + labels: ["code": "200"] |
| 69 | +) |
| 70 | +let counterHTTPVersion1 = registry.makeCounter( // 💥 crash |
| 71 | + name: "http_responses_total", |
| 72 | + labels: ["version": "1.1"] |
| 73 | +) |
| 74 | +``` |
| 75 | + |
| 76 | +The example below crashes as we try to create a ``Gauge`` named `http_responses_total` with the |
| 77 | +same name as a previously created ``Counter``. |
| 78 | + |
| 79 | +```swift |
| 80 | +let counter = registry.makeCounter(name: "http_responses_total") |
| 81 | +let gauge = registry.makeGauge(name: "http_responses_total") // 💥 crash |
| 82 | +``` |
| 83 | + |
| 84 | +[prometheus-use-labels]: https://prometheus.io/docs/practices/instrumentation/#use-labels |
0 commit comments