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
> The Processing engine is currently supported only in Docker x86 environments. Non-Docker support is coming soon. The engine, API, and developer experience are actively evolving and may change. Join our [Discord](https://discord.gg/9zaNCW2PRT) for updates and feedback.
5
5
6
-
InfluxDB 3 has an embedded Python VM for dynamically loading plugins that can execute code in the database. There are four types of plugins that can be triggered by the following events in the database:
6
+
Use the {{% product-name %}} Processing engine to run code and perform tasks
7
+
for different database events.
7
8
8
-
***WAL flush**: Triggered when the write-ahead log (WAL) is flushed to object store (once a second by default)
9
-
***Parquet persistenc (coming soon)**: Triggered when data is persisted to object store in Parquet format
10
-
***Scheduled tasks**: Triggered by a schedule, specified in cron sytnax
11
-
***On Request**: Bind to a specific endpoint under `/api/v3/engine` and trigger when GET or POST requests are made
9
+
{{% product-name %}} provides the InfluxDB 3 Processing engine, an embedded Python VM that can dynamically load and trigger Python plugins
10
+
in response to events in your database.
12
11
13
-
Each plugin type has a different trigger configuration, which will be described in the section on each plugin type.
12
+
## Plugins
14
13
15
-
When starting the server, the argument `--plugin-dir` must be provided that specifies what directory plugins are located in. There is also a public Github repository of example plugins that can be referenced when creating a trigger. The repository at [https://github.com/influxdata/influxdb3_plugins](https://github.com/influxdata/influxdb3_plugins) contans example plugins and contributions from the community.
14
+
> [!Note]
15
+
> #### Contribute and use community plugins
16
+
>
17
+
> [influxdata/influxdb3_plugins](https://github.com/influxdata/influxdb3_plugins) is a public repository on GitHub where you can find
18
+
> and contribute example plugins.
19
+
> You can reference plugins from the repository directly within a trigger configuration.
20
+
21
+
A Processing engine _plugin_ is Python code you provide to run tasks, such as
22
+
downsampling data, monitoring, creating alerts, or calling external services.
23
+
24
+
## Triggers
25
+
26
+
A _trigger_ is an InfluxDB 3 resource you create to associate a database
27
+
event (for example, a WAL flush) with the plugin that should run.
28
+
When an event occurs, the trigger passes configuration, optional arguments, and event data to the plugin.
29
+
30
+
The Processing engine provides four types of plugins and triggers--each type corresponds to an event type with event-specific configuration to let you handle events with targeted logic.
31
+
32
+
-**WAL flush**: Triggered when the write-ahead log (WAL) is flushed to the object store (default is every second)
33
+
-**Parquet persistence (coming soon)**: Triggered when InfluxDB 3 persists data to object store Parquet files
34
+
-**Scheduled tasks**: Triggered on a schedule you specify using cron syntax
35
+
-**On Request**: Bound to the HTTP API `/api/v3/engine/<CUSTOM_PATH>` endpoint and triggered by a GET or POST request to the endpoint.
36
+
37
+
## Activate the Processing engine
38
+
To enable the Processing engine, start the {{% product-name %}} server with the --plugin-dir argument and a path to your plugins directory (it doesn't need to exist yet)--for example:
Within any of the plugin types, a shared API is available to interact with the database. The shared API provides access to the following:
20
-
*`LineBuilder` to create Line Protocol lines that can be written to the database
21
-
*`query` to query data from any database
22
-
*`info`, `warn`, and `error` to log messages to the database log, which will be output in the server logs and captured in system tables queryable by SQL
46
+
All plugin types provide the InfluxDB 3 _shared API_ for interacting with the database.
47
+
The shared API provides access to the following:
23
48
24
-
### Line Builder
49
+
-`LineBuilder` to create Line Protocol lines for writing to the database
50
+
-`query` to query data from any database
51
+
-`info`, `warn`, and `error` to log messages to the database log, which is output in the server logs and captured in system tables queryable by SQL
25
52
26
-
The `LineBuilder` is a simple API for building lines of Line Protocol to write into the database. Writes are buffered while the plugin runs and are flushed when the plugin completes. The `LineBuilder` API is available in all plugin types. Here are some examples of using the `LineBuilder` API:
53
+
### Line builder
27
54
28
-
```python
29
-
line = LineBuilder("weather")
30
-
.tag("location", "us-midwest")
31
-
.float64_field("temperature", 82.5)
32
-
.time_ns(1627680000000000000)
33
-
influxdb3_local.write(line)
55
+
The `LineBuilder` is a simple API for building lines of Line Protocol to write into the database. Writes are buffered while the plugin runs and are flushed when the plugin completes. The `LineBuilder` API is available in all plugin types.
34
56
35
-
# to output it as a string: "weather,location=us-midwest temperature=82.5 1627680000000000000"
36
-
line_str = line.build()
57
+
The following example shows how to use the `LineBuilder` API:
37
58
38
-
# or build incrementally
59
+
```python
60
+
# Build line protocol incrementally
39
61
line = LineBuilder("weather")
40
62
line.tag("location", "us-midwest")
41
63
line.float64_field("temperature", 82.5)
42
64
line.time_ns(1627680000000000000)
43
65
influxdb3_local.write(line)
66
+
67
+
# Output line protocol as a string ("weather,location=us-midwest temperature=82.5 1627680000000000000")
68
+
line_str = line.build()
44
69
```
45
70
46
71
Here is the Python implementation of the `LineBuilder` API:
@@ -160,32 +185,47 @@ class LineBuilder:
160
185
```
161
186
162
187
### Query
163
-
The `query` function on the API will execute a SQL query with optional parameters (through a parameterized query) and return the results as a `List` of `Dict[String, Any]` where the key is the column name and the value is the value for that column. The `query` function is available in all plugin types.
164
188
165
-
Some examples:
189
+
The shared API `query` function executes an SQL query with optional parameters (a [parameterized query](/influxdb3/version/query-data/sql/parameterized-queries/)) and returns results as a `List` of `Dict[String, Any]` where the key is the column name and the value is the column value. The `query` function is available in all plugin types.
190
+
191
+
The following examples show how to use the `query` function:
166
192
167
193
```python
168
194
influxdb3_local.query("SELECT * from foo where bar = 'baz' and time > now() - 'interval 1 hour'")
169
195
170
-
#or using parameterized queries
196
+
#Or using parameterized queries
171
197
args = {"bar": "baz"}
172
198
influxdb3_local.query("SELECT * from foo where bar = $bar and time > now() - 'interval 1 hour'", args)
173
199
```
174
200
175
201
### Logging
176
-
The `info`, `warn`, and `error` functions on the API will log messages to the database log, which will be output in the server logs and captured in system tables queryable by SQL. The `info`, `warn`, and `error` functions are available in all plugin types. The functions take an arbitrary number of arguments and will convert them to strings and join them into a single message separated by a space. Examples:
202
+
203
+
The shared API `info`, `warn`, and `error` functions log messages to the database log, which is output in the server logs and captured in system tables queryable by SQL.
204
+
The `info`, `warn`, and `error` functions are available in all plugin types. The functions take an arbitrary number of arguments, convert them to strings, and then join them into a single message separated by a space.
205
+
206
+
The following examples show to use the `info`, `warn`, and `error` logging functions:
177
207
178
208
```python
179
209
ifluxdb3_local.info("This is an info message")
180
210
influxdb3_local.warn("This is a warning message")
181
211
influxdb3_local.error("This is an error message")
182
212
213
+
# Log a message that contains a data object
183
214
obj_to_log = {"hello": "world"}
184
215
influxdb3_local.info("This is an info message with an object", obj_to_log)
185
216
```
186
217
187
218
### Trigger arguments
188
-
Every plugin type can receive arguments from the configuration of the trigger. This is useful for passing configuration to the plugin. This can drive behavior like things to monitor for or it could be connection information to third party services that the plugin will interact with. The arguments are passed as a `Dict[str, str]` where the key is the argument name and the value is the argument value. Here's an example of how to use arguments in a WAL plugin:
219
+
220
+
Every plugin type can receive arguments from the configuration of the trigger that runs it.
221
+
You can use this to provide runtime configuration and drive behavior of a plugin--for example:
222
+
223
+
- threshold values for monitoring
224
+
- connection properties for connecting to third-party services
225
+
226
+
The arguments are passed as a `Dict[str, str]` where the key is the argument name and the value is the argument value.
227
+
228
+
The following example shows how to use an argument in a WAL plugin:
The `args` parameter is optional and can be omitted from the trigger definitions if the plugin does not need to use arguments.
239
+
The `args` parameter is optional and can be omitted from the trigger definition if the plugin doesn't need to use arguments.
200
240
201
-
## Imports
202
-
The Python plugins run using the system Python in the Docker container. Pip is installed in the container and can be used to install any dependencies.
203
-
You will need to start up the server with the `PYTHONPATH` set to the location of your site packages for your virtual environment. For example: `PYTHONPATH=myenvl/lib/python3.13/site-packages`
241
+
## Import plugin dependencies
204
242
205
-
## WAL Flush Plugin
206
-
When a WAL flush plugin is triggered, the plugin will receive a list of `table_batches` that have matched the plugin trigger (either all tables in the database or a specific table). Here's an example of a simple WAL flush plugin
243
+
Use the `influxdb3 install` command to download and install Python packages that your plugin depends on.
244
+
245
+
```bash
246
+
influxdb3 install package <PACKAGE_NAME>
247
+
248
+
### Use `influxdb3 install` with Docker
249
+
250
+
1. Start the server
251
+
252
+
```bash
253
+
docker run \
254
+
--name CONTAINER_NAME \
255
+
-v /path/to/.influxdb3/data:/data \
256
+
-v /path/to/.influxdb3/plugins:/plugins \
257
+
quay.io/influxdb/influxdb3-core:latest \
258
+
serve --node-id=node0 \
259
+
--object-store=file \
260
+
--data-dir=/data \
261
+
--http-bind=localhost:8183 \
262
+
--plugin-dir=/plugins
263
+
```
264
+
265
+
2. Use `docker exec` to run the `influxdb3 install` command:
The result is an active Python virtual environment with the package installed in`<PLUGINS_DIR>/.venv`.
272
+
You can pass additional options to use a `requirements.txt` file or a custom virtual environment path.
273
+
For more information, see the `influxdb3` CLI help:
274
+
275
+
```bash
276
+
influxdb3 install package --help
277
+
```
278
+
279
+
## WAL flush plugin
280
+
281
+
When a WAL flush plugin is triggered, the plugin receives a list of `table_batches` filtered by the trigger configuration (either _all tables_ in the database or a specific table).
282
+
283
+
The following example shows a simple WAL flush plugin:
Every trigger is associated with a specific database. The best reference for the arguments for trigger definition can be accessed through the CLI help:
309
+
When you create a trigger, you associate it with a database and provide configuration specific to the trigger type.
232
310
233
-
```shell
234
-
influxdb3 create trigger help
235
-
```
311
+
For a WAL flush trigger you specify a `trigger-spec`, which determines when the plugin is triggered (and what table data it receives):
236
312
237
-
For the WAL plugin, the `trigger-spec` can be either `all-tables` which will trigger on any write to the assoicated database or `table:<table_name>` which will call the `process_writes` function only with the writes for the given table. The trigger-spec is what the server uses to determine which plugin type the plugin-filename points to.
313
+
- `all-tables`: triggers the plugin on every write to the associated database
314
+
- `table:<table_name>` triggers the plugin functiononlyfor writes to the specified table.
238
315
239
-
The `args` parameter can be used to pass configuration to the plugin.
316
+
The following example creates a WAL flush trigger forthe `gh:examples/wal_plugin/wal_plugin.py`plugin.
240
317
241
-
For example, if creating a trigger of WAL flush from the examples repo:
Without the `gh:` at the start of the filename, the server will look for the file in its plugin directory.
322
+
The `gh:` prefix lets you fetch a plugin file directly from the [influxdata/influxdb3_plugins](https://github.com/influxdata/influxdb3_plugins) repository in GitHub.
323
+
Without the prefix, the server looks for the file inside of the plugins directory.
324
+
325
+
To provide additional configuration to your plugin, pass a list of key-value pairs in the `--trigger-arguments` option and, in your plugin, use the `args` parameter to receive the arguments.
326
+
For more information about trigger arguments, see the CLI help:
327
+
328
+
```bash
329
+
influxdb3 create trigger help
330
+
```
248
331
249
332
## Schedule Plugin
333
+
250
334
Schedule plugins run on a schedule specified in cron syntax. The plugin will receive the local API, the time of the trigger, and any arguments passed in the trigger definition. Here's an example of a simple schedule plugin:
0 commit comments