Last active
January 15, 2021 10:56
-
-
Save kigster/029d7e7a6e231f6db6d7ce7553868241 to your computer and use it in GitHub Desktop.
This file should be sourced in before running any Datadog-Instrumented application. It sets several automatic tags such as @username, @developer, @osname, @Branch and more so that multiple engineers can differentiate between their data. It also open the browser with the @username filter enabled.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# vim: ft=bash | |
# | |
# Author: Konstantin Gredeskoul, https://github.com/kigster | |
# | |
# Copyright © 2021, MIT License | |
# | |
# This file should be used by applications wanting to stream APM information about the runtime behavior of the | |
# app to their Datadog APM account. | |
# | |
# You need to set two environment variables to make this work: | |
# | |
# 1. DATADOG_API_KEY — create a new key here — https://app.datadoghq.com/access/application-keys | |
# 2. DATADOG_HOST — is the host that runs Datadog Agent (defaults to localhost). | |
# | |
# You can run the Agent locally, and install it on the majority of supported OSes, such as the Mac or Ubuntu: | |
# | |
# Mac OS-X -> https://app.datadoghq.com/account/settings#agent/mac | |
# Ubuntu -> https://app.datadoghq.com/account/settings#agent/ubuntu | |
# | |
# If you run the agent elsewhere, you will need to make sure to enable the configuration setting on the | |
# remote agent host in its /etc/datadog-agent/datadog.yml file to accept APM traffic on behalf of others: | |
# | |
# apm_non_local_traffic: true | |
# | |
# Once you have the above information, create a top level git-ignored file in your project, called `.envrc.local` | |
# | |
# export DATADOG_API_KEY=.... | |
# If the agent is not running locally, use the | |
# export DATADOG_HOST=.... | |
export blue='\e[0;30m\e[42m' | |
export red='\e[1;38m\e[41m' | |
export bldred='\e[1;31m' | |
export bldgrn='\e[1;32m' | |
export bldylw='\e[1;33m' | |
export bldpur='\e[1;35m' | |
export bldblu='\e[1;34m' | |
export undblu='\e[4;34m' | |
export clr='\e[0;0m' | |
[[ -z ${DATADOG_API_KEY} && -f .envrc.local ]] && source ".envrc.local" | |
export DD_ENV=dev | |
export DD_API_KEY="${DD_API_KEY:-${DATADOG_API_KEY}}" | |
export DD_TRACE_AGENT_HOSTNAME=${DATADOG_HOST:-"localhost"} | |
export DATADOG_ENABLED=true | |
okay() { | |
printf "\n${blue} (INFO) — %s ${clr}${bldgrn}${clr}\n\n" "$*" | |
} | |
err() { | |
printf "\n${red} (ERROR) — %s ${clr}${bldred}${clr}\n\n" "$*" | |
} | |
datadog_set_tags() { | |
# Configure datadog tags so that we can separate our APM logs from | |
# other developers within the "dev" env. | |
export NAME="$(git config --global --get user.name)" | |
declare -a tags | |
tags=( | |
hostname:"${HOSTNAME:=$(hostname)}" | |
username:"${USER}" | |
os:"$(uname -s)" | |
developer:"${NAME/ /}" | |
environment:"${DD_ENV}" | |
branch:"$(git rev-parse --abbrev-ref HEAD)" | |
) | |
export DD_TAGS="$(echo "${tags[*]}" | tr ' ' ',')" | |
export DD_TAGS="$(echo "${DD_TAGS}" | tr -d ' ')" | |
local key | |
if [[ -n ${DD_API_KEY} ]]; then | |
key="••••••••••••••••••••" | |
fi | |
okay "Printing Datadog Tags/Facets:" | |
echo | |
for tag in "${tags[@]}" key:${key} ; do | |
t=${tag/:*/} | |
v=${tag/*:/} | |
printf "${bldpur}%15s = ${bldylw}%s\n" "$t" "$v" | |
done | |
printf "${clr}\n" | |
bash -c "sleep 5 && open \"https://app.datadoghq.com/apm/traces?paused=false&query=env:dev%20%40username:${USER}\"" & | |
return 0 | |
} | |
function datadog_check_env() { | |
[[ -n "${DD_API_KEY}" ]] && return 0 | |
[[ -z ${DD_API_KEY} ]] && err "Your Datadog API key (DATADOG_API_KEY) is missing." | |
printf "${bldylw}" | |
echo "Please set DATADOG_API_KEY variable, eg:" | |
echo | |
echo " 1. echo 'export DATADOG_API_KEY=XXXXXX' >>.envrc.local" | |
echo " 2. echo 'export DATADOG_API_KEY=XXXXXX' >>.~/.bash_profile" | |
echo | |
echo "You can create a new one at the following link:" | |
echo | |
printf "↪ ${undblu}https://app.datadoghq.com/access/application-keys${clr}\n" | |
echo | |
return 1 | |
} | |
( | |
datadog_check_env && \ | |
datadog_set_tags | |
)>&2 || exit 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment