-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_test_visibility.sh
executable file
·186 lines (154 loc) · 5.58 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
# 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
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() {
local url="https://dtdg.co/latest-java-tracer"
local filepath="$ARTIFACTS_FOLDER/dd-java-agent.jar"
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
local updated_java_tool_options="-javaagent:$filepath $JAVA_TOOL_OPTIONS"
if [ ${#updated_java_tool_options} -le 1024 ]; then
echo "JAVA_TOOL_OPTIONS=$updated_java_tool_options"
echo "DD_TRACER_VERSION_JAVA=$(java -jar $filepath)"
else
>&2 echo "Error: Cannot apply Java instrumentation: updated JAVA_TOOL_OPTIONS would exceed 1024 characters"
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 >&2; then
>&2 echo "Error: Could not install dd-trace for JS"
return 1
fi
# Github Actions prohibit setting NODE_OPTIONS
local dd_trace_path="$ARTIFACTS_FOLDER/lib/node_modules/dd-trace/ci/init"
if ! is_github_actions; then
echo "NODE_OPTIONS=$NODE_OPTIONS -r $dd_trace_path"
else
echo "DD_TRACE_PACKAGE=$dd_trace_path"
fi
echo "DD_TRACER_VERSION_JS=$(npm info dd-trace version)"
}
is_node_version_compliant() {
local node_version=$(node -v | cut -d 'v' -f 2)
local 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 coverage >&2; then
>&2 echo "Error: Could not install ddtrace for Python"
return 1
fi
local 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=$(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 >&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