Skip to content

Commit 5910c02

Browse files
aldy505BYK
andauthored
ref: allow hosted js sdk bundles (#3365)
* ref: allow hosted js sdk bundles --------- Co-authored-by: Burak Yigit Kaya <[email protected]>
1 parent 036f6d4 commit 5910c02

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ HEALTHCHECK_RETRIES=10
1717
# Caution: Raising max connections of postgres increases CPU and RAM usage
1818
# see https://github.com/getsentry/self-hosted/pull/2740 for more information
1919
POSTGRES_MAX_CONNECTIONS=100
20+
# Set SETUP_JS_SDK_ASSETS to 1 to enable the setup of JS SDK assets
21+
# SETUP_JS_SDK_ASSETS=1

_unit-test/js-sdk-assets-test.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
source _unit-test/_test_setup.sh
4+
source install/dc-detect-version.sh
5+
$dcb --force-rm web
6+
7+
export SETUP_JS_SDK_ASSETS=1
8+
9+
source install/setup-js-sdk-assets.sh
10+
11+
sdk_files=$(docker compose run --no-deps --rm -v "sentry-nginx-www:/var/www" nginx ls -lah /var/www/js-sdk/)
12+
sdk_tree=$(docker compose run --no-deps --rm -v "sentry-nginx-www:/var/www" nginx tree /var/www/js-sdk/ | tail -n 1)
13+
14+
# `sdk_files` should contains 2 lines, `7.*` and `8.*`
15+
echo $sdk_files
16+
total_directories=$(echo "$sdk_files" | grep -c '[78]\.[0-9]*\.[0-9]*$')
17+
echo $total_directories
18+
test "2" == "$total_directories"
19+
echo "Pass"
20+
21+
# `sdk_tree` should outputs "2 directories, 10 files"
22+
echo "$sdk_tree"
23+
test "2 directories, 10 files" == "$(echo "$sdk_tree")"
24+
echo "Pass"
25+
26+
report_success

docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ services:
460460
source: ./nginx
461461
target: /etc/nginx
462462
- sentry-nginx-cache:/var/cache/nginx
463+
- sentry-nginx-www:/var/www
463464
depends_on:
464465
- web
465466
- relay
@@ -529,6 +530,9 @@ volumes:
529530
external: true
530531
sentry-symbolicator:
531532
external: true
533+
# This volume stores JS SDK assets and the data inside this volume should
534+
# be cleaned periodically on upgrades.
535+
sentry-nginx-www:
532536
# This volume stores profiles and should be persisted.
533537
# Not being external will still persist data across restarts.
534538
# It won't persist if someone does a docker compose down -v.

install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ source install/bootstrap-snuba.sh
3636
source install/upgrade-postgres.sh
3737
source install/set-up-and-migrate-database.sh
3838
source install/geoip.sh
39+
source install/setup-js-sdk-assets.sh
3940
source install/wrap-up.sh

install/setup-js-sdk-assets.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This will only run if the SETUP_JS_SDK_ASSETS environment variable is set to 1.
2+
# Think of this as some kind of a feature flag.
3+
if [[ "${SETUP_JS_SDK_ASSETS:-}" == "1" ]]; then
4+
echo "${_group}Setting up JS SDK assets"
5+
6+
# If the `sentry-nginx-www` volume exists, we need to prune the contents.
7+
# We don't want to fill the volume with old JS SDK assets.
8+
# If people want to keep the old assets, they can set the environment variable
9+
# `SETUP_JS_SDK_KEEP_OLD_ASSETS` to any value.
10+
if [[ -z "${SETUP_JS_SDK_KEEP_OLD_ASSETS:-}" ]]; then
11+
echo "Cleaning up old JS SDK assets..."
12+
$dcr --no-deps --rm -v "sentry-nginx-www:/var/www" nginx rm -rf /var/www/js-sdk/*
13+
fi
14+
15+
$dbuild -t sentry-self-hosted-jq-local --platform="$DOCKER_PLATFORM" jq
16+
17+
jq="docker run --rm -i sentry-self-hosted-jq-local"
18+
19+
loader_registry=$($dcr --no-deps --rm -T web cat /usr/src/sentry/src/sentry/loader/_registry.json)
20+
# The `loader_registry` should start with "Updating certificates...", we want to delete that and the subsequent ca-certificates related lines.
21+
# We want to remove everything before the first '{'.
22+
loader_registry=$(echo "$loader_registry" | sed '0,/{/s/[^{]*//')
23+
24+
latest_js_v7=$(echo "$loader_registry" | $jq -r '.versions | reverse | map(select(.|any(.; startswith("7.")))) | .[0]')
25+
latest_js_v8=$(echo "$loader_registry" | $jq -r '.versions | reverse | map(select(.|any(.; startswith("8.")))) | .[0]')
26+
27+
echo "Found JS SDKs v${latest_js_v7} and v${latest_js_v8}, downloading from upstream.."
28+
29+
# Download those two using wget
30+
for version in "${latest_js_v7}" "${latest_js_v8}"; do
31+
$dcr --no-deps --rm -v "sentry-nginx-www:/var/www" nginx mkdir -p /var/www/js-sdk/${version}
32+
for variant in "tracing" "tracing.replay" "replay" "tracing.replay.feedback" "feedback"; do
33+
$dcr --no-deps --rm -v "sentry-nginx-www:/var/www" nginx wget -q -O /var/www/js-sdk/${version}/bundle.${variant}.min.js "https://browser.sentry-cdn.com/${version}/bundle.${variant}.min.js"
34+
done
35+
done
36+
37+
echo "${_endgroup}"
38+
fi

nginx/nginx.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ http {
8888
location ^~ /api/0/relays/ {
8989
proxy_pass http://relay;
9090
}
91+
location ^~ /js-sdk/ {
92+
autoindex on;
93+
root /var/www/js-sdk;
94+
}
9195
location / {
9296
proxy_pass http://sentry;
9397
}

sentry/sentry.conf.example.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,17 @@ def get_internal_network():
378378
# JS SDK Loader #
379379
#################
380380

381+
# Configure Sentry JS SDK bundle URL template for Loader Scripts.
382+
# Learn more about the Loader Scripts: https://docs.sentry.io/platforms/javascript/install/loader/
383+
384+
# If you wish to host your own JS SDK bundles, set `SETUP_JS_SDK_ASSETS` environment variable to `1`
385+
# on your `.env` or `.env.custom` file. Then, replace the value below with your own public URL.
386+
# For example: "https://sentry.example.com/js-sdk/%s/bundle%s.min.js"
387+
#
388+
# By default, the previous JS SDK assets version will be pruned during upgrades, if you wish
389+
# to keep the old assets, set `SETUP_JS_SDK_KEEP_OLD_ASSETS` environment variable to any value on
390+
# your `.env` or `.env.custom` file. The files should only be a few KBs, and this might be useful
391+
# if you're using it directly like a CDN instead of using the loader script.
381392
JS_SDK_LOADER_DEFAULT_SDK_URL = "https://browser.sentry-cdn.com/%s/bundle%s.min.js"
382393

383394

0 commit comments

Comments
 (0)