From b1f48bfe3cb13f6e9f9fb9cc0cbdb806ba704b61 Mon Sep 17 00:00:00 2001
From: plengauer
Date: Mon, 18 Sep 2023 17:21:24 +0000
Subject: [PATCH] context propagation
---
package/DEBIAN/control | 2 +-
.../usr/bin/opentelemetry_bash_remote_sdk.py | 14 +++++++++++--
package/usr/bin/opentelemetry_bash_sdk.sh | 20 +++++++++++++------
3 files changed, 27 insertions(+), 9 deletions(-)
diff --git a/package/DEBIAN/control b/package/DEBIAN/control
index 56ecb762f..44f7f3417 100644
--- a/package/DEBIAN/control
+++ b/package/DEBIAN/control
@@ -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
diff --git a/package/usr/bin/opentelemetry_bash_remote_sdk.py b/package/usr/bin/opentelemetry_bash_remote_sdk.py
index b4377d823..ca4196d47 100644
--- a/package/usr/bin/opentelemetry_bash_remote_sdk.py
+++ b/package/usr/bin/opentelemetry_bash_remote_sdk.py
@@ -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:
@@ -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:
diff --git a/package/usr/bin/opentelemetry_bash_sdk.sh b/package/usr/bin/opentelemetry_bash_sdk.sh
index dd2477b23..39b701fde 100644
--- a/package/usr/bin/opentelemetry_bash_sdk.sh
+++ b/package/usr/bin/opentelemetry_bash_sdk.sh
@@ -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 {
@@ -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"