Skip to content

Commit bd59d56

Browse files
authored
Merge pull request #13 from taskbadger/sk/tags
docs for task tags
2 parents 22fb4f0 + c1d81bc commit bd59d56

File tree

8 files changed

+95
-14
lines changed

8 files changed

+95
-14
lines changed

docs/cli.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,13 @@ API Key: XYZ.ABC
134134

135135
Config written to ~/.config/taskbadger/config
136136
```
137+
138+
#### Tags
139+
140+
[Tags](data_model.md#tags) can also be added to the configuration file:
141+
142+
```toml
143+
[tags]
144+
environment = "production"
145+
host = "server-1"
146+
```

docs/data_model.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ The main attributes or a task are:
5959
: This represents the maximum number of seconds allowed between task updates. If a task does not receive
6060
updates within this period it will be marked as 'stale'.
6161

62+
<a name="tags"></a>
63+
`tags`
64+
65+
: A list of tags that can be used to categorize tasks. Tags are useful for filtering tasks in the UI. Each tag
66+
has a name and a value. For example, a task may have a tag `environment:production`.
67+
6268
### Example Task
6369

6470
```json
@@ -81,7 +87,10 @@ The main attributes or a task are:
8187
"created": "2022-08-24T14:15:22Z",
8288
"updated": "2022-08-24T16:15:22Z",
8389
"url": "https://taskbadger.net/a/{example_org/tasks/57ae8eVBrH7jbDgmYj6Ut2vR9S/",
84-
"public_url": "https://taskbadger.net/public/tasks/57ae8eVBrH7jbDgmYj6Ut2vR9S/"
90+
"public_url": "https://taskbadger.net/public/tasks/57ae8eVBrH7jbDgmYj6Ut2vR9S/",
91+
"tags": [
92+
{"environment": "production"}
93+
]
8594
}
8695
```
8796

docs/index.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,32 @@ In order to use the API you will need the following details:
1717
* Create one on your Profile page
1818

1919

20+
## Montior Celery Tasks
21+
22+
When using the [Celery integration](python-celery.md) you can use the `CelerySystemIntegration` class to automatically record tasks.
23+
24+
```python
25+
from celery import Celery
26+
import taskbadger
27+
from taskbadger.systems.celery import CelerySystemIntegration
28+
29+
app = Celery('hello', broker='amqp://guest@localhost//')
30+
31+
taskbadger.init(
32+
organization_slug="my-org",
33+
project_slug="my-project",
34+
token="***",
35+
tags={"environment": "production"},
36+
systems=[CelerySystemIntegration(record_task_args=True)]
37+
)
38+
39+
@app.task
40+
def hello(who):
41+
return f'hello {who}'
42+
```
43+
44+
This is all the configuration that is needed to do basic task tracking with Celery.
45+
2046
## Monitor a task from the command line
2147

2248
The Task Badger CLI allows you to run commands from the shell and have them monitored
@@ -41,6 +67,7 @@ Config written to ~/.config/taskbadger/confi
4167
```shell
4268
$ taskbadger run "demo task" \
4369
--action "error email to:me@test.com" \
70+
--tag "environment=staging" \
4471
-- path/to/script.sh
4572

4673
Task created: https://taskbadger.net/public/tasks/xyz/
@@ -53,8 +80,6 @@ See more about the [CLI](cli.md).
5380

5481
## Use the API directly
5582

56-
57-
5883
=== "Python"
5984

6085
Install the `taskbadger` Python library:
@@ -71,7 +96,8 @@ See more about the [CLI](cli.md).
7196
taskbadger.init(
7297
organization_slug="my-org",
7398
project_slug="my-project",
74-
token="***"
99+
token="***",
100+
tags={"environment": "production"},
75101
)
76102
```
77103

@@ -93,7 +119,7 @@ data.
93119
=== "Python"
94120

95121
```python
96-
> task = Task.create("task name", stale_timeout=10)
122+
> task = Task.create("task name", stale_timeout=10, tags={"environment": "staging"})
97123
> task.id
98124
"128aoa98e0fiq238"
99125
```
@@ -104,7 +130,7 @@ data.
104130
$ curl -X POST "https://taskbadger.net/api/${ORG}/${PROJECT}/tasks/" \
105131
-H "Authorization: Bearer ${API_KEY}" \
106132
-H "Content-Type: application/json" \
107-
-d '{"name": "demo task", "stale_timeout": 10}'
133+
-d '{"name": "demo task", "stale_timeout": 10, "tags": {"environment": "staging"}}'
108134
```
109135

110136
The response will include the task ID which is needed for updating the task.
@@ -127,7 +153,8 @@ data.
127153
"created": "2022-09-22T06:53:40.683555Z",
128154
"updated": "2022-09-22T06:53:40.683555Z"
129155
"url": "https://taskbadger.net/a/{example_org/tasks/{task_id}/",
130-
"public_url": "https://taskbadger.net/public/tasks/{task_id}/"
156+
"public_url": "https://taskbadger.net/public/tasks/{task_id}/",
157+
"tags": {"environment": "staging"}
131158
}
132159
```
133160

docs/python-celery.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ taskbadger.init(
2525
organization_slug="my-org",
2626
project_slug="my-project",
2727
token="***",
28-
systems=[CelerySystemIntegration()]
28+
systems=[CelerySystemIntegration()],
29+
tags={"environment": "production"}
2930
)
3031
```
3132

docs/python.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import taskbadger
2020
taskbadger.init(
2121
organization_slug="my-org",
2222
project_slug="my-project",
23-
token="***"
23+
token="***",
24+
tags={"environment": "production"},
2425
)
2526
```
2627

@@ -29,6 +30,9 @@ Details about these configuration parameters can be found [here](basics.md#organ
2930
If you attempt to use the SDK without configuring it you will get an error. To avoid this you can use the
3031
[safe functions](#safe-functions) which will log any errors to the `taskbadger` logger.
3132

33+
!!! tip
34+
[Tags](data_model.md#tags) provided here will be applied to all tasks created using the SDK. If you need to add tags to individual tasks you can do so using the create and update methods or the `task.add_tag` method. Tags added manually will override the global tags.
35+
3236
## Usage
3337

3438
The SDK provides a [Task](#taskbadger.Task) class which offers a convenient interface to the API.
@@ -49,7 +53,8 @@ task = Task.create(
4953
trigger="*/10%,success,error",
5054
integration=EmailIntegration(to="me@example.com")
5155
)
52-
]
56+
],
57+
tags={"tenant": "acme"}
5358
)
5459
```
5560

@@ -82,16 +87,15 @@ session management is handled automatically within the body of the function or C
8287

8388
### Scope
8489

85-
The SDK provides the `taskbadger.current_scope` context manager which can be used to set custom data
86-
for the duration of the context. The content of the scope will be merged with any custom task data
87-
passed directly to any of the other API methods.
90+
The SDK provides the `taskbadger.current_scope` context manager which can be used to set custom data and modify tags for the duration of the context. The content of the scope will be merged with any custom task data passed directly to any of the other API methods.
8891

8992
```python
9093
import socket
9194
import taskbadger
9295

9396
with taskbadger.current_scope() as scope:
9497
scope["hostname"] = socket.gethostname()
98+
scope.tag({"tenant": "acme"})
9599
```
96100

97101
A common use case for this is to add request scoped context in frameworks like Django or Flask using a custom
@@ -104,6 +108,7 @@ def taskbadger_scope_middleware(get_response):
104108
def middleware(request):
105109
with taskbadger.current_scope() as scope:
106110
scope["user"] = request.user.username
111+
scope.tag({"tenant": request.tenant.slug})
107112
return get_response(request)
108113

109114
return middleware
@@ -113,7 +118,7 @@ def taskbadger_scope_middleware(get_response):
113118

114119
The data passed directly to the API will take precedence over the data in the current scope. If the same
115120
key is present in the current scope as well as the data passed in directly, the value in the data passed
116-
directly will be used.
121+
directly will be used. The same applies to tags.
117122

118123

119124
## Python Reference

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ plugins:
9898
separate_signature: True
9999
members_order: source
100100
heading_level: 3
101+
line_length: 80
101102
- search:
102103
separator: '[\s\-,:!=\[\]()"/]+|(?!\b)(?=[A-Z][a-z])|\.(?!\d)|&[lg]t;'
103104
watch:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ dependencies = [
88
"invoke",
99
"mkdocs-material",
1010
"mkdocstrings[python]",
11+
"ruff>=0.9.6",
1112
]

uv.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)