Skip to content

Commit

Permalink
Add generate_uuid, remove uuidgen and tr
Browse files Browse the repository at this point in the history
  • Loading branch information
krzko committed Jul 10, 2022
1 parent 9112c9c commit 8ed2895
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ Whilst all effort has been made to limit the use of external binaries from the s
- `date`: display or set date and time
- `hostname`: set or print name of current host system
- `jq`: Command-line JSON processor
- `tr`: translate characters
- `uuidgen`: generates new UUID strings
- `uname`: Print operating system name

As a future enhancement all effort will be made to remove the need for as many external binaries as possible and just utilise the built-in functions.
Expand Down
3 changes: 2 additions & 1 deletion examples/simple_trace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ service_version="0.0.1-dev"

source ../library/log.sh
source ../library/otel_trace.sh
source ../library/uuid.sh

curl_httpbin_200() {
log_info "curl -X GET https://httpbin.org/status/200 -H 'accept: text/plain'"
Expand All @@ -29,4 +30,4 @@ otel_trace_start_parent_span curl_httpbin_200
otel_trace_start_child_span curl_httpbin_201

log_info "TraceId ${TRACE_ID}"
log_info "ParentSpanId: ${PARENT_SPAN_ID=${span_id:0:16}}"
log_info "ParentSpanId: ${PARENT_SPAN_ID}"
21 changes: 11 additions & 10 deletions library/otel_trace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
# AUTHORS, LICENSE and DOCUMENTATION
#

export TRACE_ID=$(uuidgen | tr -d '-' | tr '[:upper:]' '[:lower:]')
export PARENT_SPAN_ID=""

source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/otel_init.sh"
# source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/otel_trace_exporter.sh"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/otel_trace_schema.sh"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/log.sh"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/uuid.sh"

export TRACE_ID=$(generate_uuid 16)
export PARENT_SPAN_ID=""

#######################################
# Starts a new parent trace bound to the TRACE_ID
Expand All @@ -26,7 +27,7 @@ source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/log.sh"
#######################################
otel_trace_start_parent_span() {
local name=$1
local span_id=$(uuidgen | tr -d '-' | tr '[:upper:]' '[:lower:]')
local span_id=$(generate_uuid 8)

local start_time_unix_nano=$(date +%s)
"$@"
Expand All @@ -45,7 +46,7 @@ otel_trace_start_parent_span() {
# log_info "Passing ${name} ${TRACE_ID} ${span_id:0:16} ${parent_span_id} ${start_time_unix_nano} ${end_time_unix_nano} ${exit_status}"
otel_trace_add_resource_scopespans_span $name \
$TRACE_ID \
${span_id:0:16} \
${span_id} \
"" \
$start_time_unix_nano \
$end_time_unix_nano \
Expand All @@ -56,7 +57,7 @@ otel_trace_start_parent_span() {
# curl -ik -X POST -H 'Content-Type: application/json' -d "${json}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" -o /dev/null -s
else
log_info "traceId: ${TRACE_ID}"
log_info "spanId: ${span_id:0:16}"
log_info "spanId: ${span_id}"
log_info "parentSpanId: ${PARENT_SPAN_ID}"
log_info "OTEL_EXPORTER_OTEL_ENDPOINT=${OTEL_EXPORTER_OTEL_ENDPOINT}"
log_info "curl -ik -X POST -H 'Content-Type: application/json' -d ${otel_trace_resource_spans} ${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces"
Expand All @@ -81,7 +82,7 @@ otel_trace_start_parent_span() {
#######################################
otel_trace_start_child_span() {
local name=$1
local span_id=$(uuidgen | tr -d '-' | tr '[:upper:]' '[:lower:]')
local span_id=$(generate_uuid 8)

local start_time_unix_nano=$(date +%s)
"$@"
Expand All @@ -94,7 +95,7 @@ otel_trace_start_child_span() {
# log_info "Passing ${name} ${TRACE_ID} ${span_id:0:16} ${parent_span_id} ${start_time_unix_nano} ${end_time_unix_nano} ${exit_status}"
otel_trace_add_resource_scopespans_span $name \
$TRACE_ID \
${span_id:0:16} \
${span_id} \
$PARENT_SPAN_ID \
$start_time_unix_nano \
$end_time_unix_nano \
Expand All @@ -105,12 +106,12 @@ otel_trace_start_child_span() {
# curl -ik -X POST -H 'Content-Type: application/json' -d "${json}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" -o /dev/null -s
else
log_info "traceId: ${TRACE_ID}"
log_info "spanId: ${span_id:0:16}"
log_info "spanId: ${span_id}"
log_info "parentSpanId: ${PARENT_SPAN_ID}"
log_info "OTEL_EXPORTER_OTEL_ENDPOINT=${OTEL_EXPORTER_OTEL_ENDPOINT}"
log_info "curl -ik -X POST -H 'Content-Type: application/json' -d ${otel_trace_resource_spans} ${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces"
# curl -ik -X POST -H 'Content-Type: application/json' -d "${json}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces"
fi

PARENT_SPAN_ID=${span_id:0:16}
PARENT_SPAN_ID=${span_id}
}
40 changes: 40 additions & 0 deletions library/uuid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

#######################################
# Generates a UUID based on a desired length
# GLOBALS:
# RANDOM
# ARGUMENTS:
# length, the length of the UUID to generate
# OUTPUTS:
# Write to stdout
#######################################
generate_uuid()
{
local length=$1

local N B C='89ab'

for (( N=0; N < $1; ++N ))
do
B=$(( $RANDOM%256 ))

case $N in
6)
printf '4%x' $(( B%$1 ))
;;
8)
printf '%c%x' ${C:$RANDOM%${#C}:1} $(( B%$1 ))
;;
3 | 5 | 7 | 9)
# Add dashes '%02x-'
printf '%02x' $B
;;
*)
printf '%02x' $B
;;
esac
done

echo
}

0 comments on commit 8ed2895

Please sign in to comment.