Skip to content

Commit

Permalink
context propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
plengauer committed Sep 18, 2023
1 parent e43fe64 commit b1f48bf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: opentelemetry-bash
Version: 0.2.0
Version: 0.3.0
Architecture: all
Depends: bash, awk, apt, python3, python3-pip, python3-venv
Priority: extra
Expand Down
14 changes: 12 additions & 2 deletions package/usr/bin/opentelemetry_bash_remote_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from opentelemetry.trace import SpanKind
from opentelemetry.sdk.trace import Span, StatusCode
from opentelemetry.sdk.resources import Resource
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator

class AwsEC2ResourceDetector(ResourceDetector):
def detect(self) -> Resource:
Expand Down Expand Up @@ -63,8 +64,17 @@ def main():
processor.shutdown()
return
case 'SPAN_START':
tokens = tokens[1].split(' ', 1)
spans.append(opentelemetry.trace.get_tracer(scope, version).start_span(tokens[1], kind=SpanKind[tokens[0].upper()]))
tokens = tokens[1].split(' ', 3)
response_fifo = tokens[0]
trace_parent = tokens[1]
kind = tokens[2]
name = tokens[3]
span = opentelemetry.trace.get_tracer(scope, version).start_span(name, kind=SpanKind[kind.upper()], context=TraceContextTextMapPropagator().extract({'traceparent': trace_parent}))
spans.append(span)
carrier = {}
TraceContextTextMapPropagator().inject(carrier, opentelemetry.trace.set_span_in_context(span, None))
with open(response_fifo, 'w') as response:
response.write(carrier['traceparent'])
case 'SPAN_END':
span : Span = spans.pop()
if not span:
Expand Down
20 changes: 14 additions & 6 deletions package/usr/bin/opentelemetry_bash_sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ function otel_shutdown {
}

function otel_span_start {
echo "SPAN_START $@" > $otel_remote_sdk_pipe
local response_pipe=/tmp/opentelemetry_bash_$$_response_$(echo $RANDOM | md5sum | cut -c 1-32).pipe
mkfifo $response_pipe
echo "SPAN_START $response_pipe $OTEL_TRACE_PARENT $@" > $otel_remote_sdk_pipe
local response=$(cat $response_pipe)
export OTEL_TRACE_PARENT_STACK=$OTEL_TRACE_PARENT/$OTEL_TRACE_PARENT_STACK
export OTEL_TRACE_PARENT=$response
rm $response_pipe
}

function otel_span_end {
echo "SPAN_END" > $otel_remote_sdk_pipe
export OTEL_TRACE_PARENT=$(echo $OTEL_TRACE_PARENT_STACK | cut -d'/' -f1)
export OTEL_TRACE_PARENT_STACK=$(echo $OTEL_TRACE_PARENT_STACK | cut -d'/' -f2-)
}

function otel_span_error {
Expand All @@ -52,11 +60,11 @@ function otel_span_attribute {

function otel_observe {
otel_span_start INTERNAL $@
otel_span_attribute subprocess.executable.name=$(echo "$@" | cut -d' ' -f1 | rev | cut -d'/' -f1 | rev)
otel_span_attribute subprocess.executable.path​=$(which $(echo "$@" | cut -d' ' -f1))
otel_span_attribute subprocess.command="$@"
otel_span_attribute subprocess.command_args=$(echo "$@" | cut -d' ' -f2-)
eval $@
otel_span_attribute subprocess.executable.name=$(echo $@ | cut -d' ' -f1 | rev | cut -d'/' -f1 | rev)
otel_span_attribute subprocess.executable.path​=$(which $(echo $@ | cut -d' ' -f1))
otel_span_attribute subprocess.command=$@
otel_span_attribute subprocess.command_args=$(echo $@ | cut -d' ' -f2-)
eval "$@"
exit_code=$?
if [ "$exit_code" -ne "0" ]; then
otel_span_error "subprocess.exit_code=$exit_code"
Expand Down

0 comments on commit b1f48bf

Please sign in to comment.