Skip to content

Commit 8bd8fca

Browse files
authored
add opentelemery helper scripts (#119)
1 parent c5a6fef commit 8bd8fca

9 files changed

+143
-5
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os.path
2+
import subprocess
3+
4+
TOOLS_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "tools")
5+
6+
def test_rapids_compute_trace_id():
7+
result = subprocess.run(
8+
os.path.join(TOOLS_DIR, "rapids-get-telemetry-trace-id"),
9+
env={
10+
"GITHUB_REPOSITORY": "rapidsai/gha-tools",
11+
"GITHUB_RUN_ID": "1123123",
12+
"RUN_ATTEMPT": "1"
13+
},
14+
text=True,
15+
capture_output=True,
16+
)
17+
assert result.stdout.strip() == "22ab4ec60f37f446b4a95917e86660df"
18+
assert result.stderr == ""
19+
assert result.returncode == 0
20+
21+
def test_rapids_get_traceparent():
22+
result = subprocess.run(
23+
[os.path.join(TOOLS_DIR, "rapids-get-telemetry-traceparent"), "my_job"],
24+
env={
25+
"GITHUB_REPOSITORY": "rapidsai/gha-tools",
26+
"GITHUB_RUN_ID": "1123123",
27+
"RUN_ATTEMPT": "1"
28+
},
29+
text=True,
30+
capture_output=True,
31+
)
32+
assert result.stdout.strip() == "00-22ab4ec60f37f446b4a95917e86660df-5f57388b5b07a3e8-01"
33+
assert result.stderr == ""
34+
assert result.returncode == 0
35+
36+
def test_rapids_get_traceparent_with_step():
37+
result = subprocess.run(
38+
[os.path.join(TOOLS_DIR, "rapids-get-telemetry-traceparent"), "my_job", "my step"],
39+
env={
40+
"GITHUB_REPOSITORY": "rapidsai/gha-tools",
41+
"GITHUB_RUN_ID": "1123123",
42+
"RUN_ATTEMPT": "1"
43+
},
44+
text=True,
45+
capture_output=True,
46+
)
47+
assert result.stdout.strip() == "00-22ab4ec60f37f446b4a95917e86660df-a6e5bc57fad91889-01"
48+
assert result.stderr == ""
49+
assert result.returncode == 0

tools/rapids-conda-retry

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ condaCmd=${RAPIDS_CONDA_EXE:=conda}
6767
# needToRetry: 1 if the command should be retried, 0 if it should not be
6868
function runConda {
6969
# shellcheck disable=SC2086
70-
${condaCmd} ${args} 2>&1| tee "${outfile}"
70+
rapids-otel-wrap ${condaCmd} ${args} 2>&1| tee "${outfile}"
7171
exitcode=$?
7272
needToRetry=0
7373
needToClean=0

tools/rapids-get-pr-conda-artifact

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ if [[ -z "${commit}" ]]; then
3333
commit=$(git ls-remote https://github.com/rapidsai/"${repo}".git refs/heads/pull-request/"${pr}" | cut -c1-7)
3434
fi
3535

36-
rapids-get-artifact "ci/${repo}/pull-request/${pr}/${commit}/${artifact_name}"
36+
rapids-otel-wrap rapids-get-artifact "ci/${repo}/pull-request/${pr}/${commit}/${artifact_name}"

tools/rapids-get-telemetry-trace-id

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
# This is a global, per-run identifier. It is the same across all jobs and all steps within all jobs.
3+
# It is constant from the source repo, to shared-workflows, to shared-actions.
4+
5+
sha="$(echo "${GITHUB_REPOSITORY}+${GITHUB_RUN_ID}+${RUN_ATTEMPT}" | sha256sum | cut -f1 -d' ')"
6+
echo "${sha:0:32}"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
# This emits a TRACEPARENT, which follows the w3c trace context standard.
3+
# https://www.w3.org/TR/trace-context/
4+
#
5+
# This script can operate for two purposes:
6+
# 1. The top level of a job, whether it is the job at the source repo (e.g. rmm) level, or
7+
# the matrix job level
8+
# 2. The steps level within a job, which uses both the job name and the step name
9+
#
10+
# The job name must always be provided as the first argument.
11+
# A step name MAY be provided as the second argument. If it is specified, the output corresponds to
12+
# the step within the context of its job.
13+
14+
JOB_NAME=$1
15+
STEP_NAME=${2:-}
16+
17+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
18+
19+
TRACE_ID="$("${SCRIPT_DIR}"/rapids-get-telemetry-trace-id)"
20+
JOB_SPAN_ID="${TRACE_ID}-${JOB_NAME}"
21+
STEP_SPAN_ID="${JOB_SPAN_ID}-${STEP_NAME}"
22+
23+
JOB_TRACEPARENT=$(echo -n "${JOB_SPAN_ID}" | sha256sum | cut -f1 -d' ')
24+
STEP_TRACEPARENT=$(echo -n "${STEP_SPAN_ID}" | sha256sum | cut -f1 -d' ')
25+
26+
if [ "${STEP_NAME}" != "" ]; then
27+
echo "00-${TRACE_ID}-${STEP_TRACEPARENT:0:16}-01"
28+
else
29+
echo "00-${TRACE_ID}-${JOB_TRACEPARENT:0:16}-01"
30+
fi

tools/rapids-mamba-retry

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ for arg in "$@"; do
4646
fi
4747
done
4848

49-
rapids-conda-retry "$@"
49+
rapids-otel-wrap rapids-conda-retry "${args[@]}"

tools/rapids-otel-wrap

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
# Wraps arbitrary commands with arbitrary args. Emits an OpenTelemetry span for tracing the command
3+
4+
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
5+
6+
RAPIDS_OTEL_TRACES_EXPORTER="${RAPIDS_OTEL_TRACES_EXPORTER:-${RAPIDS_OTEL_EXPORTER:-"console"}}"
7+
RAPIDS_OTEL_METRICS_EXPORTER="${RAPIDS_OTEL_METRICS_EXPORTER:-${RAPIDS_OTEL_EXPORTER:-"console"}}"
8+
RAPIDS_OTEL_LOGS_EXPORTER="${RAPIDS_OTEL_LOGS_EXPORTER:-${RAPIDS_OTEL_EXPORTER:-"console"}}"
9+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT:-${OTEL_EXPORTER_OTLP_ENDPOINT}/v1/traces}"
10+
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT="${OTEL_EXPORTER_OTLP_METRICS_ENDPOINT:-${OTEL_EXPORTER_OTLP_ENDPOINT}/v1/metrics}"
11+
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT="${OTEL_EXPORTER_OTLP_LOGS_ENDPOINT:-${OTEL_EXPORTER_OTLP_ENDPOINT}/v1/logs}"
12+
export TRACEPARENT="${TRACEPARENT}"
13+
14+
if [[ $(type otel-cli >/dev/null 2>&1) -eq 0 ]] && [ "$TRACEPARENT" != "" ]; then
15+
echo "Running command with OpenTelemetry instrumentation";
16+
17+
set -x
18+
if [ "$OTEL_SERVICE_NAME" = "" ]; then
19+
echo "WARNING: OTEL_SERVICE_NAME variable not provided. Traces from different steps may not be associated correctly."
20+
fi
21+
22+
# Some commands have instrumentation. For example, conda-build has monkey-patched instrumentation
23+
# that can be activated with the opentelemetry-instrument command. For these commands,
24+
# we replace the command with the wrapped command, quoted as a whole for the purposes
25+
# of otel-cli exec, so that flags don't get confused.
26+
case "$1" in
27+
conda* )
28+
echo "using opentelemetry-instrument for command";
29+
command="opentelemetry-instrument $*"
30+
;;
31+
* )
32+
command="$*"
33+
;;
34+
esac
35+
36+
echo "TRACEPARENT prior to otel-cli exec is: \"${TRACEPARENT}\""
37+
STEP_TRACEPARENT=$("${SCRIPT_DIR}/rapids-get-telemetry-traceparent" "${JOB_NAME}" "${OTEL_SERVICE_NAME}")
38+
39+
# otel-cli creates a span for us that bridges the traceparent from the parent process
40+
# into the command we're wrapping
41+
otel-cli exec \
42+
--name "Run instrumented $*" \
43+
--force-parent-span-id "$(cut -d'-' -f3 <<<"$STEP_TRACEPARENT")" \
44+
--verbose \
45+
-- "${command}"
46+
RETURN_STATUS=$?
47+
else
48+
echo "Skipping instrumentation, running \"${*}\"";
49+
eval "$*"
50+
RETURN_STATUS=$?
51+
fi
52+
53+
exit "${RETURN_STATUS}"

tools/rapids-upload-conda-to-s3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ pkg_name="$(rapids-package-name "$pkg_type")"
3030
# Where conda build artifacts are output
3131
path_to_tar_up="${RAPIDS_CONDA_BLD_OUTPUT_DIR}"
3232

33-
rapids-upload-to-s3 "${pkg_name}" "${path_to_tar_up}"
33+
rapids-otel-wrap rapids-upload-to-s3 "${pkg_name}" "${path_to_tar_up}"

tools/rapids-upload-wheels-to-s3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ if [ "${CI:-false}" = "false" ]; then
2020
exit 0
2121
fi
2222

23-
rapids-upload-to-s3 "${pkg_name}" "$@"
23+
rapids-otel-wrap rapids-upload-to-s3 "${pkg_name}" "$@"

0 commit comments

Comments
 (0)