#!/usr/bin/env bash main() { # Required to add the AAS metadata export DD_AZURE_APP_SERVICES=1 export DD_HOSTNAME="none" # Remote Config does not work in AAS. It must be disabled. export DD_REMOTE_CONFIGURATION_ENABLED=false if [ -z "${DD_CHDIR}" ]; then CURRENT_DIR=$(pwd) else CURRENT_DIR="$(pwd)/${DD_CHDIR}" fi echo "Set application directory as ${CURRENT_DIR}" echo "Setting Datadog environment variables" setEnvVars echo "Creating and switching to the Datadog directory at ${DD_DIR}" mkdir -p "${DD_DIR}" && cd "${DD_DIR}" || return echo "Adding Runtime specific dependencies" getRuntimeDependencies echo "Getting the Datadog binaries" getBinaries echo "Starting the appropriate Datadog binaries" startBinaries case "$WEBSITE_STACK" in "NODE") setUpNodeEnv;; "DOTNETCORE") setUpDotnetEnv;; "JAVA") setUpJavaEnv;; "PHP") setupPHPEnv;; "PYTHON") setUpPyEnv;; "TOMCAT") setUpJavaEnv;; "*") echo "Unsupported runtime. Exiting Datadog startup" return;; esac echo "Removing any previous installations" find "${DD_DIR}"/v* -type d | grep -v "${DD_BINARY_DIR}" | xargs -r rm -r echo "Completed Datadog setup" } setEnvVars() { if [ -z "${DD_DIR}" ]; then DD_DIR="/home/datadog" fi if [ -z "${DD_BINARIES_URL}" ]; then DD_BINARIES_URL="https://github.com/DataDog/datadog-aas-linux/releases/download" fi if [ -z "${DD_AAS_LINUX_VERSION}" ]; then DD_AAS_LINUX_VERSION="v1.10.3" fi if [ -z "${DD_BINARY_DIR}" ]; then DD_BINARY_DIR="${DD_DIR}/${DD_AAS_LINUX_VERSION}" fi if [ -z "${DD_TRACE_ENABLED}" ]; then DD_TRACE_ENABLED="true" fi if [ -z "${DD_CUSTOM_METRICS_ENABLED}" ]; then DD_CUSTOM_METRICS_ENABLED="false" fi if [ -z "${DD_LOG_LEVEL}" ]; then DD_LOG_LEVEL="error" fi } getRuntimeDependencies() { # If we are in Java, Tomcat or PHP stacks, we need to find the linux type to install unzip and curl if [ "${WEBSITE_STACK}" == "JAVA" ] || [ "${WEBSITE_STACK}" == "TOMCAT" ] || [ "${WEBSITE_STACK}" == "PHP" ]; then LINUX_VERSION_NAME=$(. "/etc/os-release"; echo "$ID") if [ "${LINUX_VERSION_NAME}" == "ubuntu" ] || [ "${LINUX_VERSION_NAME}" == "debian" ]; then apt-get update apt-get install -y unzip apt-get install -y curl else apk add curl apk add libc6-compat fi fi } getBinaries() { #Check if we have already installed this version if [ ! -d "${DD_BINARY_DIR}" ]; then FILE="datadog-aas-${DD_AAS_LINUX_VERSION}.zip" echo "Downloading Datadog AAS binary ${DD_BINARIES_URL}/${DD_AAS_LINUX_VERSION}/${FILE}" if curl -L --fail "${DD_BINARIES_URL}/${DD_AAS_LINUX_VERSION}/${FILE}" -o "${FILE}"; then echo "Unzipping files" unzip "${FILE}" || return echo "Removing zip file" rm "${FILE}" # The trace agent requires a datadog.yaml touch "${DD_BINARY_DIR}/datadog.yaml" else echo "Failed to download the Datadog binary succesfully." return fi else echo "Version ${DD_AAS_LINUX_VERSION} of Datadog AAS previously installed" fi } startBinaries() { BINARY_DIR="${DD_DIR}/${DD_AAS_LINUX_VERSION}" if [ "${DD_TRACE_ENABLED}" = "true" ]; then echo "Starting the trace agent" eval "${DD_BINARY_DIR}/process_manager ${BINARY_DIR}/trace-agent run -c ${DD_BINARY_DIR} &" fi if [ "$DD_CUSTOM_METRICS_ENABLED" = "true" ] || [ "$DD_RUNTIME_METRICS_ENABLED" = "true" ]; then echo "Starting DogStatsD" eval "${DD_BINARY_DIR}/process_manager ${BINARY_DIR}/dogstatsd start &" fi } setUpNodeEnv() { echo "Setting up Datadog tracing for Node" echo "Installing Node tracer" yarn add dd-trace || return ORIG_NODE_OPTIONS=$NODE_OPTIONS export NODE_OPTIONS="--require=${DD_DIR}/node_modules/dd-trace/init ${ORIG_NODE_OPTIONS}" # confirm updates to NODE_OPTIONS node --help >/dev/null || (export NODE_OPTIONS="${ORIG_NODE_OPTIONS}" && return) } setUpDotnetEnv() { echo "Setting up Datadog tracing for .NET" if [ -z "${DD_DOTNET_TRACER_VERSION}" ]; then DD_DOTNET_TRACER_VERSION=2.44.0 fi # Set a tracer directory to avoid issues with unarchiving DD_DOTNET_TRACER_DIR="${DD_DIR}/dd-trace-dotnet-v${DD_DOTNET_TRACER_VERSION}" # If the tracer directory doesn't exist, then we can download the tracer. if [ ! -d "$DD_DOTNET_TRACER_DIR" ]; then mkdir -p "${DD_DOTNET_TRACER_DIR}" && cd "${DD_DOTNET_TRACER_DIR}" || return DD_DOTNET_TRACER_FILE=datadog-dotnet-apm-${DD_DOTNET_TRACER_VERSION}.tar.gz DD_DOTNET_TRACER_URL=https://github.com/DataDog/dd-trace-dotnet/releases/download/v${DD_DOTNET_TRACER_VERSION}/${DD_DOTNET_TRACER_FILE} echo "Installing .NET tracer from ${DD_DOTNET_TRACER_URL}" if curl -L --fail "${DD_DOTNET_TRACER_URL}" -o "${DD_DOTNET_TRACER_FILE}"; then tar -xzf "${DD_DOTNET_TRACER_FILE}" || return else echo "Downloading the tracer was unsuccessful" return fi fi export CORECLR_ENABLE_PROFILING=1 export CORECLR_PROFILER="{846F5F1C-F9AE-4B07-969E-05C26BC060D8}" export CORECLR_PROFILER_PATH="${DD_DOTNET_TRACER_DIR}/Datadog.Trace.ClrProfiler.Native.so" export DD_DOTNET_TRACER_HOME="${DD_DOTNET_TRACER_DIR}" if [[ "${DD_PROFILING_ENABLED:-,,}" == "1" ]]; then echo "Profiler is enabled." export LD_PRELOAD="${DD_DOTNET_TRACER_DIR}/continuousprofiler/Datadog.Linux.ApiWrapper.x64.so ${LD_PRELOAD}" # Allow the profiler to add this to its tag export WEBSITE_OS="linux" fi } setUpJavaEnv() { echo "Setting up Datadog tracing for Java" if [ -z "${DD_JAVA_TRACER_VERSION}" ]; then DD_JAVA_TRACER_VERSION=1.26.1 fi echo "Using version ${DD_JAVA_TRACER_VERSION} of the JAVA tracer" DD_JAVA_TRACER_FILE="dd-java-agent-${DD_JAVA_TRACER_VERSION}.jar" DD_JAVA_TRACER_URL="https://github.com/DataDog/dd-trace-java/releases/download/v${DD_JAVA_TRACER_VERSION}/${DD_JAVA_TRACER_FILE}" echo "Installing JAVA tracer from ${DD_JAVA_TRACER_URL}" if ! curl -L --fail "${DD_JAVA_TRACER_URL}" -o "${DD_JAVA_TRACER_FILE}"; then echo "Downloading the tracer was unsuccessful" return fi echo "Adding the JAVA tracer to the startup command" DD_JAVAAGENT="-javaagent:${DD_DIR}/${DD_JAVA_TRACER_FILE}" if [ "${WEBSITE_STACK}" == "TOMCAT" ]; then export JAVA_OPTS="${JAVA_OPTS} ${DD_JAVAAGENT}" else DD_START_APP=$(echo "${DD_START_APP//-jar/$DD_JAVAAGENT -jar}") fi } setupPHPEnv() { echo "Setting up Datadog tracing for PHP" if [ -z "${DD_PHP_TRACER_VERSION}" ]; then DD_PHP_TRACER_VERSION=0.96.0 fi DD_PHP_TRACER_URL=https://github.com/DataDog/dd-trace-php/releases/download/${DD_PHP_TRACER_VERSION}/datadog-setup.php echo "Installing PHP tracer from ${DD_PHP_TRACER_URL}" if curl -LO --fail "${DD_PHP_TRACER_URL}"; then eval "php datadog-setup.php --php-bin=all" else echo "Downloading the tracer was unsuccessful" return fi } setUpPyEnv() { echo "Setting up Datadog tracing for Python" if [ -z "${DD_PYTHON_TRACER_VERSION}" ]; then DD_PYTHON_TRACER_VERSION=2.4.0 fi pip install ddtrace # append ddtrace-run command to original start command DD_START_APP="ddtrace-run ${DD_START_APP}" } main echo "Executing start command: \"${DD_START_APP}\"" cd "${CURRENT_DIR}" eval "${DD_START_APP}"