influxdata / influxdata/kapacitor
Requesting easier method to test scripts than recordings
- Dominant language
- Go
- Stars
- 2.4k
- Forks
- 479
- Avg merge
- 4d 16h
- Merged PRs (30d)
- 4
Description
## Problem
This is a feature request to provide an easier way to test kapacitor scripts than using recordings.
Recordings are very difficult to work with for a number of reasons:
To start, you have to submit the data you want to test to a live InfluxDB server. This in itself poses several problems.
If the scenario you want to test is not actually happening at that moment, you have to manually inject fake data. If this is a production system this would a big downside as people might be using the system, and the fake data could screw things up.
So the solution here might be to spin up a local InfluxDB & kapacitor, but it becomes a lot of work just to test a kapacitor script against some sample data:
1. Install InfluxDB
2. Start InfluxDB
3. Create test database
4. Install kapacitor
5. Configure kapacitor
6. Start kapacitor
7. Add tick script
8. Enable tick script
9. Start recording
10. Inject data to InfluxDB
11. Stop recording
12. Tweak script
13. Add updated tick script
14. Replay recording
Yes most of these are little tasks, but it's quite a lot of them, and requires juggling multiple terminal windows around.
And even doing this, you still have timing difficulties. With stream scripts it's not that hard to forge the timestamp of point before submitting it, but with batch scripts it's a lot harder as the batch query is scheduled, so you have to coordinate your data injection with kapacitor's polling interval.
And if you want to tweak your sample data just a little bit, you have to go through teps 9-14 over again.
And then a few weeks down the road, if you want to make a change to your tick script, and you want to make sure your change doesn't break any previous behavior, you have to make sure you keep those recordings around so you can test with them again. And if you work on a team of people, and other people might modify the script, you have to somehow transfer those recordings around.
## Proposal
Ultimately I think it would be a lot easier if there were a way to keep the test data in the script itself, or in a file along side it (which could be stored in a VCS).
The data should also be human maintainable. Meaning a person should be able to construct & edit the data by hand, without having to record data.
Both these would make scripts a lot easier to develop and maintain.
----
I personally have a hack I use for doing this with stream scripts. I start with a script such as:
```
//> foo x=0i 0s
//> foo v=1i 1000ms
//> foo v=2i 2000ms
//> foo v=0i 3000ms
//> foo v=8i 4000ms
//> foo v=16i 5000ms
var data = stream|from().measurement('foo')
|log()
```
I then have & run the following shell script:
```bash
#!/bin/bash
kapacitord -config /dev/stdin < ' $tick_file | \
sed -e 's#^//> ##' | \
awk '{ sub("us","000"); sub("ms","000000",$NF); sub("s","000000000",$NF); print }' | \
curl -X POST 'http://localhost:9092/kapacitor/v1/write?db=db&rp=rp' --data-binary @-
echo
```
The script spins up a `kapacitord`, adds & enables the script, parses the `//>` lines out of the script, and injects them to kapacitor's write API endpoint.
This means I can keep my sample data with my script, and test it with a single command. It's easy to use, and has no InfluxDB involved. Unfortunately it does not handle batch scripts.
It would be nice if kapacitor had similar functionality.
Contributor guide
Research direction
Start by reviewing the kapacitord configuration, the kapacitor define and enable commands, and the write API used by the example shell script. Define how human-maintainable sample data can live with a tick script and support both stream and batch scripts without recordings or a live InfluxDB server.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, shell
- Domain
- stream-processing, testing-qa
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100