|
| 1 | +--- |
| 2 | +title: Deploying OTel Collector on Railway |
| 3 | +short: Railway |
| 4 | +--- |
| 5 | +import { Steps } from "nextra/components"; |
| 6 | + |
| 7 | +# Deploying OTel Collector on Railway |
| 8 | +OTel Collector is available as an official [Docker image](https://hub.docker.com/r/otel/opentelemetry-collector) on Docker Hub. We can use it as a base image and layer in our configuration before deploying it on Railway |
| 9 | +You find the resources for the Deployment [here](https://github.com/autometrics-dev/autometrics-shared/tree/main/deployment-guides-resources/otel-collector). |
| 10 | + |
| 11 | +<Steps> |
| 12 | + |
| 13 | +### Create a GitHub repository and a Otel Collector configuration file |
| 14 | +To set up the OTel Collector instance, utilize Railway's repository deployment and leverage [Dockerfile features](https://docs.railway.app/deploy/dockerfiles). |
| 15 | +Create a file named `otel-collector-config.yaml` to specify a receiver endpoint for sending metrics from the instrumented application |
| 16 | + |
| 17 | +```yaml {filename="otel-collector-config.yml"} showLineNumbers |
| 18 | +receivers: |
| 19 | + otlp: |
| 20 | + protocols: |
| 21 | + http: |
| 22 | + endpoint: 0.0.0.0:4318 |
| 23 | + |
| 24 | + pipelines: |
| 25 | + metrics: |
| 26 | + receivers: [otlp] |
| 27 | +``` |
| 28 | +#### Optional: Export metrics |
| 29 | +As the OTel Collector acts as a proxy, we can define an exporter to send metrics. |
| 30 | +In this example, metrics are pushed to [Prometheus's remote write receiver](https://prometheus.io/docs/prometheus/latest/querying/api/#remote-write-receiver). |
| 31 | +Note that while Prometheus advises against this method for efficient sample ingestion, it is chosen here because Autometrics relies on Prometheus for superior querying and visualization capabilities. |
| 32 | +
|
| 33 | +```yaml {filename="otel-collector-config.yml"} showLineNumbers |
| 34 | +exporters: |
| 35 | + prometheusremotewrite: |
| 36 | + endpoint: http://prometheus:9090/api/v1/write |
| 37 | + resource_to_telemetry_conversion: |
| 38 | + enabled: true # Convert resource attributes to |
| 39 | + |
| 40 | + pipelines: |
| 41 | + metrics: |
| 42 | + exporters: [prometheusremotewrite] |
| 43 | +``` |
| 44 | +You can find the full otel-config.yaml [here](https://github.com/autometrics-dev/autometrics-shared/blob/main/deployment-guides-resources/otel-collector/otel-collector-config.yaml). |
| 45 | +
|
| 46 | +### Add a Dockerfile in the root of the repository |
| 47 | +The below [Dockerfile](https://github.com/autometrics-dev/autometrics-shared/blob/main/deployment-guides-resources/otel-collector/Dockerfiles) uses the base Otel Collector image and loads our configuration |
| 48 | +
|
| 49 | +```Dockerfile |
| 50 | +FROM otel/opentelemetry-collector:latest |
| 51 | + |
| 52 | +ADD otel-collector-config.yaml /etc/otelcol/config.yaml |
| 53 | +``` |
| 54 | + |
| 55 | +### Create a Railway service from a GitHub repo |
| 56 | +Create a Railway service from our newly created GitHub repo, it should pick up the configuration from the Dockerfile. |
| 57 | + |
| 58 | +### Add a PORT environment variable |
| 59 | +In order to make the OTel Collector accessible to other services in your Railway project or externally (optional) - you need to tell Railway which port to listen on using the "magic" PORT environment variable. |
| 60 | + |
| 61 | +To add it go to Service > Variables and add just the port number. |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +### Configure application code |
| 66 | + Utilize the 'push' capabilities in your chosen Autometrics language library, detailed in each language's quickstart section. |
| 67 | + When initializing the OTLP exporter, specify the endpoint for your deployed OTel Collector. |
| 68 | + Note that the OTel Collector expects metrics at **/v1/metrics**. |
| 69 | + |
| 70 | + |
| 71 | + If you have deployed the OTel collector with the port set to 4318 your internal Railway endpoint should look something like this: |
| 72 | + |
| 73 | + `http://name-of-collector-service:4318/v1/metrics` |
| 74 | + |
| 75 | +</Steps> |
0 commit comments