@@ -20,6 +20,52 @@ A fully working use-case and example of library usage is available in the
20
20
There is a one-time setup phase to prime the code for autometrics. Once this
21
21
phase is accomplished, only calling ` go generate ` is necessary.
22
22
23
+ ### Install the go generator.
24
+
25
+ The generator is the binary in cmd/autometrics, so the easiest way to get it is
26
+ to install it through go:
27
+
28
+ ``` console
29
+ go install github.com/autometrics-dev/autometrics-go/cmd/autometrics@latest
30
+ ```
31
+
32
+ In order to have ` autometrics ` visible then, make sure that the directory
33
+ ` $GOBIN ` (or the default ` $GOPATH/bin ` ) is in your ` $PATH ` :
34
+
35
+ ``` console
36
+ $ echo " $PATH " | grep -q " ${GOBIN:- $GOPATH / bin} " && echo " GOBIN in PATH" || echo " GOBIN not in PATH, please add it"
37
+ GOBIN in PATH
38
+ ```
39
+
40
+ ### Import the libraries and initialize the metrics
41
+
42
+ In the main entrypoint of your program, you need to both add package
43
+
44
+ ``` go
45
+ import (
46
+ am " github.com/autometrics-dev/autometrics-go/pkg/autometrics/prometheus"
47
+ )
48
+ ```
49
+
50
+ And then in your main function initialize the metrics
51
+
52
+ ``` go
53
+ am.Init (nil , am.DefBuckets )
54
+ ```
55
+
56
+ > ** Warning**
57
+ > If you want to enable alerting from Autometrics, you ** MUST**
58
+ have the ` --latency-ms ` values to match the values given in your buckets. The
59
+ values in the buckets are given in _ seconds_ . By default, the generator will
60
+ error and tell you the valid default values if they don't match.
61
+ If the default values do not match you use case, you can change the buckets in
62
+ the init call, and add a ` -custom-latency ` argument to the ` //go:generate ` invocation.
63
+
64
+ ``` patch
65
+ - //go:generate autometrics
66
+ + //go:generate autometrics -custom-latency
67
+ ```
68
+
23
69
### Add cookies in your code
24
70
25
71
Given a starting function like:
@@ -34,7 +80,6 @@ func RouteHandler(args interface{}) error {
34
80
The manual changes you need to do are:
35
81
36
82
``` go
37
- // Somewhere in your file, probably at the bottom
38
83
// go:generate autometrics
39
84
40
85
// autometrics:doc
@@ -50,6 +95,12 @@ value you return for function you want to instrument.
50
95
51
96
### Generate the documentation and instrumentation code
52
97
98
+ Install the go generator using ` go install ` as usual:
99
+
100
+ ``` console
101
+ go install https://github.com/autometrics-dev/autometrics-go/cmd/autometrics
102
+ ```
103
+
53
104
Once you've done this, the ` autometrics ` generator takes care of the rest, and you can
54
105
simply call ` go generate ` with an optional environment variable:
55
106
@@ -77,13 +128,13 @@ For Prometheus the shortest way is to add the handler code in your main entrypoi
77
128
78
129
``` go
79
130
import (
80
- " github.com/autometrics-dev/autometrics-go/pkg/autometrics"
131
+ am " github.com/autometrics-dev/autometrics-go/pkg/autometrics/prometheus "
81
132
" github.com/prometheus/client_golang/prometheus/promhttp"
82
133
)
83
134
84
135
85
136
func main () {
86
- autometrics .Init (nil , autometrics .DefBuckets )
137
+ am .Init (nil , am .DefBuckets )
87
138
http.Handle (" /metrics" , promhttp.Handler ())
88
139
}
89
140
```
@@ -114,6 +165,52 @@ The valid arguments for alert generation are:
114
165
- ` --latency-target ` : latency target for the threshold, between 0 and 100 (so X%
115
166
of calls must last less than ` latency-ms ` milliseconds). You must specify both
116
167
latency options, or none.
168
+
169
+ > ** Warning**
170
+ > The generator will error out if you use targets that are not
171
+ supported by the bundled [ Alerting rules file] ( ./configs/autometrics.rules.yml ) .
172
+ Support for custom target is planned but not present at the moment
173
+
174
+ ## (OPTIONAL) OpenTelemetry Support
175
+
176
+ Autometrics supports using OpenTelemetry with a prometheus exporter instead of using
177
+ Prometheus to publish the metrics. The changes you need to make are:
178
+
179
+ - change where the ` amImpl ` import points to
180
+ ``` patch
181
+ import (
182
+ - am "github.com/autometrics-dev/autometrics-go/pkg/autometrics/prometheus"
183
+ + am "github.com/autometrics-dev/autometrics-go/pkg/autometrics/otel"
184
+ )
185
+ ```
186
+ - change the call to ` amImpl.Init ` to the new signature: instead of a registry,
187
+ the ` Init ` function takes a meter name for the ` otel_scope ` label of the exported
188
+ metric. You can use the name of the application or its version for example
189
+
190
+ ``` patch
191
+ - am.Init(nil, am.DefBuckets)
192
+ + am.Init("myApp/v2/prod", am.DefBuckets)
193
+ ```
194
+
195
+ - add the ` -otel ` flag to the ` //go:generate ` directive
196
+
197
+ ``` patch
198
+ - //go:generate autometrics
199
+ + //go:generate autometrics -otel
200
+ ```
201
+
202
+ ## (OPTIONAL) Git hook
203
+
204
+ As autometrics is a Go generator that modifies the source code when run, it
205
+ might be interesting to set up ` go generate ./... ` to run in a git pre-commit
206
+ hook so that you never forget to run it if you change the source code.
207
+
208
+ If you use a tool like [ pre-commit] ( https://pre-commit.com/ ) , see their
209
+ documentation about how to add a hook that will run ` go generate ./... ` .
210
+
211
+ Otherwise, a simple example has been added in the [ configs folder] ( ./configs/pre-commit )
212
+ as an example. You can copy this file in your copy of your project's repository, within
213
+ ` .git/hooks ` and make sure that the file is executable.
117
214
118
215
## Status
119
216
@@ -125,8 +222,10 @@ the current status
125
222
The first version of the library has _ not_ been written by Go experts. Any comment or
126
223
code suggestion as Pull Request is more than welcome!
127
224
128
- ### Metrics system
225
+ ### Support for custom alerting rules generation
129
226
130
- For the time being only Prometheus metrics are supported, but the code has been
131
- written with the possibility to have other systems, like OpenTelemetry,
132
- integrated in the same way.
227
+ The alerting system for SLOs that Autometrics uses is based on
228
+ [ Sloth] ( https://github.com/slok/sloth ) , and it has native Go types for
229
+ marshalling/unmarshalling rules, so it should be possible to provide an extra
230
+ binary in this repository, that only takes care of generating a new [ rules
231
+ file] ( ./configs/autometrics.rules.yml ) with custom objectives.
0 commit comments