Created
July 8, 2023 00:04
-
-
Save howardjohn/e471334009e95906b8888a1848c33e2b to your computer and use it in GitHub Desktop.
A terrible terrible hack to automatically trace shell function exeuction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
export OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317 | |
function gen-uid() { | |
tr -dc 'a-f0-9' < /dev/urandom | head -c$1 | |
} | |
trace=`gen-uid 32` | |
spans=() | |
starts=() | |
function my_debug_stuff() { | |
[[ ${FUNCNAME[1]} != $BASH_COMMAND ]] && return | |
local tp=`gen-uid 16` | |
spans+=("$tp") | |
starts+=("$(date -u +%s.%N)") | |
echo "DEBUG: Starting [$BASH_COMMAND] Spans=${spans[@]}" | |
} | |
function my_return() { | |
local tp="" | |
if [[ ${#spans[@]} -gt 1 ]]; then | |
echo "override tp" | |
tp="00-${trace}-${spans[-2]}-01" | |
fi | |
local span="${spans[-1]}" | |
local start="${starts[-1]}" | |
echo "RETURN ${FUNCNAME[1]}, span=${spans[-1]}, start=$start, parent=$tp" | |
unset spans[-1] | |
unset starts[-1] | |
TRACEPARENT=$tp otel-cli span \ | |
--service "test4" \ | |
--name "${FUNCNAME[1]}" \ | |
--start "$start" \ | |
--end "$(date -u +%s.%N)" \ | |
--force-trace-id "$trace" \ | |
--force-span-id "$span" | |
} | |
traceparent="" | |
function do-work() { | |
sleep 1 | |
} | |
function inner() { | |
sleep .1 | |
inner-deep | |
inner-deep | |
} | |
function inner-deep() { | |
sleep .01 | |
} | |
function outer() { | |
inner | |
inner | |
sleep .2 | |
inner | |
} | |
set -o functrace | |
trap my_return RETURN | |
trap my_debug_stuff DEBUG | |
outer | |
do-work |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment