Skip to content

Commit 8ab52d0

Browse files
add telemetry data into run-ab-platform (#36933)
1 parent fd45159 commit 8ab52d0

File tree

1 file changed

+169
-12
lines changed

1 file changed

+169
-12
lines changed

run-ab-platform.sh

+169-12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Help()
3131
echo -e " -h --help Print this Help."
3232
echo -e " -x --debug Verbose mode."
3333
echo -e " -b --background Run docker compose up in detached mode."
34+
echo -e " --dnt Disable telemetry collection"
3435
echo -e ""
3536
}
3637

@@ -39,13 +40,133 @@ Help()
3940
docker_compose_debug_yaml="docker-compose.debug.yaml"
4041
dot_env=".env"
4142
dot_env_dev=".env.dev"
42-
flags="flags.yml"
43-
temporal_yaml="temporal/dynamicconfig/development.yaml"
44-
# any string is an array to POSIX shell. Space seperates values
43+
flags="flags.yml"
44+
temporal_yaml="temporal/dynamicconfig/development.yaml"
45+
# any string is an array to POSIX shell. Space separates values
4546
all_files="$docker_compose_yaml $docker_compose_debug_yaml $dot_env $dot_env_dev $flags $temporal_yaml"
4647

4748
base_github_url="https://raw.githubusercontent.com/airbytehq/airbyte-platform/v$VERSION/"
4849

50+
telemetrySuccess=false
51+
telemetrySessionULID=""
52+
telemetryUserULID=""
53+
telemetryEnabled=true
54+
# telemetry requires curl to be installed
55+
if ! command -v curl > /dev/null; then
56+
telemetryEnabled=false
57+
fi
58+
59+
# TelemetryConfig configures the telemetry variables and will disable telemetry if it cannot be configured.
60+
TelemetryConfig()
61+
{
62+
# only attempt to do anything if telemetry is not disabled
63+
if $telemetryEnabled; then
64+
telemetrySessionULID=$(curl -s http://ulid.abapp.cloud/ulid | xargs)
65+
66+
if [[ $telemetrySessionULID = "" || ${#telemetrySessionULID} -ne 26 ]]; then
67+
# if we still don't have a ulid, give up on telemetry data
68+
telemetryEnabled=false
69+
return
70+
fi
71+
72+
# if we have an analytics file, use it
73+
if test -f ~/.airbyte/analytics.yml; then
74+
telemetryUserULID=$(cat ~/.airbyte/analytics.yml | grep "anonymous_user_id" | cut -d ":" -f2 | xargs)
75+
fi
76+
# if the telemtery ulid is still undefined, attempt to create it and write the analytics file
77+
if [[ $telemetryUserULID = "" || ${#telemetryUserULID} -ne 26 ]]; then
78+
telemetryUserULID=$(curl -s http://ulid.abapp.cloud/ulid | xargs)
79+
if [[ $telemetryUserULID = "" || ${#telemetryUserULID} -ne 26 ]]; then
80+
# if we still don't have a ulid, give up on telemetry data
81+
telemetryEnabled=false
82+
else
83+
# we created a new ulid, write it out
84+
echo "Thanks you for using Airbyte!"
85+
echo "Anonymous usage reporting is currently enabled. For more information, please see https://docs.airbyte.com/telemetry"
86+
mkdir -p ~/.airbyte
87+
cat > ~/.airbyte/analytics.yml <<EOL
88+
# This file is used by Airbyte to track anonymous usage statistics.
89+
# For more information or to opt out, please see
90+
# - https://docs.airbyte.com/operator-guides/telemetry
91+
anonymous_user_id: $telemetryUserULID
92+
EOL
93+
fi
94+
fi
95+
fi
96+
}
97+
98+
# TelemetryDockerUp checks if the webapp container is in a running state. If it is it will send a successful event.
99+
# if after 10 minutes it hasn't succeeded, a failed event will be sent (or if the user terminates early, a failed event would
100+
# also be sent).
101+
#
102+
# Note this only checks if the webapp container is running, that doesn't actually mean the entire stack is up.
103+
# Some further refinement on this metric may be necessary.
104+
TelemetryDockerUp()
105+
{
106+
if ! $telemetryEnabled; then
107+
return
108+
fi
109+
110+
# for up to 600 seconds (10 minutes), check to see if the server services is in a running state
111+
end=$((SECONDS+600))
112+
while [ $SECONDS -lt $end ]; do
113+
webappState=$(docker compose ps --all --format "{{.Service}}:{{.State}}" 2>/dev/null | grep server | cut -d ":" -f2 | xargs)
114+
if [ "$webappState" = "running" ]; then
115+
TelemetrySend "success" "install"
116+
break
117+
fi
118+
sleep 1
119+
done
120+
121+
TelemetrySend "failed" "install" "webapp was not running within 600 seconds"
122+
}
123+
124+
readonly telemetryKey="kpYsVGLgxEqD5OuSZAQ9zWmdgBlyiaej"
125+
readonly telemetryURL="https://api.segment.io/v1/track"
126+
TelemetrySend()
127+
{
128+
if $telemetrySuccess; then
129+
# due to how traps work, we don't want to send a failure for exiting docker after we sent a success
130+
return
131+
fi
132+
133+
if $telemetryEnabled; then
134+
# start, failed, success
135+
local state=$1
136+
# install, uninstall
137+
local event=$2
138+
# optional error
139+
local err=${3:-""}
140+
141+
local now=$(date -u "+%Y-%m-%dT%H:%M:%SZ")
142+
local body=$(cat << EOL
143+
{
144+
"anonymousId":"$telemetryUserULID",
145+
"event":"$event",
146+
"properties": {
147+
"deployment_mode":"run_ab",
148+
"session_id":"$telemetrySessionULID",
149+
"state":"$state",
150+
"os":"$OSTYPE",
151+
"script_version":"$VERSION",
152+
"testing":"true",
153+
"error":"$err"
154+
},
155+
"timestamp":"$now",
156+
"writeKey":"$telemetryKey"
157+
}
158+
EOL
159+
)
160+
curl -s -o /dev/null -H "Content-Type: application/json" -X POST -d "$body" $telemetryURL
161+
if [[ $state = "success" ]]; then {
162+
telemetrySuccess=true
163+
}
164+
fi
165+
fi
166+
}
167+
168+
TelemetryConfig
169+
49170
############################################################
50171
# Download #
51172
############################################################
@@ -95,27 +216,53 @@ this_file_directory=$(dirname $0)
95216
# Run this from the / directory because we assume relative paths
96217
cd ${this_file_directory}
97218

219+
args=$@
220+
# Parse the arguments for specific flags before parsing for actions.
221+
for argument in $args; do
222+
case $argument in
223+
-h | --help)
224+
Help
225+
exit
226+
;;
227+
-b | --background)
228+
dockerDetachedMode="-d"
229+
;;
230+
--dnt)
231+
telemetryEnabled=false
232+
;;
233+
esac
234+
done
98235

99-
for argument in $@; do
236+
for argument in $args; do
100237
case $argument in
101238
-d | --download)
239+
TelemetrySend "start" "download"
240+
trap 'TelemetrySend "failed" "download" "sigint"' SIGINT
241+
trap 'TelemetrySend "failed" "download" "sigterm"' SIGTERM
102242
Download
243+
TelemetrySend "success" "download"
103244
exit
104245
;;
105246
-r | --refresh)
247+
TelemetrySend "start" "refresh"
248+
trap 'TelemetrySend "failed" "refresh" "sigint"' SIGINT
249+
trap 'TelemetrySend "failed" "refresh" "sigterm"' SIGTERM
106250
DeleteLocalAssets
107251
Download
108-
exit
109-
;;
110-
-h | --help)
111-
Help
252+
TelemetrySend "success" "refresh"
112253
exit
113254
;;
114255
-x | --debug)
115256
set -o xtrace # -x display every line before execution; enables PS4
116257
;;
258+
-h | --help)
259+
# noop, this was checked in the previous for loop
260+
;;
117261
-b | --background)
118-
dockerDetachedMode="-d"
262+
# noop, this was checked in the previous for loop
263+
;;
264+
--dnt)
265+
# noop, this was checked in the previous for loop
119266
;;
120267
*)
121268
echo "$argument is not a known command."
@@ -124,9 +271,11 @@ for argument in $@; do
124271
exit
125272
;;
126273
esac
127-
shift
128274
done
129275

276+
TelemetrySend "start" "install"
277+
trap 'TelemetrySend "failed" "install" "sigint"' SIGINT
278+
trap 'TelemetrySend "failed" "install" "sigterm"' SIGTERM
130279

131280
########## Pointless Banner for street cred ##########
132281
# Make sure the console is huuuge
@@ -148,12 +297,13 @@ fi
148297
########## Dependency Check ##########
149298
if ! docker compose version >/dev/null 2>/dev/null; then
150299
echo -e "$red_text""docker compose v2 not found! please install docker compose!""$default_text"
300+
TelemetrySend "failed" "install" "docker compose not installed"
151301
exit 1
152302
fi
153303

154304
Download
155305

156-
########## Source Envionmental Variables ##########
306+
########## Source Environmental Variables ##########
157307

158308
for file in $dot_env $dot_env_dev; do
159309
echo -e "$blue_text""Loading Shell Variables from $file...""$default_text"
@@ -162,16 +312,23 @@ done
162312

163313

164314
########## Start Docker ##########
165-
166315
echo
167316
echo -e "$blue_text""Starting Docker Compose""$default_text"
317+
if [ -z "$dockerDetachedMode" ]; then
318+
# if running in docker-detach mode, kick off a background task as `docker compose up` will be a blocking
319+
# call and we'll have no way to determine when we've successfully started.
320+
TelemetryDockerUp &
321+
fi
168322

169323
docker compose up $dockerDetachedMode
170324

171325
# $? is the exit code of the last command. So here: docker compose up
172326
if test $? -ne 0; then
173327
echo -e "$red_text""Docker compose failed. If you are seeing container conflicts""$default_text"
174328
echo -e "$red_text""please consider removing old containers""$default_text"
329+
TelemetrySend "failed" "install" "docker compose failed"
330+
else
331+
TelemetrySend "success" "install"
175332
fi
176333

177334
########## Ending Docker ##########

0 commit comments

Comments
 (0)