Skip to content

Commit

Permalink
Add debug mode for otel zero code instrumentation (#11)
Browse files Browse the repository at this point in the history
* Add debug mode for otel zero code instrumentation

Signed-off-by: svrnm <[email protected]>

* add debug mode integration tests

Signed-off-by: svrnm <[email protected]>

---------

Signed-off-by: svrnm <[email protected]>
  • Loading branch information
svrnm authored Aug 1, 2024
1 parent d0fcc0b commit f43e825
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
21 changes: 19 additions & 2 deletions otelify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Set the default values
OTELIFY_STRICT=${OTELIFY_STRICT:-false}
OTELIFY_DEBUG=${OTELIFY_DEBUG:-false}
OTELIFY_DEBUG_OTEL=${OTELIFY_DEBUG_OTEL:-false}
OTELIFY_KEEP_DOWNLOADS=${OTELIFY_KEEP_DOWNLOADS:-true}
OTELIFY_DIRECTORY=${OTELIFY_DIRECTORY:-~/.otelify}

Expand All @@ -37,7 +38,8 @@ usage() {
echo "Usage: $0 [options] <application>"
echo ""
echo "Options:"
echo " -d: Enable debug mode"
echo " -d: Enable debug mode for otelify.sh"
echo " -D: Enable debug mode for the OpenTelemetry zero-code instrumentation"
echo " -e: Set the OpenTelemetry exporter for all signals, e.g. -e otlp"
echo " -f: Set the directory where the OpenTelemetry files will be downloaded, e.g. -f /tmp. Default is ~/.otelify"
echo " -h: Show this help message"
Expand Down Expand Up @@ -118,6 +120,11 @@ setup_node() {
export NODE_OPTIONS="${NODE_OPTIONS} --require @opentelemetry/auto-instrumentations-node/register"
}

enable_otel_debug_mode() {
export OTEL_JAVAAGENT_DEBUG=true
export OTEL_LOG_LEVEL=debug
}

# allow tests to overwrite the following two functions for mocking
if [[ $(type -t dotnet_instrument) != function ]]; then
dotnet_instrument() {
Expand All @@ -139,13 +146,18 @@ if [ $# -lt 1 ]; then
exit 0
fi

while getopts "de:f:hrs-:" opt; do
while getopts "dDe:f:hrs-:" opt; do
case "${opt}" in
'd')
# if the option is -d, then debug mode is enabled
OTELIFY_DEBUG=true
debug "Debug mode enabled"
;;
'D')
# if the option is -D, then debug mode is enabled for the OpenTelemetry zero-code instrumentation
OTELIFY_DEBUG_OTEL=true
debug "Debug mode enabled for the OpenTelemetry zero-code instrumentation"
;;
'e')
# if the option is -e, then the OpenTelemetry exporter is set for all signals
debug "Setting the OpenTelemetry exporter for all signals to ${OPTARG}"
Expand Down Expand Up @@ -193,6 +205,11 @@ if [ ! -d "${OTELIFY_DIRECTORY}" ]; then
mkdir -p "${OTELIFY_DIRECTORY}"
fi

# if OTELIFY_DEBUG_OTEL is set to true, we enable the debug mode for the OpenTelemetry zero-code instrumentation
if [ "${OTELIFY_DEBUG_OTEL}" = true ]; then
enable_otel_debug_mode
fi

# remaining arguments are the application
application="${*}"

Expand Down
13 changes: 10 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ INTEGRATION_TEST_URL=${INTEGRATION_TEST_URL:-"https://github.com/cisco-open/"}

FULL_IMAGE_NAME=bats/bats:1.11.0

# Check if we are doing a quick check, if so we will skip the integration tests.
DISABLE_DOTNET=""

for arg in "$@"; do
# Check if we are doing a quick check, if so we will skip the integration tests.
if [ "$arg" = "-q" ] || [ "$arg" = "--quick" ]; then
echo "No integration tests will be run"
WITH_INTEGRATION=0
break # Exit the loop if -q or --quick is found
fi

# Check if we are disabling dotnet integration tests.
if [ "$arg" = "-n" ] || [ "$arg" = "--disable-dotnet" ]; then
echo "Disabling dotnet integration tests"
DISABLE_DOTNET=(--filter-tags '!integration:dotnet')
fi
done

Expand All @@ -43,4 +50,4 @@ if [ ${WITH_INTEGRATION} -eq 1 ]; then
${DOCKER_COMMAND} build -t "$FULL_IMAGE_NAME" .
fi

${DOCKER_COMMAND} run -it -e WITH_INTEGRATION="${WITH_INTEGRATION}" -e INTEGRATION_TEST_URL="${INTEGRATION_TEST_URL}" -e BATS_LIB_PATH=/usr/lib/bats -v "${PWD}:/code" "${FULL_IMAGE_NAME}" -x --verbose-run test
${DOCKER_COMMAND} run -it -e WITH_INTEGRATION="${WITH_INTEGRATION}" -e INTEGRATION_TEST_URL="${INTEGRATION_TEST_URL}" -e BATS_LIB_PATH=/usr/lib/bats -v "${PWD}:/code" "${FULL_IMAGE_NAME}" "${DISABLE_DOTNET[@]}" test
40 changes: 38 additions & 2 deletions test/1-apps.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

_mock_setup() {
node() {
echo "OTEL_LOG_LEVEL=${OTEL_LOG_LEVEL}"
echo "OTEL_TRACES_EXPORTER=${OTEL_TRACES_EXPORTER}"
echo "OTEL_METRICS_EXPORTER=${OTEL_METRICS_EXPORTER}"
echo "OTEL_LOGS_EXPORTER=${OTEL_LOGS_EXPORTER}"
Expand All @@ -30,6 +31,7 @@ _mock_setup() {
echo "npm ${*}"
}
java() {
echo "OTEL_JAVAAGENT_DEBUG=${OTEL_JAVAAGENT_DEBUG}"
echo "OTEL_TRACES_EXPORTER=${OTEL_TRACES_EXPORTER}"
echo "OTEL_METRICS_EXPORTER=${OTEL_METRICS_EXPORTER}"
echo "OTEL_LOGS_EXPORTER=${OTEL_LOGS_EXPORTER}"
Expand All @@ -39,7 +41,6 @@ _mock_setup() {
}

dotnet_instrument() {

echo "OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED=${OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED}"
echo "OTEL_DOTNET_AUTO_METRICS_CONSOLE_EXPORTER_ENABLED=${OTEL_DOTNET_AUTO_METRICS_CONSOLE_EXPORTER_ENABLED}"
echo "OTEL_DOTNET_AUTO_LOGS_CONSOLE_EXPORTER_ENABLED=${OTEL_DOTNET_AUTO_LOGS_CONSOLE_EXPORTER_ENABLED}"
Expand All @@ -49,6 +50,11 @@ _mock_setup() {
echo "${OTEL_DOTNET_AUTO_HOME}/instrument.sh ${*}"
}

enable_otel_debug_mode() {
echo "export OTEL_JAVAAGENT_DEBUG=true"
echo "export OTEL_LOG_LEVEL=debug"
}

chmod() {
echo "chmod ${*}"
}
Expand Down Expand Up @@ -90,6 +96,7 @@ teardown() {
run otelify.sh -- "${APPJS}"
assert_output - << END
npm install --prefix ${OTELIFY_DIRECTORY} @opentelemetry/auto-instrumentations-node @opentelemetry/api
OTEL_LOG_LEVEL=
OTEL_TRACES_EXPORTER=console
OTEL_METRICS_EXPORTER=console
OTEL_LOGS_EXPORTER=console
Expand All @@ -101,6 +108,7 @@ END
run otelify.sh -- node -r ./mytest.js "${APPJS}"
assert_output - << END
npm install --prefix ${OTELIFY_DIRECTORY} @opentelemetry/auto-instrumentations-node @opentelemetry/api
OTEL_LOG_LEVEL=
OTEL_TRACES_EXPORTER=console
OTEL_METRICS_EXPORTER=console
OTEL_LOGS_EXPORTER=console
Expand All @@ -114,6 +122,7 @@ END
run otelify.sh -- java -jar "${APPJAR}"
assert_output - << END
curl -o ${OTELIFY_DIRECTORY}/otelify-opentelemetry-javaagent.jar -L https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
OTEL_JAVAAGENT_DEBUG=
OTEL_TRACES_EXPORTER=logging
OTEL_METRICS_EXPORTER=logging
OTEL_LOGS_EXPORTER=logging
Expand All @@ -125,6 +134,7 @@ END
run otelify.sh -- java -jar "${APPJAR}"
assert_output - << END
curl -o ${OTELIFY_DIRECTORY}/otelify-opentelemetry-javaagent.jar -L https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
OTEL_JAVAAGENT_DEBUG=
OTEL_TRACES_EXPORTER=logging
OTEL_METRICS_EXPORTER=logging
OTEL_LOGS_EXPORTER=logging
Expand Down Expand Up @@ -154,7 +164,7 @@ END

@test "will run any command provided" {
run otelify.sh -- echo "hello"
assert_output - << END
assert_output - << END
curl -o /tmp/otelify-test-dir/otel-dotnet-auto-install.sh -L https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh
chmod +x /tmp/otelify-test-dir/otel-dotnet-auto-install.sh
/tmp/otelify-test-dir/otel-dotnet-auto-install.sh
Expand All @@ -169,4 +179,30 @@ OTEL_METRICS_EXPORTER=console
OTEL_LOGS_EXPORTER=console
/tmp/otelify-test-dir/otel-dotnet-auto/instrument.sh echo hello
END
}

@test "will set environment variables for otel debug mode" {
run otelify.sh -D -- java -jar "${APPJAR}"
assert_output - << END
curl -o ${OTELIFY_DIRECTORY}/otelify-opentelemetry-javaagent.jar -L https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
OTEL_JAVAAGENT_DEBUG=true
OTEL_TRACES_EXPORTER=logging
OTEL_METRICS_EXPORTER=logging
OTEL_LOGS_EXPORTER=logging
OTEL_METRIC_EXPORT_INTERVAL=15000
JAVA_TOOL_OPTIONS= -javaagent:${OTELIFY_DIRECTORY}/otelify-opentelemetry-javaagent.jar
java -jar ${APPJAR}
END

run otelify.sh -D -- node -r ./mytest.js "${APPJS}"
assert_output - << END
npm install --prefix ${OTELIFY_DIRECTORY} @opentelemetry/auto-instrumentations-node @opentelemetry/api
OTEL_LOG_LEVEL=debug
OTEL_TRACES_EXPORTER=console
OTEL_METRICS_EXPORTER=console
OTEL_LOGS_EXPORTER=console
NODE_PATH=${OTELIFY_DIRECTORY}/node_modules:
NODE_OPTIONS= --require @opentelemetry/auto-instrumentations-node/register
node -r ./mytest.js ${APPJS}
END
}
12 changes: 12 additions & 0 deletions test/2-integration.bats
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ teardown() {
@test "can otelify nodejs applications" {
run otelify.sh -- node "${APP_DIR}/app.js"
assert_output --partial 'traceId: '

# Test with debug flag
run otelify.sh -D -- node "${APP_DIR}/app.js"
assert_output --partial '@opentelemetry/api: Registered a global for diag'
}

#bats test_tags=integration:java, java
Expand All @@ -61,10 +65,18 @@ teardown() {
cd "${OLD_PWD}"
run otelify.sh -d -- java -jar "${APP_DIR}/app.jar"
assert_output --partial 'io.opentelemetry.exporter.logging.LoggingSpanExporter'

# Test with debug flag
run otelify.sh -d -D -- java -jar "${APP_DIR}/app.jar"
assert_output --partial '[main] DEBUG io.opentelemetry.javaagent.'
}

#bats test_tags=integration:dotnet, dotnet
@test "can otelify dotnet applications" {
run otelify.sh -d -- dotnet run --project "${APP_DIR}"
assert_output --partial 'Activity.TraceId:'

# Test with debug flag
# run otelify.sh -D -d -- dotnet run --project "${APP_DIR}"
# assert_output --partial 'to be done'
}

0 comments on commit f43e825

Please sign in to comment.