You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Documentation updates for the upcoming v1.0 launch (#49)
* move k8s up
* update golang to include otel instructions
* update python to include otel instructions
* tweak rust things
* Update Golang documentation for 1.0 release
---------
Co-authored-by: Gerry Agbobada <gerry@fiberplane.com>
To add version information to your metrics, simply fill the `autometrics.BuildInfo` parameter to the `autometrics.Init()` call:
17
+
To add version information to your metrics, simply add the relevant option parameters to the `autometrics.Init()` call:
18
18
19
19
```go {4-8} filename="main.go"
20
20
autometrics.Init(
21
-
nil,
22
-
autometrics.DefBuckets,
23
-
autometrics.BuildInfo{
24
-
Version: "<version_here>",
25
-
Commit: "<git_commit_hash_here",
26
-
Branch: "<git_branch_here>",
27
-
},
28
-
nil,
21
+
autometrics.WithService("<service_name_here>"),
22
+
autometrics.WithVersion("<version_here>"),
23
+
autometrics.WithCommit("<git_commit_hash_here>"),
24
+
autometrics.WithBranch("<git_branch_here>"),
29
25
)
30
26
```
31
27
32
-
`autometrics.BuildInfo` parameter accepts each fields as strings which you can set as you wish or use something like `ldflags` to set them at build time.
28
+
All `With...` initialization settings are optional, and not setting them will leave the label empty like `repository_url` and `repository_provider` in the example above.
29
+
30
+
`autometrics.With...` options accept most fields as strings which you can set as you wish or use something like `ldflags` to set them at build time. Check the documentation on pkg.go.dev (for [Prometheus version](https://pkg.go.dev/github.com/autometrics-dev/autometrics-go/prometheus/autometrics) or [Open Telemetry version](https://pkg.go.dev/github.com/autometrics-dev/autometrics-go/otel/autometrics)) for more details about the initialization settings (all functions returning `autometrics.Option`).
33
31
34
32
## Using `ldflags` to add version information at build time
And then in your main function initialize the metrics:
137
148
138
-
```yaml filename="prometheus.yml"
139
-
scrape_configs:
140
-
- job_name: my-app
141
-
metrics_path: /metrics
142
-
static_configs:
143
-
# Replace the port with the port your app listens on
144
-
- targets: ['<host>:<port>']
145
-
# For a real deployment, you would want the scrape interval to be
146
-
# longer but for testing, you want the data to show up quickly
147
-
scrape_interval: 200ms
149
+
```go filename="main.go"
150
+
autometrics.Init(
151
+
autometrics.WithService("myApp"),
152
+
autometrics.WithMeterName("myApp/v2/prod"),
153
+
autometrics.WithCommit("anySHA"),
154
+
autometrics.WithVersion("2.1.37"),
155
+
)
148
156
```
157
+
158
+
### Add `go:generate autometrics --otel` to your code
159
+
160
+
The manual changes you need to do are:
161
+
162
+
```go filename="main.go"
163
+
//go:generate autometrics --otel
164
+
165
+
//autometrics:doc
166
+
funcRouteHandler(argsinterface{}) (errerror) { // Name the error return value; this is an optional but recommended change
167
+
// ...
168
+
returnnil
169
+
}
170
+
```
171
+
172
+
<Callout>
173
+
If you want the generated metrics to contain the function success rate, you must name the error return value. This is why we recommend to name the error value you return for the function you want to instrument.
174
+
</Callout>
175
+
176
+
### Generate the documentation and instrumentation code
177
+
178
+
Install the go generator using `go install` as usual:
179
+
180
+
```bash filename="Shell"
181
+
go install https://github.com/autometrics-dev/autometrics-go/cmd/autometrics@latest
182
+
```
183
+
184
+
Once you've done this, the `autometrics` generator takes care of the rest, and you can
185
+
simply call `go generate` with an optional environment variable:
186
+
187
+
```bash filename="Shell"
188
+
AM_PROMETHEUS_URL=http://localhost:9090/ go generate ./...
189
+
```
190
+
191
+
The generator will augment your doc comment to add quick links to metrics (using the Prometheus URL as base URL), and add a unique defer statement that will take care of instrumenting your code.
192
+
193
+
The environment variable `AM_PROMETHEUS_URL` controls the base URL of the instance that is scraping the deployed version of your code. Having an environment variable means you can change the generated links without touching your code. Default value: `http://localhost:9090/`.
194
+
195
+
<Callout>
196
+
If you do not want to have lengthy documentation generated on top of the function, you can use `//autometrics:inst` annotation instead of `//autometrics:doc`.
197
+
</Callout>
198
+
199
+
### Push metrics to the OpenTelemetry collector
200
+
201
+
If you have an OTLP collector setup with an accessible URL, then you can directly push your metrics to it by passing the relevant `WithPush...` options to `autometrics.Init`:
Autometrics supports exporting metrics to Prometheus via the Prometheus Python client. This example uses the Prometheus Python client, available settings are same as the Prometheus Python client. By default, the Prometheus exporter will expose metrics on port 9464.
34
+
35
+
```python
36
+
from autometrics import autometrics, init
37
+
38
+
init(
39
+
tracker="prometheus",
40
+
exporter={
41
+
"type": "prometheus",
42
+
"port": 9464,
43
+
},
44
+
service_name="my-service",
45
+
)
46
+
```
47
+
</Tab>
48
+
49
+
<Tab>
50
+
51
+
Autometrics supports exporting metrics to OTLP collectors via gRPC and HTTP transports. This example uses the HTTP transport, available settings are similar to the OpenTelemetry Python SDK. By default, the OTLP exporter will send metrics to `localhost:4318`.
Copy file name to clipboardExpand all lines: src/pages/rust/quickstart.mdx
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,15 @@ import PreviewMetricsInExplorerSnippet from "@/snippets/preview-in-am-explorer.m
8
8
<Steps>
9
9
### Add Autometrics
10
10
11
+
Add the `autometrics` crate dependency using `cargo`:
12
+
11
13
```bash copy
12
14
cargo add autometrics
13
15
```
14
16
15
17
### Add the Autometrics macro
16
18
17
-
Add the `#[autometrics]` to the functions you want to instrument with metrics
19
+
Add the `#[autometrics]`macro to the functions you want to instrument with metrics
18
20
19
21
```rust
20
22
useautometrics::autometrics;
@@ -27,7 +29,7 @@ pub async fn create_user() {
27
29
28
30
### Export the metrics
29
31
30
-
<Tabsitems={['Expose a endpoint for Prometheus', 'Push to a OpenTelemetry collector']}>
32
+
<Tabsitems={['Expose an endpoint for Prometheus', 'Push to a OpenTelemetry collector']}>
31
33
<Tab>
32
34
<Callouttype="info">
33
35
If you're already using Prometheus metrics in your app, you can skip this step. You only need to configure Autometrics to use the same underlying metrics collection library as your existing Prometheus metrics. See [Metrics Libraries](https://docs.rs/autometrics/latest/autometrics/#metrics-libraries) for more information in the API reference.
0 commit comments