> ## Documentation Index
> Fetch the complete documentation index at: https://www.aptible.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Collect Prometheus Metrics from Apps

> Learn how to collect Prometheus metrics from containerized apps running on Aptible.

When an App exposes Prometheus metrics on Aptible, a traditional external Prometheus scraper usually cannot collect accurate per-container metrics. Aptible Containers are ephemeral and sit behind Endpoints, so an external scraper reaches the Endpoint instead of individual Containers. With multiple Containers, that can produce round-robin samples instead of a complete per-container view.

Use a push-based collection pattern instead: each Container sends its metrics to a collector App running on Aptible, and that collector forwards metrics to your monitoring backend.

## Recommended Architecture

```text theme={null}
App Container -> Telegraf Collector App -> Monitoring Backend
```

This pattern works well with tools such as Telegraf because the collector can receive Prometheus-formatted metrics and forward them to an output destination like Datadog, InfluxDB, or another metrics backend.

## Set Up Push-Based Collection

<Steps>
  <Step title="Deploy a metrics collector">
    Deploy a separate App in the same Environment to run Telegraf or another collector that can receive Prometheus-formatted metrics.
  </Step>

  <Step title="Configure the collector input">
    Configure Telegraf with the Prometheus listener input, such as `[[inputs.prometheus_listener]]`, so it can receive metrics pushed by your app Containers.
  </Step>

  <Step title="Configure the collector output">
    Configure the output plugin for your monitoring backend, such as `[[outputs.datadog]]` for Datadog.
  </Step>

  <Step title="Push metrics from each app Container">
    Update your application to send its local Prometheus metrics endpoint to the collector on a regular interval.
  </Step>
</Steps>

## Ruby Example

This example reads metrics from a local Prometheus endpoint and posts them to a Telegraf collector:

```ruby theme={null}
require "net/http"

Thread.new do
  loop do
    begin
      metrics = Net::HTTP.get(URI("http://0.0.0.0:9394/metrics"))
      Net::HTTP.post(
        URI("https://your-telegraf-app.aptible.in:9126/metrics"),
        metrics,
        { "Content-Type" => "text/plain" }
      )
    rescue => e
      puts "Metrics push failed: #{e.message}"
    end

    sleep 15
  end
end
```

For Ruby applications, the `prometheus_exporter` gem can help collect and format metrics before you push them to your collector.

<Tip>
  Keep your push interval aligned with the retention and alerting needs of your monitoring backend. Shorter intervals improve visibility but increase network traffic and backend ingest volume.
</Tip>
