-
Notifications
You must be signed in to change notification settings - Fork 2
/
install_test_visibility.sh
executable file
·263 lines (217 loc) · 8.47 KB
/
install_test_visibility.sh
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
# Unless explicitly stated otherwise all files in this repository are licensed
# under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/)
# Copyright 2024-present Datadog, Inc.
# This script installs Datadog tracing libraries for the specified languages
# and prints to standard output the environment variables that need to be set for enabling Test Visibility.
# The variables are printed in the following format: variableName=variableValue
set -e
ARTIFACTS_FOLDER="${DD_TRACER_FOLDER:-$(pwd)/.datadog}"
if ! mkdir -p $ARTIFACTS_FOLDER; then
>&2 echo "Error: Cannot create folder: $ARTIFACTS_FOLDER"
return 1
fi
install_java_tracer() {
if [ -z "$DD_SET_TRACER_VERSION_JAVA" ]; then
DD_SET_TRACER_VERSION_JAVA=$(get_latest_java_tracer_version)
fi
local filepath_tracer
filepath_tracer="$ARTIFACTS_FOLDER/dd-java-agent.jar"
local filepath_checksum
filepath_checksum="$ARTIFACTS_FOLDER/dd-java-agent.jar.sha256"
download_file "https://repo1.maven.org/maven2/com/datadoghq/dd-java-agent/$DD_SET_TRACER_VERSION_JAVA/dd-java-agent-$DD_SET_TRACER_VERSION_JAVA.jar" $filepath_tracer
download_file "https://repo1.maven.org/maven2/com/datadoghq/dd-java-agent/$DD_SET_TRACER_VERSION_JAVA/dd-java-agent-$DD_SET_TRACER_VERSION_JAVA.jar.sha256" $filepath_checksum
if ! verify_checksum "$(cat $filepath_checksum)" "$filepath_tracer"; then
return 1
fi
case $DD_INSTRUMENTATION_BUILD_SYSTEM_JAVA in
gradle)
echo "GRADLE_OPTS=-Dorg.gradle.jvmargs=-javaagent:$filepath_tracer $GRADLE_OPTS"
;;
maven)
echo "MAVEN_OPTS=-javaagent:$filepath_tracer $MAVEN_OPTS"
;;
*)
local updated_java_tool_options="-javaagent:$filepath_tracer $JAVA_TOOL_OPTIONS"
if [ ${#updated_java_tool_options} -le 1024 ]; then
echo "JAVA_TOOL_OPTIONS=$updated_java_tool_options"
else
>&2 echo "Error: Cannot apply Java instrumentation: updated JAVA_TOOL_OPTIONS would exceed 1024 characters"
return 1
fi
;;
esac
echo "DD_TRACER_VERSION_JAVA=$(command -v java >/dev/null 2>&1 && java -jar $filepath_tracer || unzip -p $filepath_tracer META-INF/MANIFEST.MF | grep -i implementation-version | cut -d' ' -f2)"
}
verify_checksum() {
if command -v sha256sum >/dev/null 2>&1; then
if ! echo "$1 $2" | sha256sum --quiet -c -; then
return 1
fi
elif command -v shasum >/dev/null 2>&1; then
if ! echo "$1 $2" | shasum --quiet -a 256 -c -; then
return 1
fi
else
>&2 echo "Error: Neither sha256sum nor shasum is installed."
return 1
fi
}
get_latest_java_tracer_version() {
local filepath_metadata
filepath_metadata="$ARTIFACTS_FOLDER/maven-metadata.xml"
local filepath_checksum
filepath_checksum="$ARTIFACTS_FOLDER/maven-metadata.xml.sha256"
download_file "https://repo1.maven.org/maven2/com/datadoghq/dd-java-agent/maven-metadata.xml" $filepath_metadata
download_file "https://repo1.maven.org/maven2/com/datadoghq/dd-java-agent/maven-metadata.xml.sha256" $filepath_checksum
if ! verify_checksum "$(cat $filepath_checksum)" "$filepath_metadata"; then
return 1
fi
local java_tracer_version
java_tracer_version=$(grep -o "<latest>.*</latest>" $filepath_metadata | sed -e 's/<[^>]*>//g')
rm $filepath_metadata $filepath_checksum
echo "$java_tracer_version"
}
download_file() {
local url=$1
local filepath=$2
if command -v curl >/dev/null 2>&1; then
curl -Lo "$filepath" "$url"
elif command -v wget >/dev/null 2>&1; then
wget -O "$filepath" "$url"
else
>&2 echo "Error: Neither wget nor curl is installed."
return 1
fi
}
install_js_tracer() {
if ! command -v npm >/dev/null 2>&1; then
>&2 echo "Error: npm is not installed."
return 1
fi
if ! command -v node >/dev/null 2>&1; then
>&2 echo "Error: node is not installed."
return 1
fi
if ! is_node_version_compliant; then
>&2 echo "Error: node v18.0.0 or newer is required, got $(node -v)"
return 1
fi
# set location for installing global packages (the script may not have the permissions to write to the default one)
export NPM_CONFIG_PREFIX=$ARTIFACTS_FOLDER
# install dd-trace as a "global" package
# (otherwise, doing SCM checkout might rollback the changes to package.json, and any subsequent `npm install` calls will result in removing the package)
if ! npm install -g dd-trace${DD_SET_TRACER_VERSION_JS:+@$DD_SET_TRACER_VERSION_JS} >&2; then
>&2 echo "Error: Could not install dd-trace for JS"
return 1
fi
# Github Actions prohibit setting NODE_OPTIONS
local dd_trace_ci_init_path="$ARTIFACTS_FOLDER/lib/node_modules/dd-trace/ci/init"
local dd_trace_register_path="$ARTIFACTS_FOLDER/lib/node_modules/dd-trace/register.js"
if ! is_github_actions; then
echo "NODE_OPTIONS=$NODE_OPTIONS -r $dd_trace_ci_init_path"
else
echo "DD_TRACE_PACKAGE=$dd_trace_ci_init_path"
fi
# We can't set the --import flag directly in NODE_OPTIONS since it's only compatible from Node.js>=20.6.0 and Node.js>=18.19,
# not even if !is_github_actions.
# Additionally, it's not useful for test frameworks other than vitest, which is ESM first.
echo "DD_TRACE_ESM_IMPORT=$dd_trace_register_path"
echo "DD_TRACER_VERSION_JS=$(npm list -g dd-trace | grep dd-trace | awk -F@ '{print $2}')"
}
is_node_version_compliant() {
local node_version
node_version=$(node -v | cut -d 'v' -f 2)
local major_node_version
major_node_version=$(echo $node_version | cut -d '.' -f 1)
if [ "$major_node_version" -lt 18 ]; then
return 1
fi
}
is_github_actions() {
if [ -z "$GITHUB_ACTION" ]; then
return 1
fi
}
install_python_tracer() {
if ! command -v pip >/dev/null 2>&1; then
>&2 echo "Error: pip is not installed."
return 1
fi
python -m venv .dd_civis_env >&2
source .dd_civis_env/bin/activate >&2
if ! pip install -U ddtrace${DD_SET_TRACER_VERSION_PYTHON:+==$DD_SET_TRACER_VERSION_PYTHON} coverage >&2; then
>&2 echo "Error: Could not install ddtrace for Python"
return 1
fi
local dd_trace_path
dd_trace_path=$(pip show ddtrace | grep Location | awk '{print $2}')
if ! [ -d $dd_trace_path ]; then
>&2 echo "Error: Could not determine ddtrace package location (tried $dd_trace_path)"
return 1
fi
local coverage_path
coverage_path=$(pip show coverage | grep Location | awk '{print $2}')
if ! [ -d $coverage_path ]; then
>&2 echo "Error: Could not determine coverage package location (tried $coverage_path)"
return 1
fi
echo "PYTHONPATH=$dd_trace_path:$coverage_path:$PYTHONPATH"
echo "PYTEST_ADDOPTS=--ddtrace $PYTEST_ADDOPTS"
echo "DD_TRACER_VERSION_PYTHON=$(pip show ddtrace | grep Version | cut -d ' ' -f2)"
deactivate >&2
}
install_dotnet_tracer() {
if ! command -v dotnet >/dev/null 2>&1; then
>&2 echo "Error: dotnet is not installed."
return 1
fi
if ! dotnet tool update --tool-path $ARTIFACTS_FOLDER dd-trace ${DD_SET_TRACER_VERSION_DOTNET:+--version $DD_SET_TRACER_VERSION_DOTNET} >&2; then
>&2 echo "Error: Could not install dd-trace for .NET"
return 1
fi
if [ -z "$DD_API_KEY" ]; then
>&2 echo "Error: dd-trace for .NET configuration requires DD_API_KEY to be set"
return 1
fi
# Using "jenkins" for now, as it outputs the env vars in a provider-agnostic format.
# Grepping to filter out lines that are not environment variables
DD_CIVISIBILITY_AGENTLESS_ENABLED=true $ARTIFACTS_FOLDER/dd-trace ci configure jenkins | grep '='
echo "DD_TRACER_VERSION_DOTNET=$(dotnet tool list --tool-path $ARTIFACTS_FOLDER | grep dd-trace | awk '{print $2}')"
}
# set common environment variables
echo "DD_CIVISIBILITY_ENABLED=true"
echo "DD_CIVISIBILITY_AGENTLESS_ENABLED=true"
if [ -z "$DD_ENV" ]; then
echo "DD_ENV=ci"
fi
# install tracer libraries
if [ -n "$DD_CIVISIBILITY_INSTRUMENTATION_LANGUAGES" ]; then
if [ "$DD_CIVISIBILITY_INSTRUMENTATION_LANGUAGES" = "all" ]; then
DD_CIVISIBILITY_INSTRUMENTATION_LANGUAGES="java js python dotnet"
fi
for lang in $( echo "$DD_CIVISIBILITY_INSTRUMENTATION_LANGUAGES" )
do
case $lang in
java)
install_java_tracer
;;
js)
install_js_tracer
;;
python)
install_python_tracer
;;
dotnet)
install_dotnet_tracer
;;
*)
>&2 echo "Unknown language: $lang. Must be one of: java, js, python, dotnet"
exit 1;
;;
esac
done
else
>&2 echo "Error: DD_CIVISIBILITY_INSTRUMENTATION_LANGUAGES environment variable should be set to all or a space-separated subset of java, js, python, dotnet"
exit 1;
fi