Skip to content

Commit bc4d19a

Browse files
Document how to use labels (#101)
Co-authored-by: hamzahrmalik <[email protected]>
1 parent b7ed98c commit bc4d19a

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

Sources/Prometheus/Docs.docc/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ values in the Prometheus text format.
99

1010
``Prometheus`` integrates with [Swift Metrics](doc:swift-metrics).
1111

12+
For general advice on how to use `Prometheus` make sure to also read the [Prometheus documentation][prometheus-docs].
13+
1214
## Installation
1315

1416
``Prometheus`` is available through Swift Package Manager. To include it in your project add the
@@ -68,6 +70,7 @@ print(String(decoding: buffer, as: Unicode.UTF8.self))
6870
### Getting started
6971

7072
- <doc:swift-metrics>
73+
- <doc:labels>
7174
- ``PrometheusCollectorRegistry``
7275
- ``PrometheusMetricsFactory``
7376

@@ -79,3 +82,5 @@ print(String(decoding: buffer, as: Unicode.UTF8.self))
7982
- ``Histogram``
8083
- ``DurationHistogram``
8184
- ``ValueHistogram``
85+
86+
[prometheus-docs]: https://prometheus.io/docs/introduction/overview/
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Sources/Prometheus/Docs.docc/swift-metrics.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Usage with SwiftMetrics
1+
# Emit metrics collected by Swift Metrics
22

3-
In this article you will learn how Swift Prometheus integrates with Swift Metrics and how you can
4-
use both libraries together.
3+
Learn how Swift Prometheus integrates with Swift Metrics – an abstract API that is widely used in
4+
the swift-server ecosystem.
55

66
## Overview
77

@@ -102,6 +102,11 @@ Metrics.Counter(label: "my_counter") // will show up in Prometheus exports as `c
102102
This can be particularly usefull, if you want to change the names and labels for metrics that are
103103
generated in a third party library.
104104

105+
> Important: Please note, that all Prometheus metrics with the same name, **must** use the same
106+
> label names.
107+
> Use the ``PrometheusMetricsFactory/nameAndLabelSanitizer`` to ensure this remains true metrics
108+
> that are created in third party libraries. See <doc:labels> for more information about this.
109+
105110
### Defining Buckets for Histograms
106111

107112
#### Default buckets

0 commit comments

Comments
 (0)