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

Inject safely into old debian python venvs #1010

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/false

if _otel_string_contains "$(_otel_resolve_command_self)" /usr/local/bin/renovate || _otel_string_contains "$(_otel_resolve_command_self)" /usr/local/sbin/renovate; then # renovate looks at some very specific env vars to enable tracing
export OTEL_SHELL_CONFIG_INJECT_DEEP=TRUE
export OTEL_SHELL_CONFIG_INSTRUMENT_ABSOLUTE_PATHS=TRUE
export OTEL_BSP_MAX_EXPORT_BATCH_SIZE=1
if _otel_string_contains "$OTEL_TRACES_EXPORTER" console; then export RENOVATE_TRACING_CONSOLE_EXPORTER=true; fi
\export OTEL_SHELL_CONFIG_INJECT_DEEP=TRUE
\export OTEL_SHELL_CONFIG_INSTRUMENT_ABSOLUTE_PATHS=TRUE
\export OTEL_BSP_MAX_EXPORT_BATCH_SIZE=1
if _otel_string_contains "$OTEL_TRACES_EXPORTER" console; then \export RENOVATE_TRACING_CONSOLE_EXPORTER=true; fi
if \[ -z "$OTEL_EXPORTER_OTLP_ENDPOINT" ] && ( _otel_string_contains "$OTEL_TRACES_EXPORTER" otlp || \[ -z "$OTEL_TRACES_EXPORTER" ] ); then
if \[ -n "$OTEL_EXPORTER_OTLP_TRACES_ENDPOINT" ]; then
if _otel_string_ends_with "$OTEL_EXPORTER_OTLP_TRACES_ENDPOINT" /v1/traces; then
export OTEL_EXPORTER_OTLP_ENDPOINT="${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT%/v1/traces}"
\export OTEL_EXPORTER_OTLP_ENDPOINT="${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT%/v1/traces}"
fi
else
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
\export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
fi
fi
fi
40 changes: 31 additions & 9 deletions src/usr/share/opentelemetry_shell/agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ _otel_auto_instrument() {
# special instrumentations
_otel_alias_prepend alias _otel_alias_and_instrument
_otel_alias_prepend unalias _otel_unalias_and_reinstrument
_otel_alias_prepend hash _otel_hash_and_reinstrument
if \type hash 1> /dev/null 2> /dev/null; then
_otel_alias_prepend hash _otel_hash_and_reinstrument
fi
_otel_alias_prepend export _otel_export_PATH_and_reinstrument
_otel_alias_prepend . _otel_instrument_and_source
if \[ "$_otel_shell" = bash ]; then _otel_alias_prepend source _otel_instrument_and_source; fi

Expand Down Expand Up @@ -345,6 +348,25 @@ _otel_hash_and_reinstrument() {
return "$exit_code"
}

_otel_export_PATH_and_reinstrument() {
shift
local exit_code=0
\export "$@" || local exit_code="$?"
if \[ "$1" = PATH ] || _otel_string_starts_with "$1" PATH=; then
local aliases_pre="$(\mktemp)"
local aliases_new="$(\mktemp)"
\alias | \sed 's/^alias //' | \awk '{print "\\alias " $0 }' > "$aliases_pre"
\unalias -a
_otel_auto_instrument "$_otel_shell_auto_instrumentation_hint"
\alias | \sed 's/^alias //' | \awk '{print "\\alias " $0 }' > "$aliases_new"
\unalias -a
\. "$aliases_pre"
\. "$aliases_new"
\rm "$aliases_pre" "$aliases_new"
fi
return "$exit_code"
}

_otel_instrument_and_source() {
local n="$1"
shift
Expand All @@ -356,11 +378,11 @@ _otel_instrument_and_source() {

_otel_inject_and_exec_directly() { # this function assumes there is no fd fuckery
if \[ "$#" = 1 ]; then
export OTEL_SHELL_CONSERVATIVE_EXEC=TRUE
\export OTEL_SHELL_CONSERVATIVE_EXEC=TRUE
_otel_sdk_communicate 'SPAN_AUTO_END'
if \[ -n "$_otel_commandline_override" ]; then
export OTEL_SHELL_COMMANDLINE_OVERRIDE="$_otel_commandline_override"
export OTEL_SHELL_COMMANDLINE_OVERRIDE_SIGNATURE="$PPID"
\export OTEL_SHELL_COMMANDLINE_OVERRIDE="$_otel_commandline_override"
\export OTEL_SHELL_COMMANDLINE_OVERRIDE_SIGNATURE="$PPID"
fi
\eval '"exec"' "$(\xargs -0 sh -c '. otelapi.sh; _otel_escape_args "$@"' sh < /proc/$$/cmdline)"
fi
Expand All @@ -372,10 +394,10 @@ _otel_inject_and_exec_directly() { # this function assumes there is no fd fucker
otel_span_end "$span_id"
_otel_sdk_communicate 'SPAN_AUTO_END'

export TRACEPARENT="$my_traceparent"
export OTEL_SHELL_AUTO_INJECTED=TRUE
export OTEL_SHELL_COMMANDLINE_OVERRIDE="$(_otel_command_self)"
export OTEL_SHELL_COMMANDLINE_OVERRIDE_SIGNATURE="$PPID"
\export TRACEPARENT="$my_traceparent"
\export OTEL_SHELL_AUTO_INJECTED=TRUE
\export OTEL_SHELL_COMMANDLINE_OVERRIDE="$(_otel_command_self)"
\export OTEL_SHELL_COMMANDLINE_OVERRIDE_SIGNATURE="$PPID"
shift
\exec sh -c '. otel.sh
eval "$(_otel_escape_args "$@")"' sh "$@"
Expand Down Expand Up @@ -411,7 +433,7 @@ _otel_record_exec() {
otel_span_deactivate "$span_id"
otel_span_end "$span_id"
_otel_sdk_communicate 'SPAN_AUTO_END'
export TRACEPARENT="$my_traceparent"
\export TRACEPARENT="$my_traceparent"
}

command() {
Expand Down
22 changes: 11 additions & 11 deletions src/usr/share/opentelemetry_shell/api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

# translate legacy config
if \[ -n "$OTEL_SHELL_TRACES_ENABLE" ] || \[ -n "$OTEL_SHELL_METRICS_ENABLE" ] || \[ -n "$OTEL_SHELL_LOGS_ENABLE" ]; then
\[ "$OTEL_SHELL_TRACES_ENABLE" = TRUE ] && export OTEL_TRACES_EXPORTER=otlp || export OTEL_TRACES_EXPORTER=""
\[ "$OTEL_SHELL_METRICS_ENABLE" = TRUE ] && export OTEL_METRICS_EXPORTER=otlp || export OTEL_METRICS_EXPORTER=""
\[ "$OTEL_SHELL_LOGS_ENABLE" = TRUE ] && export OTEL_LOGS_EXPORTER=otlp || export OTEL_LOGS_EXPORTER=""
export OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=delta
\[ "$OTEL_SHELL_TRACES_ENABLE" = TRUE ] && \export OTEL_TRACES_EXPORTER=otlp || \export OTEL_TRACES_EXPORTER=""
\[ "$OTEL_SHELL_METRICS_ENABLE" = TRUE ] && \export OTEL_METRICS_EXPORTER=otlp || \export OTEL_METRICS_EXPORTER=""
\[ "$OTEL_SHELL_LOGS_ENABLE" = TRUE ] && \export OTEL_LOGS_EXPORTER=otlp || \export OTEL_LOGS_EXPORTER=""
\export OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=delta
fi

# check environment
Expand Down Expand Up @@ -206,18 +206,18 @@ otel_span_traceparent() {

otel_span_activate() {
local span_handle="$1"
export TRACEPARENT_STACK="$TRACEPARENT/$TRACEPARENT_STACK"
export TRACEPARENT="$(otel_span_traceparent "$span_handle")"
if \[ -z "$TRACESTATE" ]; then export TRACESTATE=""; fi
\export TRACEPARENT_STACK="$TRACEPARENT/$TRACEPARENT_STACK"
\export TRACEPARENT="$(otel_span_traceparent "$span_handle")"
if \[ -z "$TRACESTATE" ]; then \export TRACESTATE=""; fi
}

otel_span_deactivate() {
if _otel_string_contains "$TRACEPARENT_STACK" /; then
export TRACEPARENT="${TRACEPARENT_STACK%%/*}"
export TRACEPARENT_STACK="${TRACEPARENT_STACK#*/}"
\export TRACEPARENT="${TRACEPARENT_STACK%%/*}"
\export TRACEPARENT_STACK="${TRACEPARENT_STACK#*/}"
else
export TRACEPARENT="$TRACEPARENT_STACK"
export TRACEPARENT_STACK=""
\export TRACEPARENT="$TRACEPARENT_STACK"
\export TRACEPARENT_STACK=""
fi
if \[ -z "$TRACEPARENT" ]; then
unset TRACEPARENT_STACK
Expand Down
2 changes: 0 additions & 2 deletions tests/auto/test_auto_injection_python.shell
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ requests.get("http://example.com/baz")
' > script.py
python3 script.py | grep -v -F '"parent_id": null,' | grep -- '/baz' || exit 1

\[ "$SHELL" = bash ] || exit 0 # TODO dashes on debian:stable (not oldstable, not testing or newer) seem to have a bug and not re-hash commands properly and therefore not reinstrument properly.

dir=$(mktemp -d)
python3 -m venv --system-site-packages "$dir"/venv || exit 1
. "$dir"/venv/bin/activate
Expand Down
Loading