How to collect Prometheus metrics from containerized apps on Aptible

Last updated: March 29, 2025

When running containerized applications that expose Prometheus metrics on Aptible, traditional pull-based scraping methods aren't ideal due to the dynamic nature of containers. Here's how to effectively collect metrics from your containerized applications.

Why traditional scraping doesn't work well

Since containers on Aptible are ephemeral and sit behind a load balancer (Endpoint), external Prometheus scrapers can only reach the Endpoint rather than individual containers. This results in round-robin sampling across all containers rather than true per-container metrics.

Recommended Solution: Push-based Metrics Collection

Instead of pull-based scraping, implement a push model where each container actively sends its metrics to a central collector. Here's how to set it up:

  1. Deploy Telegraf as a separate app in your Aptible environment to act as a metrics collector

  2. Configure Telegraf with:

    • The Prometheus listener plugin ([[inputs.prometheus_listener]])

    • The appropriate output plugin (e.g., [[outputs.datadog]] for Datadog)

  3. Modify your application code to push metrics to the Telegraf instance

Example Implementation

Here's an example of how to push metrics from a Ruby application:

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

This approach provides near real-time, per-container visibility into your metrics, even in a dynamic containerized environment.

For Ruby applications, consider using the prometheus_exporter gem to help collect and format your metrics.