README.md
1# thanos-mixin
2
3> Note that everything is experimental and may change significantly at any time.
4> Also it still has missing alert and dashboard definitions for certain components, e.g. rule and sidecar. Please feel free to contribute.
5
6This directory contains extensible and customizable monitoring definitons for Thanos. [Grafana](http://grafana.com/) dashboards, and [Prometheus rules](https://prometheus.io/docs/prometheus/latest/configuration/recording_rules/) combined with documentation and scripts to provide easy monitoring experience for Thanos.
7
8You can find more about monitoring-mixins in [the design document](https://docs.google.com/document/d/1A9xvzwqnFVSOZ5fD3blKODXfsat5fg6ZhnKu9LK3lB4/edit#heading=h.gt9r2h2gklj3), and you could check out other examples like [Prometheus Mixin](https://github.com/prometheus/prometheus/tree/master/documentation/prometheus-mixin).
9
10The content of this project is written in [jsonnet](http://jsonnet.org/). This project could both be described as a package as well as a library.
11
12## Requirements
13
14### jsonnet
15
16The content of this project consists of a set of [jsonnet](http://jsonnet.org/) files making up a library to be consumed.
17
18We recommend to use [go-jsonnet](https://github.com/google/go-jsonnet). It's an implementation of [Jsonnet](http://jsonnet.org/) in pure Go. It is feature complete but is not as heavily exercised as the [Jsonnet C++ implementation](https://github.com/google/jsonnet).
19
20To install:
21
22```shell
23go get github.com/google/go-jsonnet/cmd/jsonnet
24```
25
26### jsonnet-bundler
27
28`thanos-mixin` uses [jsonnet-bundler](https://github.com/jsonnet-bundler/jsonnet-bundler#install) (the jsonnet package manager) to manage its dependencies.
29
30We also recommend you to use `jsonnet-bundler` to install or update if you decide to use `thanos-mixin` as a dependency for your custom configurations.
31
32To install:
33
34```shell
35go get github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb
36```
37
38> An e.g. of how to install a given version of this library: `jb install github.com/thanos-io/thanos/mixin@main`.
39
40## Use as a library
41
42To use the `thanos-mixin` as a dependency, simply use the `jsonnet-bundler` install functionality:
43```shell
44$ mkdir thanos-mixin; cd thanos-mixin
45$ jb init # Creates the initial/empty `jsonnetfile.json`
46# Install the thanos-mixin dependency
47$ jb install github.com/thanos-io/thanos/mixin@main # Creates `vendor/` & `jsonnetfile.lock.json`, and fills in `jsonnetfile.json`
48```
49
50To update the `thanos-mixin` as a dependency, simply use the `jsonnet-bundler` update functionality:
51```shell
52$ jb update
53```
54
55#### Configure
56
57This project is intended to be used as a library. You can extend and customize dashboards and alerting rules by creating for own generators, such as the generators ([alerts.jsonnet](alerts.jsonnet) and [dashboards.jsonnet](dashboards.jsonnet)) that are use to create [examples](/examples). Default parameters are collected in [config.libsonnet](config.libsonnet), feel free to modify and generate your own definitons.
58
59[embedmd]:# (config.libsonnet)
60```libsonnet
61{
62 local thanos = self,
63 // TargetGroups is a way to help mixin users to add high level target grouping to their alerts and dashboards.
64 // With the help of TargetGroups you can use a single observability stack to monitor several Thanos instances.
65 // The key in the key-value pair will be used as "label name" in the alerts and variable name in the dashboards.
66 // The value in the key-value pair will be used as a query to fetch available values for the given label name.
67 targetGroups+:: {
68 // For example for given following groups,
69 // namespace: 'thanos_status',
70 // cluster: 'find_mi_cluster_bitte',
71 // zone: 'an_i_in_da_zone',
72 // region: 'losing_my_region',
73 // will generate queriers for the alerts as follows:
74 // (
75 // sum by (cluster, namespace, region, zone, job) (rate(thanos_compact_group_compactions_failures_total{job=~"thanos-compact.*"}[5m]))
76 // /
77 // sum by (cluster, namespace, region, zone, job) (rate(thanos_compact_group_compactions_total{job=~"thanos-compact.*"}[5m]))
78 // * 100 > 5
79 // )
80 //
81 // AND for the dashborads:
82 //
83 // sum by (cluster, namespace, region, zone, job) (rate(thanos_compact_group_compactions_failures_total{cluster=\"$cluster\", namespace=\"$namespace\", region=\"$region\", zone=\"$zone\", job=\"$job\"}[$interval]))
84 // /
85 // sum by (cluster, namespace, region, zone, job) (rate(thanos_compact_group_compactions_total{cluster=\"$cluster\", namespace=\"$namespace\", region=\"$region\", zone=\"$zone\", job=\"$job\"}[$interval]))
86 },
87 query+:: {
88 selector: 'job=~".*thanos-query.*"',
89 title: '%(prefix)sQuery' % $.dashboard.prefix,
90 },
91 store+:: {
92 selector: 'job=~".*thanos-store.*"',
93 title: '%(prefix)sStore' % $.dashboard.prefix,
94 },
95 receive+:: {
96 selector: 'job=~".*thanos-receive.*"',
97 title: '%(prefix)sReceive' % $.dashboard.prefix,
98 },
99 rule+:: {
100 selector: 'job=~".*thanos-rule.*"',
101 title: '%(prefix)sRule' % $.dashboard.prefix,
102 },
103 compact+:: {
104 selector: 'job=~".*thanos-compact.*"',
105 title: '%(prefix)sCompact' % $.dashboard.prefix,
106 },
107 sidecar+:: {
108 selector: 'job=~".*thanos-sidecar.*"',
109 title: '%(prefix)sSidecar' % $.dashboard.prefix,
110 },
111 // TODO(kakkoyun): Fix naming convention: bucketReplicate
112 bucket_replicate+:: {
113 selector: 'job=~".*thanos-bucket-replicate.*"',
114 title: '%(prefix)sBucketReplicate' % $.dashboard.prefix,
115 },
116 dashboard+:: {
117 prefix: 'Thanos / ',
118 tags: ['thanos-mixin'],
119 timezone: 'UTC',
120 selector: ['%s="$%s"' % [level, level] for level in std.objectFields(thanos.targetGroups)],
121 dimensions: ['%s' % level for level in std.objectFields(thanos.targetGroups)],
122
123 overview+:: {
124 title: '%(prefix)sOverview' % $.dashboard.prefix,
125 selector: std.join(', ', thanos.dashboard.selector),
126 dimensions: std.join(', ', thanos.dashboard.dimensions + ['job']),
127 },
128 },
129}
130```
131
132You can format your code using:
133```shell
134$ make jsonnet-format
135```
136
137## Examples
138
139This project is intended to be used as a library. However, it also provides drop-in examples to monitor Thanos.
140
141### Requirements
142
143Although all the required dependencies are handled by `Makefile`, keep in mind that in addition the dependencies that are listed above we have following dependencies:
144
145#### gojsontoyaml
146
147`gojsontoyaml` is used to convert generated `json` definitons to `yaml`.
148
149To install:
150```shell
151go get github.com/brancz/gojsontoyaml
152```
153
154### Generate
155
156To generate examples after modifying, make sure `jsonnet` dependencies are installed.
157```shell
158$ make jsonnet-vendor
159```
160
161and then
162
163```shell
164$ make examples
165```
166
167Make action runs the jsonnet code, then reads each key of the generated json and uses that as the file name, and writes the value of that key to that file, and converts each json manifest to yaml.
168
169> Make commands should handle dependecies for you.
170
171### Test and validate
172
173You validate your structural correctness of your Prometheus [alerting rules](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/) or [recording rules](https://prometheus.io/docs/prometheus/latest/configuration/recording_rules/) with:
174
175```shell
176$ make example-rules-lint
177```
178
179Check out [test.yaml](/examples/alerts/tests.yaml) to add/modify tests for the mixin. To learn more about how to write test for Prometheus, check out [official documentation](https://www.prometheus.io/docs/prometheus/latest/configuration/unit_testing_rules/).
180
181You test alerts with:
182
183```shell
184$ make alerts-test
185```
186
187---
188