-
Notifications
You must be signed in to change notification settings - Fork 8
/
datadog_wrapper
272 lines (217 loc) · 8.88 KB
/
datadog_wrapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env bash
main() {
# Required to add the AAS metadata
export DD_AZURE_APP_SERVICES=1
export DD_APM_REMOTE_TAGGER=false
export DD_ENABLE_METADATA_COLLECTION=false
# 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.11"
fi
if [ -z "${DD_TRACE_LOG_DIRECTORY}" ]; then
DD_TRACE_LOG_DIRECTORY="/home/LogFiles/datadog/${DD_AAS_LINUX_VERSION}"
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
apk add gcompat
ln -sfv ld-linux-x86-64.so.2 /lib/libresolv.so.2
fi
fi
}
getBinaries() {
# Check if we have already installed this version or if we successfully downloaded binaries before
if [ ! -d "${DD_BINARY_DIR}" -o -z "$(ls -A "${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}"
echo "Creating logs directory at ${DD_TRACE_LOG_DIRECTORY}"
mkdir -p "${DD_TRACE_LOG_DIRECTORY}"
# patch the datadog yaml file to set the aas version for logging path for easier navigation
sed -i 's\{AAS_LINUX_VERSION}\'"$DD_AAS_LINUX_VERSION"'\g' "${DD_BINARY_DIR}"/datadog.yaml
# patch the dogstatsD yaml file to set the aas version for logging path for easier navigation
sed -i 's\{AAS_LINUX_VERSION}\'"$DD_AAS_LINUX_VERSION"'\g' "${DD_BINARY_DIR}"/dogstatsd.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() {
if [ "${DD_TRACE_ENABLED}" = "true" ]; then
echo "Starting the trace agent"
eval "${DD_BINARY_DIR}/process_manager ${DD_BINARY_DIR}/trace-agent run -c ${DD_BINARY_DIR}/datadog.yaml &"
fi
if [ "$DD_CUSTOM_METRICS_ENABLED" = "true" ] || [ "$DD_RUNTIME_METRICS_ENABLED" = "true" ]; then
echo "Starting DogStatsD"
eval "${DD_BINARY_DIR}/process_manager ${DD_BINARY_DIR}/dogstatsd start -c ${DD_BINARY_DIR}/dogstatsd.yaml &"
fi
}
setUpNodeEnv() {
NODE_RUNTIME_VERSION=$(node --version)
echo "Found runtime version ${NODE_RUNTIME_VERSION}"
echo "Setting up Datadog tracing for Node"
echo "Installing Node tracer"
local DD_NODE_TRACER_VERSION_4=4.49.0
local DD_NODE_TRACER_VERSION_5=5.25.0
if [[ "$NODE_RUNTIME_VERSION" =~ ^v16.* ]]; then
yarn add "dd-trace@$DD_NODE_TRACER_VERSION_4" || return
else
yarn add "dd-trace@$DD_NODE_TRACER_VERSION_5" || return
fi
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=3.5.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.42.0
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=1.4.2
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.16.0
fi
pip install "ddtrace==${DD_PYTHON_TRACER_VERSION}"
# 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}"