Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a demo that shows how to work around span background races #168

Merged
merged 1 commit into from
May 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions demos/20span-background-race-workarounds.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
# an otel-cli demo of workarounds for race conditions on span background
#
# otel-cli span background is usually run as a subprocess with & in the command
# as below. An issue that shows up sometimes is a race condition where the shell
# starts otel-cli in the background, and then immediately calls otel-cli span
# or similar hoping to use the --tp-carrier file, which might not be created
# before the process looks for it. There are a couple solutions below.

set -e
set -x

carrier=$(mktemp) # traceparent propagation via tempfile
sockdir=$(mktemp -d) # a unix socket will be created here

../otel-cli span background \
--tp-carrier $carrier \
--sockdir $sockdir \
--service $0 \
--name "$0 script execution #1" \
--timeout 10 &

# On Linux, the inotifywait command will do the trick, waiting for the
# file to be written. Without a timeout it could hang forever if it loses
# the race and otel-cli finishes writing the carrier before inotifywait
# starts watching. A short timeout ensures it won't hang.
[ ! -e $carrier ] && inotifywait --timeout 0.1 $carrier
../otel-cli span --tp-carrier $carrier --name "child of span background, after inotifywait"
../otel-cli span end --sockdir $sockdir

# start another one for the second example
../otel-cli span background \
--tp-carrier $carrier \
--sockdir $sockdir \
--service $0 \
--name "$0 script execution #2" \
--timeout 10 &

# otel-cli span event already waits for span background's socket file
# to appear so just sending an event will do enough synchronization, at
# the cost of a meaningless event.
../otel-cli span event --sockdir $sockdir --name "wait for span background"
../otel-cli span --tp-carrier $carrier --name "child of span background, after span event"
../otel-cli span end --sockdir $sockdir