Skip to content

Instantly share code, notes, and snippets.

@romiras
Created April 21, 2024 10:08
Show Gist options
  • Save romiras/d4d339facd22f2d5ac7c717836ff719a to your computer and use it in GitHub Desktop.
Save romiras/d4d339facd22f2d5ac7c717836ff719a to your computer and use it in GitHub Desktop.
Debugging Datadog agent in Ruby on Rails app
Rails.application.routes.draw do
get '/health', to: proc { [200, {}, ['ok']] }, as: :app_health_check
get '/dd', to: proc {
port_is_open = Socket.tcp('localhost', 8126, connect_timeout: 5) { true } rescue false
res = "Datadog port is open: #{port_is_open}"
[200, {}, [res]]
}, as: :dd_health_check
end
@romiras
Copy link
Author

romiras commented Apr 21, 2024

Solution

Wait for Datadog agent has started and listening on a port 8126:

wait_for_datadog.sh:

#!/bin/env sh

max_attempts=20
attempts=0

while ! nc -z localhost 8126; do
  if [ $attempts -ge $max_attempts ]; then
    echo "Max attempts reached. Datadog agent did not start."
    exit 1
  fi

  echo "Waiting for Datadog agent to start... Attempt: $((attempts+1))"
  attempts=$((attempts+1))
  sleep 1
done

Procfile:

web: ./wait_for_datadog.sh && bundle exec puma -p ${PORT:-3000} -e production

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment