Skip to content

Commit

Permalink
example: otel-wrapper-shim.sh + symlinks to trace nodejs builds (#339)
Browse files Browse the repository at this point in the history
* example: otel-wrapper-shim.sh + symlinks to trace nodejs builds

* copy changes
  • Loading branch information
alsuren authored Jun 28, 2024
1 parent a3d9182 commit 92a6f0a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions demos/30-trace-build-process/bash
43 changes: 43 additions & 0 deletions demos/30-trace-build-process/otel-wrapper-shim.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Build Process Tracing Example
#
# It's possible to instrument complex build processes in nodejs (npm/yarn/pnpm) and
# C (make/cmake/ninja) by injecting instrumented versions of some key commands into $PATH.
#
# This example is a folder with a shim script in it, and a bunch of fake tools that are just
# symlinks to the shim. It needs to be a folder so that it can be cleanly injected into $PATH.
#
# To see this working, start `otel-desktop-viewer` as discribed in the main README.md, and then:
# (
# cd .. && \
# ([ -d mermaid ] || git clone https://github.com/mermaid-js/mermaid) && \
# cd mermaid/ && \
# ../otel-cli/demos/30-trace-build-process/pnpm install
# )
#
# For more complex build processes, I have found jaeger to be quite good, because it also includes
# a black "critical path line". See https://www.jaegertracing.io/docs/1.54/getting-started/ for
# their all-in-one docker conainer setup instructions. Otherwise, everything else is the same as
# when using otel-desktop-viewer.

set -euo pipefail

export OTEL_EXPORTER_OTLP_ENDPOINT="${OTEL_EXPORTER_OTLP_ENDPOINT:-localhost:4317}"

TOOL_NAME="$(basename $0)"
LOCATION_IN_PATH="$(dirname $0)"
HERE="$(dirname $(readlink -f $0))"
# This is just a guess, based on what's left after we've removed ourselves and any symlinks we know about from the results.
# If we're symlinked into $PATH in more than once place then we could still end up in loops.
# If this becomes a problem, we could instead iterate over each location and call readlink,
# and drop anything that resolves to $HERE.
ORIGINAL_TOOL_PATH="$(which -a "$TOOL_NAME" | grep -Ev "($HERE|$LOCATION_IN_PATH)" | head -n1)"

# Put this dir first in $PATH so that nested calls out to bash and pnpm are instrumented
# (tools like npm have a habit of messing with $PATH to put themselves first in subshells).
# This will probably get quite long if there is a lot of recursion.
# If this causes problems, we could prune ouselves out before inserting ourself at the head.
export PATH="$HERE:$PATH"

otel-cli exec --service "$TOOL_NAME" --name "$*" -- $ORIGINAL_TOOL_PATH "$@"
1 change: 1 addition & 0 deletions demos/30-trace-build-process/pnpm
1 change: 1 addition & 0 deletions demos/30-trace-build-process/sh
1 change: 1 addition & 0 deletions demos/30-trace-build-process/tsx

0 comments on commit 92a6f0a

Please sign in to comment.