Skip to content

Commit 162bf08

Browse files
laulaulandGerry Agbobada
andauthored
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>
1 parent 19fbebb commit 162bf08

5 files changed

Lines changed: 178 additions & 73 deletions

File tree

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
{
2-
"docker": "Docker",
3-
"local": "Local",
4-
"kubernetes": "Kubernetes",
5-
6-
"fly-io": "Fly.io",
7-
"northflank": "Northflank",
8-
"railway": "Railway"
9-
10-
2+
"docker": "Docker",
3+
"kubernetes": "Kubernetes",
4+
"local": "Local",
5+
"fly-io": "Fly.io",
6+
"northflank": "Northflank",
7+
"railway": "Railway"
118
}

src/pages/go/adding-version-information.mdx

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,25 @@ It produces a `build_info` metric and uses labels to expose the version, commit
99
Example:
1010

1111
```prometheus
12-
build_info{branch="main",commit="5cc2aa5447907fbf0a7ddd",version="1.0.0"} 1
12+
build_info{branch="main",commit="5cc2aa5447907fbf0a7ddd",version="1.0.0",service_name="Billing",repository_url="",repository_provider=""} 1
1313
```
1414

1515
## Usage
1616

17-
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:
1818

1919
```go {4-8} filename="main.go"
2020
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>"),
2925
)
3026
```
3127

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`).
3331

3432
## Using `ldflags` to add version information at build time
3533

@@ -49,14 +47,10 @@ var (
4947

5048
```go {4-8} filename="main.go"
5149
autometrics.Init(
52-
nil,
53-
autometrics.DefBuckets,
54-
autometrics.BuildInfo{
55-
Version: Version,
56-
Commit: Commit,
57-
Branch: Branch,
58-
},
59-
nil,
50+
autometrics.WithService("Billing"),
51+
autometrics.WithVersion(Version),
52+
autometrics.WithCommit(Commit),
53+
autometrics.WithBranch(Branch),
6054
)
6155
```
6256

src/pages/go/quickstart.mdx

Lines changed: 114 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@ There is a one-time setup phase to prime the code for autometrics. Once this pha
1313

1414
The generator is the binary in cmd/autometrics, so the easiest way to get it is to install it through Go:
1515

16-
```bash
16+
```bash filename="Shell"
1717
go install github.com/autometrics-dev/autometrics-go/cmd/autometrics@latest
1818
```
1919

2020
### Add the generator to your PATH
2121

22-
In order to have autometrics visible then, make sure that the directory $GOBIN (or the default $GOPATH/bin) is in your $PATH:
22+
In order to have autometrics visible then, make sure that the directory `$GOBIN` (or the default `$GOPATH/bin`) is in your `$PATH`:
2323

24-
```bash
24+
```bash filename="Shell"
2525
echo "$PATH" | grep -q "${GOBIN:-$GOPATH/bin}" && echo "GOBIN in PATH" || echo "GOBIN not in PATH, please add it"
2626
GOBIN in PATH
2727
```
2828

2929
## Instructions
3030

31+
<Tabs items={["Using Prometheus client","Using OpenTelemetry"]}>
32+
33+
<Tab>
34+
3135
<Steps>
3236

3337
### Import the library in your code
@@ -43,16 +47,25 @@ import (
4347
And then in your main function initialize the metrics:
4448

4549
```go filename="main.go"
46-
autometrics.Init(
47-
nil,
48-
autometrics.DefBuckets,
49-
autometrics.BuildInfo{},
50-
nil,
51-
)
50+
autometrics.Init()
5251
```
5352

5453
### Add `go:generate autometrics` to your code
5554

55+
#### Automatically
56+
57+
You can use the `am` binary and its `instrument` subcommand to automatically instrument your whole codebase. You can also use a grep invocation:
58+
59+
```bash filename="Shell"
60+
# On Linux, use sed, not gsed
61+
find . \
62+
-type d -name vendor -prune -or \
63+
-type f -name '*.go' \
64+
-print0 | xargs -0 gsed -i -e '/package/{a\//go:generate autometrics --inst-all --no-doc' -e ':a;n;ba}'
65+
```
66+
67+
#### Manually
68+
5669
The manual changes you need to do are:
5770

5871
```go filename="main.go"
@@ -71,13 +84,7 @@ If you want the generated metrics to contain the function success rate, you must
7184

7285
### Generate the documentation and instrumentation code
7386

74-
Install the go generator using `go install` as usual:
75-
76-
```bash filename="Shell"
77-
go install https://github.com/autometrics-dev/autometrics-go/cmd/autometrics@latest
78-
```
79-
80-
Once you've done this, the `autometrics` generator takes care of the rest, and you can
87+
As you have installed the generator as part of the prerequisites, it will take care of the rest, and you can
8188
simply call `go generate` with an optional environment variable:
8289

8390
```bash filename="Shell"
@@ -107,10 +114,10 @@ import (
107114

108115
func main() {
109116
autometrics.Init(
110-
"web-server",
111-
autometrics.DefBuckets,
112-
autometrics.BuildInfo{},
113-
nil,
117+
autometrics.WithService("web-server"),
118+
autometrics.WithMeterName("web-server"),
119+
autometrics.WithCommit("anySHA"),
120+
autometrics.WithVersion("2.1.37"),
114121
)
115122
http.Handle("/metrics", promhttp.Handler())
116123
}
@@ -121,28 +128,98 @@ This is the shortest way to initialize and expose the metrics that autometrics w
121128
You're set! You can now start your app and preview the metrics.
122129

123130
</Steps>
131+
</Tab>
124132

125-
## Viewing the metrics
133+
<Tab>
126134

127-
### Using Autometrics Explorer
128-
<PreviewMetricsInExplorerSnippet/>
135+
<Steps>
129136

130-
### Configure your Prometheus
137+
### Import the library in your code
131138

132-
Make sure your Prometheus is configured correctly to find the metrics.
139+
In the main entrypoint of your program, add the package:
133140

134-
See the [Configuring Prometheus](/deploying-prometheus) section for deploy target-specific instructions.
141+
```go filename="main.go"
142+
import (
143+
autometrics "github.com/autometrics-dev/autometrics-go/otel/autometrics"
144+
)
145+
```
135146

136-
Example configuration:
147+
And then in your main function initialize the metrics:
137148

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+
)
148156
```
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+
func RouteHandler(args interface{}) (err error) { // Name the error return value; this is an optional but recommended change
167+
// ...
168+
return nil
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`:
202+
203+
```go {6-11}
204+
shutdown, err := autometrics.Init(
205+
autometrics.WithService("myApp"),
206+
autometrics.WithMeterName("myApp/v2/prod"),
207+
autometrics.WithCommit("anySHA"),
208+
autometrics.WithVersion("2.1.37"),
209+
autometrics.WithPushCollectorURL("https://collector.example.com"),
210+
autometrics.WithPushJobName("instance_2"), // You can leave the JobName out to let autometrics generate one
211+
autometrics.WithPushPeriod(1 * time.Second),
212+
autometrics.WithPushTimeout(500 * time.Millisecond),
213+
)
214+
```
215+
216+
</Steps>
217+
</Tab>
218+
219+
</Tabs>
220+
221+
## Viewing the metrics
222+
223+
### Using Autometrics Explorer
224+
<PreviewMetricsInExplorerSnippet/>
225+

src/pages/python/quickstart.mdx

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,50 @@ def create_user(name):
2222
# ...
2323
```
2424

25-
### Set the metrics collection library (optional)
25+
### Configure metrics exporting
2626

27-
Use the environment variable `AUTOMETRICS_TRACKER` to set the metrics collection
28-
library. The default is `OPEN_TELEMETRY`.
27+
You can configure `autometrics` to export using either Prometheus or OpenTelemetry libraries by calling the `init` function:
2928

30-
Options:
31-
- `OPEN_TELEMETRY` - uses `opentelemetry` to collect and expose metrics
32-
- `PROMETHEUS` - uses `prometheus_client` to collect and expose metrics
29+
<Tabs items={["Using Prometheus client", "Using OpenTelemetry"]}>
3330

31+
<Tab>
32+
33+
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`.
52+
53+
```python
54+
from autometrics import autometrics, init
55+
56+
init(
57+
exporter={
58+
"type": "otlp-proto-http",
59+
"endpoint": "http://your-otlp-endpoint",
60+
"push_interval": 1000,
61+
},
62+
service_name="otlp-exporter",
63+
)
64+
```
65+
66+
</Tab>
67+
68+
</Tabs>
3469
</Steps>
3570

3671
## Viewing your metrics

src/pages/rust/quickstart.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import PreviewMetricsInExplorerSnippet from "@/snippets/preview-in-am-explorer.m
88
<Steps>
99
### Add Autometrics
1010

11+
Add the `autometrics` crate dependency using `cargo`:
12+
1113
```bash copy
1214
cargo add autometrics
1315
```
1416

1517
### Add the Autometrics macro
1618

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
1820

1921
```rust
2022
use autometrics::autometrics;
@@ -27,7 +29,7 @@ pub async fn create_user() {
2729

2830
### Export the metrics
2931

30-
<Tabs items={['Expose a endpoint for Prometheus', 'Push to a OpenTelemetry collector']}>
32+
<Tabs items={['Expose an endpoint for Prometheus', 'Push to a OpenTelemetry collector']}>
3133
<Tab>
3234
<Callout type="info">
3335
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

Comments
 (0)