File: usage.md

package info (click to toggle)
aiodogstatsd 0.16.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: python: 1,080; makefile: 70; sh: 19
file content (111 lines) | stat: -rw-r--r-- 2,514 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Usage

## Basics

`aiodogstatsd.Client` can be initialized with:

- `host` — host string of your StatsD server (default: `localhost`);
- `port` — post of your StatsD server (default: `9125`);
- `namespace` — optional namespace string to prefix all metrics;
- `constant_tags` — optional tags dictionary to apply to all metrics;
- `read_timeout` (default: `0.5`);
- `close_timeout`;
- `sample_rate` (default: `1`).

Below you can find an example of client initialization. Keep your eyes on lines 13 and 15. You always need to not to forget to initialize connection and close it at the end:

```python hl_lines="13 15"
client = aiodogstatsd.Client(
    host="127.0.0.1",
    port=8125,
    namespace="hello",
    constant_tags={
        "service": "auth",
    },
    read_timeout=0.5,
    close_timeout=0.5,
    sample_rate=1,
)

await client.connect()
client.increment("users.online")
await client.close()
```

## Context manager

As an option you can use `aiodogstatsd.Client` as a context manager. In that case you don't need to remember to initialize and close connection:

```python
async with aiodogstatsd.Client() as client:
    client.increment("users.online")
```

## Sending metrics

### Gauge

Record the value of a gauge, optionally setting `tags` and a `sample_rate`.

```python
client.gauge("users.online", value=42)
```

### Increment

Increment a counter, optionally setting a `value`, `tags` and a `sample_rate`.

```python
client.increment("users.online")
```

### Decrement

Decrement a counter, optionally setting a `value`, `tags` and a `sample_rate`.

```python
client.decrement("users.online")
```

### Histogram

Sample a histogram value, optionally setting `tags` and a `sample_rate`.

```python
client.histogram("request.time", value=0.2)
```

### Distribution

Send a global distribution value, optionally setting `tags` and a `sample_rate`.

```python
client.distribution("uploaded.file.size", value=8819)
```

### Timing

Record a timing, optionally setting `tags` and a `sample_rate`.

```python
client.timing("query.time", value=0.5)
```

### TimeIt

Context manager for easily timing methods, optionally settings `tags`, `sample_rate` and `threshold_ms`.

```python
with client.timeit("query.time"):
    ...
```

### timeit_task

Wrapper for `asyncio.create_task` that creates a task from a given `Awaitable` and sends timing metric of it's duration.

```python
async def do_something():
    await asyncio.sleep(1.0)
await client.timeit_task(do_something(), "task.time")
```