Skip to content

Instantly share code, notes, and snippets.

@kenzo0107 kenzo0107/README.md
Last active Jun 19, 2019

Embed
What would you like to do?
post rails metrics to datadog

rails-metrics-to-datadog

What's this project

This project provides an shell script that post rails metrics to datadog.

Usage

cd <path-to-rails>

env PROJECT=${PROJECT} DATADOG_API_KEY=${DATADOG_API_KEY} RAILS_ENV=${RAILS_ENV} rails_metrics_to_datadog.sh codestats
env PROJECT=${PROJECT} DATADOG_API_KEY=${DATADOG_API_KEY} RAILS_ENV=${RAILS_ENV} rails-metrics-to-datadog.sh dependent_gem_count
env PROJECT=${PROJECT} DATADOG_API_KEY=${DATADOG_API_KEY} RAILS_ENV=${RAILS_ENV} rails_metrics_to_datadog.sh bench_rails_runner
  • via docker
- sudo docker run --rm -v ${PWD}/:<workdir>/ -e PROJECT=${PROJECT} -e DATADOG_API_KEY=${DATADOG_API_KEY} -e RAILS_ENV=${RAILS_ENV} ${AWS_ACCOUNT_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/${ENV}-${PROJECT}-rails:${IMAGE_TAG} <workdir>/rails_metrics_to_datadog.sh codestats
- sudo docker run --rm -v ${PWD}/:<workdir>/ -e PROJECT=${PROJECT} -e DATADOG_API_KEY=${DATADOG_API_KEY} -e RAILS_ENV=${RAILS_ENV} ${AWS_ACCOUNT_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/${ENV}-${PROJECT}-rails:${IMAGE_TAG} <workdir>y/rails_metrics_to_datadog.sh dependent_gem_count
- sudo docker run --rm -v ${PWD}/:<workdir>/ -e PROJECT=${PROJECT} -e DATADOG_API_KEY=${DATADOG_API_KEY} -e RAILS_ENV=${RAILS_ENV} ${AWS_ACCOUNT_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/${ENV}-${PROJECT}-rails:${IMAGE_TAG} <workdir>/rails_metrics_to_datadog.sh bench_rails_runner
#!/bin/sh
post_metrics_datadog() {
metrics="$1"
val="$2"
currenttime=$(date +%s)
curl -s -X POST -H "Content-type: application/json" \
-d "{ \"series\" :
[{\"metric\":\"${metrics}\",
\"points\":[[$currenttime, ${val}]],
\"type\":\"gauge\",
\"host\":\"${PROJECT}\"}]
}" \
"https://api.datadoghq.com/api/v1/series?api_key=${DATADOG_API_KEY}"
}
codestats() {
r=$(bundle exec rake stats | awk -F\| 'NR>3{if($2!="") print $2,":",$3}' | sed 's| ||g')
IFS='
'
for i in $(echo "${r}"); do
key=$(echo "${i}" | awk -F: '{print $1}')
val=$(echo "${i}" | awk -F: '{print $2}')
echo "key: ${key} => val: ${val}"
post_metrics_datadog "rails.codestats.${key}" "${val}"
done
}
dependent_gem_count() {
r=$(gem dependency | grep '^Gem' | wc -l | sed -e 's/[^0-9]//g')
echo "$r"
post_metrics_datadog "rails.dependency.count" $r
}
bench_rails_runner() {
chmod +x ./bin/rails
echo "=== Warming disk cache, ... ==="
time ./bin/rails r '1;'
echo "=== Benchmark of 'rails r' 3 times ! ==="
for i in `seq 1 3`
do
# time の出力は std error 扱いの為、 標準出力へリダイレクト
r=`(time -p ./bin/rails r '1;') 2>&1 | grep real | awk '{print $2}'`
echo "load_time:$r"
post_metrics_datadog "rails.runner.load_time" "$r"
done
}
case "$1" in
codestats ) codestats ;;
dependent_gem_count ) dependent_gem_count ;;
bench_rails_runner ) bench_rails_runner ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.