Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions scripts/auto-instrumentation/instrumentation-java-so-tomcat.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
# otel-system-wide-java-extended.sh
# Install OpenTelemetry Java agent for host JVMs, provide docker-run wrapper,
Expand Down Expand Up @@ -133,15 +133,30 @@
# Function to detect Java services
detect_java_services() {
local services=()
for service in $(systemctl list-units --type=service --state=active --no-pager --no-legend | awk '{print $1}' | grep -E '\.(service)$'); do
# Add timeout to prevent hanging when systemd is not available or slow
local service_list
if ! service_list=$(timeout 10 systemctl list-units --type=service --state=active --no-pager --no-legend 2>/dev/null); then
# If systemctl fails or times out, return empty array
return 0
fi

for service in $(echo "$service_list" | awk '{print $1}' | grep -E '\.(service)$'); do
# Skip empty service names
if [ -z "$service" ]; then
continue
fi
# Check if service runs Java
local exec_start
exec_start=$(systemctl show "$service" --property=ExecStart --no-pager 2>/dev/null | cut -d'=' -f2- | tr -d '"')
if echo "$exec_start" | grep -q "java"; then
services+=("$service")
fi
done
echo "${services[@]}"

# Only echo services if we have any
if [ ${#services[@]} -gt 0 ]; then
echo "${services[@]}"
fi
}

# Function to detect Java Docker containers
Expand Down Expand Up @@ -1431,16 +1446,25 @@
local java_services
mapfile -t java_services < <(detect_java_services)

for service in "${java_services[@]}"; do
local env_output
env_output=$(systemctl show "$service" --property=Environment --no-pager 2>/dev/null)
if echo "$env_output" | grep -q "OTEL_EXPORTER_OTLP_ENDPOINT"; then
info "✓ Service: $service (instrumented)"
instrumented_count=$((instrumented_count + 1))
else
info "✗ Service: $service (not instrumented)"
fi
done
# Handle case where no Java services are found
if [ ${#java_services[@]} -eq 0 ] || [ -z "${java_services[0]}" ]; then
info "No Java services found"
else
for service in "${java_services[@]}"; do
# Skip empty service names
if [ -z "$service" ]; then
continue
fi
local env_output
env_output=$(systemctl show "$service" --property=Environment --no-pager 2>/dev/null)
if echo "$env_output" | grep -q "OTEL_EXPORTER_OTLP_ENDPOINT"; then
info "✓ Service: $service (instrumented)"
instrumented_count=$((instrumented_count + 1))
else
info "✗ Service: $service (not instrumented)"
fi
done
fi

# Check Docker containers
info "=== Docker Containers ==="
Expand Down
Loading