|
| 1 | +(opentelemetry-tutorial-otelcol)= |
| 2 | +# Connect the OpenTelemetry Collector to CrateDB |
| 3 | + |
| 4 | +This tutorial walks you through configuring the [OpenTelemetry Collector], |
| 5 | +its built-in [Prometheus Remote Write Exporter], and the |
| 6 | +[CrateDB Prometheus Adapter], to receive [OpenTelemetry] [metrics] and |
| 7 | +store them into CrateDB. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Docker is used for running all components. This approach works consistently |
| 12 | +across Linux, macOS, and Windows. Alternatively, you can use Podman. |
| 13 | + |
| 14 | +### Commands |
| 15 | + |
| 16 | +Prepare shortcut for {ref}`crate-crash:index` command. |
| 17 | + |
| 18 | +::::{tab-set} |
| 19 | +:sync-group: os |
| 20 | + |
| 21 | +:::{tab-item} Linux and macOS |
| 22 | +:sync: unix |
| 23 | +Add these settings to your shell profile (`~/.profile`) to make them persistent. |
| 24 | +```shell |
| 25 | +alias crash="docker run --rm -it --network=host crate/crate:latest crash" |
| 26 | +``` |
| 27 | +::: |
| 28 | +:::{tab-item} Windows PowerShell |
| 29 | +:sync: powershell |
| 30 | +Add these settings to your PowerShell profile (`$PROFILE`) to make them persistent. |
| 31 | +```powershell |
| 32 | +function crash { docker run --rm -i --network=host crate/crate:latest crash @args } |
| 33 | +``` |
| 34 | +::: |
| 35 | +:::{tab-item} Windows Command |
| 36 | +:sync: dos |
| 37 | +```shell |
| 38 | +doskey crash=docker run --rm -i --network=host crate/crate:latest crash $* |
| 39 | +``` |
| 40 | +::: |
| 41 | + |
| 42 | +:::: |
| 43 | + |
| 44 | +### Services |
| 45 | + |
| 46 | +Save the files {download}`compose.yaml`, |
| 47 | +{download}`cratedb-prometheus-adapter.yaml`, |
| 48 | +{download}`otelcol.yaml` and {download}`ddl.sql` |
| 49 | +to your machine and start all services using |
| 50 | +Docker Compose or Podman Compose. |
| 51 | +```shell |
| 52 | +docker compose up |
| 53 | +``` |
| 54 | + |
| 55 | +## Submit data |
| 56 | + |
| 57 | +### Use netcat |
| 58 | + |
| 59 | +Use [netcat] to submit metrics using the [Carbon plaintext protocol]. |
| 60 | +```shell |
| 61 | +printf "temperature;job=app 42.42 1758486061\nhumidity;job=app 84.84 1758486061" \ |
| 62 | + | nc -c localhost 2003 |
| 63 | +``` |
| 64 | + |
| 65 | +### Use Python |
| 66 | + |
| 67 | +Use the OTel Python language SDK to submit metrics. To do that, |
| 68 | +save the Python OTel example file {download}`example.py` to your machine and |
| 69 | +use the `opentelemetry-instrument` program to invoke your Python application. |
| 70 | +```shell |
| 71 | +opentelemetry-instrument --service_name=app python example.py |
| 72 | +``` |
| 73 | +:::{literalinclude} example.py |
| 74 | +::: |
| 75 | +The [uv] utility can invoke the demo program including dependencies, |
| 76 | +otherwise install them using `pip install opentelemetry-distro opentelemetry-exporter-otlp` |
| 77 | +or similarly. |
| 78 | +```shell |
| 79 | +uv run --with=opentelemetry-distro --with=opentelemetry-exporter-otlp \ |
| 80 | + opentelemetry-instrument --service_name=app python example.py |
| 81 | +``` |
| 82 | + |
| 83 | +### Use any language |
| 84 | + |
| 85 | +Use any of the available [OpenTelemetry language APIs & SDKs] for C++, C#/.NET, |
| 86 | +Erlang/Elixir, Go, Java, JavaScript, PHP, Python, Ruby, Rust, or Swift. |
| 87 | + |
| 88 | +## Explore data |
| 89 | + |
| 90 | +CrateDB stored the metrics in the designated table, ready for inspection and analysis. |
| 91 | +```shell |
| 92 | +crash --hosts "http://crate:crate@localhost:4200/" \ |
| 93 | + -c "SELECT * FROM testdrive.metrics ORDER BY timestamp LIMIT 5;" |
| 94 | +``` |
| 95 | +```psql |
| 96 | ++---------------+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+---------------------+----------------+ |
| 97 | +| timestamp | labels_hash | labels | value | valueRaw | day__generated | |
| 98 | ++---------------+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+---------------------+----------------+ |
| 99 | +| 1758480857158 | 64614d7f1ef80933 | {"__name__": "target_info", "job": "app", "subsystem": "otel-testdrive", "telemetry_auto_version": "0.58b0", "telemetry_sdk_language": "python", "telemetry_sdk_name": "opentelemetry", "telemetry_sdk_version": "1.37.0"} | 1.0 | 4607182418800017408 | 1758412800000 | |
| 100 | +| 1758480857158 | 7c6f57205e58af4c | {"__name__": "temperature", "job": "app", "subsystem": "otel-testdrive"} | 42.42 | 4631166901565532406 | 1758412800000 | |
| 101 | +| 1758480857158 | 3fce270356467381 | {"__name__": "humidity", "job": "app", "subsystem": "otel-testdrive"} | 84.84 | 4635670501192902902 | 1758412800000 | |
| 102 | ++---------------+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+---------------------+----------------+ |
| 103 | +SELECT 3 rows in set (0.005 sec) |
| 104 | +``` |
| 105 | + |
| 106 | + |
| 107 | +[Carbon plaintext protocol]: https://graphite.readthedocs.io/en/latest/feeding-carbon.html |
| 108 | +[CrateDB Prometheus Adapter]: https://github.com/crate/cratedb-prometheus-adapter |
| 109 | +[metrics]: https://opentelemetry.io/docs/concepts/signals/metrics/ |
| 110 | +[netcat]: https://en.wikipedia.org/wiki/Netcat |
| 111 | +[OpenTelemetry]: https://opentelemetry.io/docs/what-is-opentelemetry/ |
| 112 | +[OpenTelemetry Collector]: https://opentelemetry.io/docs/collector/ |
| 113 | +[OpenTelemetry language APIs & SDKs]: https://opentelemetry.io/docs/languages/ |
| 114 | +[Prometheus Remote Write Exporter]: https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/prometheusremotewriteexporter |
| 115 | +[uv]: https://docs.astral.sh/uv/ |
0 commit comments