|
| 1 | +(statsd-tutorial)= |
| 2 | +# Load data into CrateDB using StatsD and Telegraf |
| 3 | + |
| 4 | +This tutorial walks you through configuring [Telegraf] to receive [StatsD] |
| 5 | +metrics and store them into CrateDB. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +Docker is used for running all components. This approach works consistently |
| 10 | +across Linux, macOS, and Windows. Alternatively, you can use Podman. |
| 11 | + |
| 12 | +### CLI |
| 13 | + |
| 14 | +Prepare shortcut for the psql command. |
| 15 | + |
| 16 | +::::{tab-set} |
| 17 | +:sync-group: os |
| 18 | + |
| 19 | +:::{tab-item} Linux and macOS |
| 20 | +:sync: unix |
| 21 | +To make the settings persistent, add them to your shell profile (`~/.profile`). |
| 22 | +```shell |
| 23 | +alias nc="docker run --rm -i --network=cratedb-demo docker.io/toolbelt/netcat" |
| 24 | +alias psql="docker run --rm -i --network=cratedb-demo docker.io/postgres psql" |
| 25 | +``` |
| 26 | +::: |
| 27 | +:::{tab-item} Windows PowerShell |
| 28 | +:sync: powershell |
| 29 | +To make the settings persistent, add them to your PowerShell profile (`$PROFILE`). |
| 30 | +```powershell |
| 31 | +function psql { docker run --rm -i --network=cratedb-demo docker.io/postgres psql @args } |
| 32 | +``` |
| 33 | +::: |
| 34 | +:::{tab-item} Windows Command |
| 35 | +:sync: dos |
| 36 | +```shell |
| 37 | +doskey psql=docker run --rm -i --network=cratedb-demo docker.io/postgres psql $* |
| 38 | +``` |
| 39 | +::: |
| 40 | + |
| 41 | +:::: |
| 42 | + |
| 43 | +### CrateDB |
| 44 | + |
| 45 | +Create a shared network. |
| 46 | +```shell |
| 47 | +docker network create cratedb-demo |
| 48 | +``` |
| 49 | + |
| 50 | +Start CrateDB. |
| 51 | +```shell |
| 52 | +docker run --name=cratedb --rm -it --network=cratedb-demo \ |
| 53 | + --publish=4200:4200 --publish=5432:5432 \ |
| 54 | + --env=CRATE_HEAP_SIZE=2g docker.io/crate -Cdiscovery.type=single-node |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +## Configure Telegraf |
| 59 | + |
| 60 | +Configure Telegraf to receive StatsD and to store them |
| 61 | +in CrateDB, by using the configuration blueprint outlined below, possibly |
| 62 | +adjusting it to match your environment. Store this file under the name |
| 63 | +`telegraf.conf`. |
| 64 | + |
| 65 | +:::{literalinclude} telegraf.conf |
| 66 | +::: |
| 67 | + |
| 68 | + |
| 69 | +## Start Telegraf |
| 70 | + |
| 71 | +::::{tab-set} |
| 72 | +:sync-group: os |
| 73 | + |
| 74 | +:::{tab-item} Linux and macOS |
| 75 | +:sync: unix |
| 76 | +```shell |
| 77 | +docker run --name=telegraf --rm -it --network=cratedb-demo \ |
| 78 | + --volume "$(pwd)"/telegraf.conf:/etc/telegraf/telegraf.conf \ |
| 79 | + --publish 8125:8125/udp \ |
| 80 | + docker.io/telegraf |
| 81 | +``` |
| 82 | +::: |
| 83 | +:::{tab-item} Windows PowerShell |
| 84 | +:sync: powershell |
| 85 | +```powershell |
| 86 | +docker run --name=telegraf --rm -it --network=cratedb-demo ` |
| 87 | + --volume "${PWD}\telegraf.conf:/etc/telegraf/telegraf.conf" ` |
| 88 | + --publish 8125:8125/udp ` |
| 89 | + docker.io/telegraf |
| 90 | +``` |
| 91 | +::: |
| 92 | +:::{tab-item} Windows Command |
| 93 | +:sync: dos |
| 94 | +```shell |
| 95 | +docker run --name=telegraf --rm -it --network=cratedb-demo ^ |
| 96 | + --volume "%cd%\telegraf.conf:/etc/telegraf/telegraf.conf" ^ |
| 97 | + --publish 8125:8125/udp ^ |
| 98 | + docker.io/telegraf |
| 99 | +``` |
| 100 | +::: |
| 101 | +:::: |
| 102 | + |
| 103 | +## Submit data |
| 104 | + |
| 105 | +### netcat |
| 106 | +Use [netcat] for submitting data. |
| 107 | +```shell |
| 108 | +echo "temperature:42|g\nhumidity:84|g" | nc -C -w 1 -u telegraf 8125 |
| 109 | +``` |
| 110 | + |
| 111 | +### Python |
| 112 | +Use the [statsd package] to submit data from your Python application. |
| 113 | +```shell |
| 114 | +uv pip install statsd |
| 115 | +``` |
| 116 | +```python |
| 117 | +from statsd import StatsClient |
| 118 | + |
| 119 | +statsd = StatsClient("localhost", 8125) |
| 120 | +statsd.gauge("temperature", 42) |
| 121 | +statsd.gauge("humidity", 84) |
| 122 | +statsd.close() |
| 123 | +``` |
| 124 | + |
| 125 | +### Any |
| 126 | + |
| 127 | +Use any of the available [StatsD client libraries] for Node.js, Java, Python, |
| 128 | +Ruby, Perl, PHP, Clojure, Io, C, C++, .NET, Go, Apache, Varnish, PowerShell, |
| 129 | +Browser, Objective-C, ActionScript, WordPress, Drupal, Haskell, R, Lua, or |
| 130 | +Nim. |
| 131 | + |
| 132 | +## Explore data |
| 133 | + |
| 134 | +After receiving data, the first metrics will appear in the designated table in |
| 135 | +CrateDB, ready to be inspected. |
| 136 | +```shell |
| 137 | +psql "postgresql://crate:crate@cratedb:5432/" -c "SELECT * FROM doc.metrics ORDER BY timestamp LIMIT 5;" |
| 138 | +``` |
| 139 | +```psql |
| 140 | + hash_id | timestamp | name | tags | fields | day |
| 141 | +----------------------+----------------------------+-------------+-----------------------------------------------+--------------+---------------------------- |
| 142 | + -8005856065082590291 | 2025-09-20 19:30:45.000+00 | temperature | {"host":"2748411a9651","metric_type":"gauge"} | {"value":42} | 2025-09-20 00:00:00.000+00 |
| 143 | + 7068016256787696496 | 2025-09-20 19:30:45.000+00 | humidity | {"host":"2748411a9651","metric_type":"gauge"} | {"value":84} | 2025-09-20 00:00:00.000+00 |
| 144 | +(2 rows) |
| 145 | +``` |
| 146 | + |
| 147 | + |
| 148 | +[netcat]: https://en.wikipedia.org/wiki/Netcat |
| 149 | +[StatsD]: https://github.com/statsd/statsd |
| 150 | +[statsd package]: https://pypi.org/project/statsd/ |
| 151 | +[StatsD client libraries]: https://github.com/statsd/statsd/wiki#client-implementations |
| 152 | +[Telegraf]: https://www.influxdata.com/time-series-platform/telegraf/ |
0 commit comments