|
| 1 | +(opentelemetry-telegraf-tutorial)= |
| 2 | +# Store OpenTelemetry metrics using Telegraf and CrateDB |
| 3 | + |
| 4 | +This tutorial walks you through configuring [Telegraf] to receive |
| 5 | +[OpenTelemetry] [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 | +### Commands |
| 13 | + |
| 14 | +Prepare shortcut for {ref}`crate-crash:index` command. |
| 15 | + |
| 16 | +::::{tab-set} |
| 17 | +:sync-group: os |
| 18 | + |
| 19 | +:::{tab-item} Linux and macOS |
| 20 | +:sync: unix |
| 21 | +Add these settings to your shell profile (`~/.profile`) to make them persistent. |
| 22 | +```shell |
| 23 | +alias crash="docker run --rm -it --network=host crate/crate:latest crash" |
| 24 | +``` |
| 25 | +::: |
| 26 | +:::{tab-item} Windows PowerShell |
| 27 | +:sync: powershell |
| 28 | +Add these settings to your PowerShell profile (`$PROFILE`) to make them persistent. |
| 29 | +```powershell |
| 30 | +function crash { docker run --rm -i --network=host crate/crate:latest crash @args } |
| 31 | +``` |
| 32 | +::: |
| 33 | +:::{tab-item} Windows Command |
| 34 | +:sync: dos |
| 35 | +```shell |
| 36 | +doskey crash=docker run --rm -i --network=host crate/crate:latest crash $* |
| 37 | +``` |
| 38 | +::: |
| 39 | + |
| 40 | +:::: |
| 41 | + |
| 42 | +### Services |
| 43 | + |
| 44 | +Save the files {download}`compose.yaml` and {download}`telegraf.conf` |
| 45 | +to your machine and start all services using |
| 46 | +Docker Compose or Podman Compose. |
| 47 | +```shell |
| 48 | +docker compose up |
| 49 | +``` |
| 50 | + |
| 51 | +## Submit data |
| 52 | + |
| 53 | +### Use Python |
| 54 | + |
| 55 | +Use the OTel Python language SDK to submit metrics. To do that, |
| 56 | +save the Python OTel example file {download}`example.py` to your machine and |
| 57 | +use the `opentelemetry-instrument` program to invoke your Python application. |
| 58 | +```shell |
| 59 | +opentelemetry-instrument --service_name=app python example.py |
| 60 | +``` |
| 61 | +:::{literalinclude} example.py |
| 62 | +::: |
| 63 | +The [uv] utility can invoke the demo program including dependencies, |
| 64 | +otherwise install them using `pip install opentelemetry-distro opentelemetry-exporter-otlp` |
| 65 | +or similarly. |
| 66 | +```shell |
| 67 | +uv run --with=opentelemetry-distro --with=opentelemetry-exporter-otlp \ |
| 68 | + opentelemetry-instrument --service_name=app python example.py |
| 69 | +``` |
| 70 | + |
| 71 | +### Use any language |
| 72 | + |
| 73 | +Use any of the available [OpenTelemetry language APIs & SDKs] for C++, C#/.NET, |
| 74 | +Erlang/Elixir, Go, Java, JavaScript, PHP, Python, Ruby, Rust, or Swift. |
| 75 | + |
| 76 | +## Explore data |
| 77 | + |
| 78 | +CrateDB stored the metrics in the designated table, ready for inspection and analysis. |
| 79 | +```shell |
| 80 | +crash --hosts "http://crate:crate@localhost:4200/" \ |
| 81 | + -c "SELECT * FROM metrics ORDER BY timestamp LIMIT 5;" |
| 82 | +``` |
| 83 | +```psql |
| 84 | ++---------------------+---------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------+---------------+ |
| 85 | +| hash_id | timestamp | name | tags | fields | day | |
| 86 | ++---------------------+---------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------+---------------+ |
| 87 | +| 4549776513022193265 | 1758500094846 | temperature | {"host": "2805bf17ee55", "otel_library_name": "testdrive.meter.name", "service_name": "app", "telemetry_auto_version": "0.58b0", "telemetry_sdk_language": "python", "telemetry_sdk_name": "opentelemetry", "telemetry_sdk_version": "1.37.0"} | {"gauge": 42.42} | 1758499200000 | |
| 88 | +| -926134856403504308 | 1758500094846 | humidity | {"host": "2805bf17ee55", "otel_library_name": "testdrive.meter.name", "service_name": "app", "telemetry_auto_version": "0.58b0", "telemetry_sdk_language": "python", "telemetry_sdk_name": "opentelemetry", "telemetry_sdk_version": "1.37.0"} | {"gauge": 84.84} | 1758499200000 | |
| 89 | ++---------------------+---------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------+---------------+ |
| 90 | +SELECT 2 rows in set (0.049 sec) |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | +[metrics]: https://opentelemetry.io/docs/concepts/signals/metrics/ |
| 95 | +[OpenTelemetry]: https://opentelemetry.io/docs/what-is-opentelemetry/ |
| 96 | +[OpenTelemetry language APIs & SDKs]: https://opentelemetry.io/docs/languages/ |
| 97 | +[Telegraf]: https://www.influxdata.com/time-series-platform/telegraf/ |
| 98 | +[uv]: https://docs.astral.sh/uv/ |
0 commit comments