Skip to content

Commit 70aa67f

Browse files
feat: use GITHUB_TOKEN if available in Serverless-Init .NET install script (#748)
Currently the .NET install script for Serverless-Init makes unauthenticated requests to the Github API, sometimes resulting in rate limiting. This PR adds a `GITHUB_TOKEN` to be passed as a [Docker secret](https://docs.docker.com/build/building/secrets/) to the .NET install script to allow for an increased rate limit. If no `GITHUB_TOKEN` is passed then requests will be unauthenticated as they were before this change. ## Motivation #734 ## Additional Notes - Check if a `GITHUB_TOKEN` environment variable is set. If it is, pass it in the `Authorization` header with `Bearer` - Add a `status_code` attribute to the response json and print it for troubleshooting - Use `jq` to parse the response json * Note that control characters needed to be stripped from the Github responses in order to be successfully parsed - If the tracer version is not parsed from the response json, presumably due to rate limiting or some other issue on Github's end, exit the script Github token provided -> success ``` #16 6.759 Github token provided #16 7.243 Status code of version request: 200 #16 7.291 Downloading version 3.21.0 of the .NET tracer into /tmp/datadog-dotnet-apm.tar.gz #16 7.291 Github token provided #16 10.08 Status code of download request: 200 ``` No Github token provided -> success ``` #16 7.664 Github token not provided #16 7.967 Status code of version request: 200 #16 8.018 Downloading version 3.21.0 of the .NET tracer into /tmp/datadog-dotnet-apm.tar.gz #16 8.019 Github token not provided #16 11.10 Status code of download request: 200 ``` No Github token provided -> rate limited ``` 6.518 Github token not provided 6.780 Status code of version request: 403 6.828 Error: Could not determine the tracer version. Exiting. ``` ### Usage - Pass value in `GITHUB_TOKEN` environment variable to the Docker secret `github-token`. ``` docker build -t <image> --secret id=github-token,env=GITHUB_TOKEN . ``` - In the Dockerfile, read the value of the `github-token` secret into the `GITHUB_TOKEN` environment variable so it can be passed in the authentication header of the Github API request. ``` RUN --mount=type=secret,id=github-token,env=GITHUB_TOKEN \ chmod +x /app/dotnet.sh && /app/dotnet.sh ``` Relevant documentation to update following this change: - https://docs.datadoghq.com/serverless/guide/gcr_serverless_init/?tab=dotnet - https://docs.datadoghq.com/serverless/guide/aca_serverless_init/?tab=dotnet - https://docs.datadoghq.com/serverless/guide/azure_app_service_linux_containers_serverless_init/?tab=dotnet
1 parent 8bdd819 commit 70aa67f

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

scripts/serverless_init_dotnet.sh

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,35 @@ if [ -f "/etc/alpine-release" ]; then
66
exit 1
77
fi
88

9-
# Make sure curl is installed
10-
apt-get update && apt-get install -y curl
9+
# Make sure curl and jq are installed
10+
apt-get update && apt-get install -y curl jq
11+
12+
ghcurl() {
13+
if [ -n "$GITHUB_TOKEN" ]; then
14+
echo "Github token provided" >&2
15+
curl -sSL -w '{"status_code": %{http_code}}' -H "Authorization: Bearer $GITHUB_TOKEN" "$@" | jq -sre add
16+
else
17+
echo "Github token not provided" >&2
18+
curl -sSL -w '{"status_code": %{http_code}}' "$@" | jq -sre add
19+
fi
20+
}
1121

1222
# Get latest released version of dd-trace-dotnet
13-
TRACER_VERSION=$(curl -s https://api.github.com/repos/DataDog/dd-trace-dotnet/releases/latest | grep 'tag_name' | cut -d '"' -f 4)
14-
TRACER_VERSION=${TRACER_VERSION#v} # remove the 'v' prefix
23+
response=$(ghcurl https://api.github.com/repos/DataDog/dd-trace-dotnet/releases/latest)
24+
sanitized_response=$(echo "$response" | tr -d '\000-\037') # remove control characters from json response
25+
echo "Status code of version request: $(echo "$sanitized_response" | jq '.status_code')"
26+
TRACER_VERSION=$(echo "$sanitized_response" | jq -r '.tag_name // empty' | sed 's/^v//')
27+
28+
if [ -z "$TRACER_VERSION" ]; then
29+
echo "Error: Could not determine the tracer version. Exiting." >&2
30+
exit 1
31+
fi
1532

1633
# Download the tracer to the dd_tracer folder
1734
echo Downloading version "${TRACER_VERSION}" of the .NET tracer into /tmp/datadog-dotnet-apm.tar.gz
18-
curl -L "https://github.com/DataDog/dd-trace-dotnet/releases/download/v${TRACER_VERSION}/datadog-dotnet-apm-${TRACER_VERSION}.tar.gz" -o /tmp/datadog-dotnet-apm.tar.gz
35+
response=$(ghcurl "https://github.com/DataDog/dd-trace-dotnet/releases/download/v${TRACER_VERSION}/datadog-dotnet-apm-${TRACER_VERSION}.tar.gz" \
36+
-o /tmp/datadog-dotnet-apm.tar.gz)
37+
echo "Status code of download request: $(echo "$response" | jq '.status_code')"
1938

2039
# Unarchive the tracer and remove the tmp
2140
mkdir -p /dd_tracer/dotnet

0 commit comments

Comments
 (0)