Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit Starlark README #7832

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions plugins/processors/starlark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ The `starlark` processor calls a Starlark function for each matched metric,
allowing for custom programmatic metric processing.

The Starlark language is a dialect of Python, and will be familiar to those who
have experience with the Python language. However, keep in mind that it is not
Python and that there are major syntax [differences][#Python Differences].
have experience with the Python language. However, there are major [differences](#python-differences).
Existing Python code is unlikely to work unmodified. The execution environment
is sandboxed, and it is not possible to do I/O operations such as reading from
files or sockets.
Expand All @@ -22,7 +21,7 @@ Telegraf minimum version: Telegraf 1.15.0
## The Starlark source can be set as a string in this configuration file, or
## by referencing a file containing the script. Only one source or script
## should be set at once.
##

## Source of the Starlark script.
source = '''
def apply(metric):
Expand All @@ -35,29 +34,31 @@ def apply(metric):

### Usage

The script should contain a function called `apply` that takes the metric as
The Starlark code should contain a function called `apply` that takes a metric as
its single argument. The function will be called with each metric, and can
return `None`, a single metric, or a list of metrics.

```python
def apply(metric):
return metric
```

Reference the Starlark [specification][] to see the list of available types and
functions that can be used in the script. In addition to these the following
For a list of available types and functions that can be used in the code, see
the Starlark [specification][].

In addition to these, the following InfluxDB-specific
types and functions are exposed to the script.

**Metric(*name*)**:
- **Metric(*name*)**:
Create a new metric with the given measurement name. The metric will have no
tags or fields and defaults to the current time.

- **name**:
The name is a [string][string] containing the metric measurement name.
The name is a [string][] containing the metric measurement name.

- **tags**:
A [dict-like][dict] object containing the metric's tags.


- **fields**:
A [dict-like][dict] object containing the metric's fields. The values may be
of type int, float, string, or bool.
Expand All @@ -66,18 +67,20 @@ of type int, float, string, or bool.
The timestamp of the metric as an integer in nanoseconds since the Unix
epoch.

**deepcopy(*metric*)**: Make a copy of an existing metric.
- **deepcopy(*metric*)**: Make a copy of an existing metric.

### Python Differences

While Starlark is similar to Python it is not the same.
While Starlark is similar to Python, there are important differences to note:

- Starlark has limited support for error handling and no exceptions. If an
error occurs the script will immediately end and Telegraf will drop the
metric. Check the Telegraf logfile for details about the error.

- It is not possible to import other packages and the Python standard library
is not available. As such, it is not possible to open files or sockets.
is not available.

- It is not possible to open files or sockets.

- These common keywords are **not supported** in the Starlark grammar:
```
Expand All @@ -102,6 +105,7 @@ Use `deepcopy(metric)` to create a copy of the metric.
**How can I return multiple metrics?**

You can return a list of metrics:

```python
def apply(metric):
m2 = deepcopy(metric)
Expand All @@ -119,6 +123,7 @@ Use the `Metric(name)` function and set at least one field.
**What is the fastest way to iterate over tags/fields?**

The fastest way to iterate is to use a for-loop on the tags or fields attribute:

```python
def apply(metric):
for k in metric.tags:
Expand All @@ -127,7 +132,8 @@ def apply(metric):
```

When you use this form, it is not possible to modify the tags inside the loop,
if this is needed you should use the `.keys()`, `.values()`, or `.items()` forms:
if this is needed you should use one of the `.keys()`, `.values()`, or `.items()` methods:

```python
def apply(metric):
for k, v in metric.tags.items():
Expand Down