Deploying OTel Collector on Railway

OTel Collector is available as an official Docker image (opens in a new tab) on Docker Hub. We can use it as a base image and layer in our configuration before deploying it on Railway You find the resources for the Deployment here (opens in a new tab).

Create a GitHub repository and a Otel Collector configuration file

To set up the OTel Collector instance, utilize Railway's repository deployment and leverage Dockerfile features (opens in a new tab). Create a file named otel-collector-config.yaml to specify a receiver endpoint for sending metrics from the instrumented application

otel-collector-config.yml
receivers:
  otlp:
    protocols:
      http:
        endpoint: 0.0.0.0:4318
 
  pipelines:
    metrics:
      receivers: [otlp]

Optional: Export metrics

As the OTel Collector acts as a proxy, we can define an exporter to send metrics. In this example, metrics are pushed to Prometheus's remote write receiver (opens in a new tab). 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.

otel-collector-config.yml
exporters:
  prometheusremotewrite:
    endpoint: http://prometheus:9090/api/v1/write
    resource_to_telemetry_conversion:
      enabled: true # Convert resource attributes to
 
  pipelines:
    metrics:
      exporters: [prometheusremotewrite]

You can find the full otel-config.yaml here (opens in a new tab).

Add a Dockerfile in the root of the repository

The below Dockerfile (opens in a new tab) uses the base Otel Collector image and loads our configuration

FROM otel/opentelemetry-collector:latest

ADD otel-collector-config.yaml /etc/otelcol/config.yaml

Create a Railway service from a GitHub repo

Create a Railway service from our newly created GitHub repo, it should pick up the configuration from the Dockerfile.

Add a PORT environment variable

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.

To add it go to Service > Variables and add just the port number. Railway add PORT variable for Otel Collector

Configure application code

Utilize the 'push' capabilities in your chosen Autometrics language library, detailed in each language's quickstart section. When initializing the OTLP exporter, specify the endpoint for your deployed OTel Collector. Note that the OTel Collector expects metrics at /v1/metrics.

If you have deployed the OTel collector with the port set to 4318 your internal Railway endpoint should look something like this:

http://name-of-collector-service:4318/v1/metrics