Skip to content

Commit ce6ee89

Browse files
authored
Fixed the issue where systemd services are not available (#277)
1 parent 56cef21 commit ce6ee89

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

scripts/auto-instrumentation/instrumentation-java-so-tomcat.sh

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,30 @@ update_middleware_credentials() {
133133
# Function to detect Java services
134134
detect_java_services() {
135135
local services=()
136-
for service in $(systemctl list-units --type=service --state=active --no-pager --no-legend | awk '{print $1}' | grep -E '\.(service)$'); do
136+
# Add timeout to prevent hanging when systemd is not available or slow
137+
local service_list
138+
if ! service_list=$(timeout 10 systemctl list-units --type=service --state=active --no-pager --no-legend 2>/dev/null); then
139+
# If systemctl fails or times out, return empty array
140+
return 0
141+
fi
142+
143+
for service in $(echo "$service_list" | awk '{print $1}' | grep -E '\.(service)$'); do
144+
# Skip empty service names
145+
if [ -z "$service" ]; then
146+
continue
147+
fi
137148
# Check if service runs Java
138149
local exec_start
139150
exec_start=$(systemctl show "$service" --property=ExecStart --no-pager 2>/dev/null | cut -d'=' -f2- | tr -d '"')
140151
if echo "$exec_start" | grep -q "java"; then
141152
services+=("$service")
142153
fi
143154
done
144-
echo "${services[@]}"
155+
156+
# Only echo services if we have any
157+
if [ ${#services[@]} -gt 0 ]; then
158+
echo "${services[@]}"
159+
fi
145160
}
146161

147162
# Function to detect Java Docker containers
@@ -1431,16 +1446,25 @@ list_instrumented_apps() {
14311446
local java_services
14321447
mapfile -t java_services < <(detect_java_services)
14331448

1434-
for service in "${java_services[@]}"; do
1435-
local env_output
1436-
env_output=$(systemctl show "$service" --property=Environment --no-pager 2>/dev/null)
1437-
if echo "$env_output" | grep -q "OTEL_EXPORTER_OTLP_ENDPOINT"; then
1438-
info "✓ Service: $service (instrumented)"
1439-
instrumented_count=$((instrumented_count + 1))
1440-
else
1441-
info "✗ Service: $service (not instrumented)"
1442-
fi
1443-
done
1449+
# Handle case where no Java services are found
1450+
if [ ${#java_services[@]} -eq 0 ] || [ -z "${java_services[0]}" ]; then
1451+
info "No Java services found"
1452+
else
1453+
for service in "${java_services[@]}"; do
1454+
# Skip empty service names
1455+
if [ -z "$service" ]; then
1456+
continue
1457+
fi
1458+
local env_output
1459+
env_output=$(systemctl show "$service" --property=Environment --no-pager 2>/dev/null)
1460+
if echo "$env_output" | grep -q "OTEL_EXPORTER_OTLP_ENDPOINT"; then
1461+
info "✓ Service: $service (instrumented)"
1462+
instrumented_count=$((instrumented_count + 1))
1463+
else
1464+
info "✗ Service: $service (not instrumented)"
1465+
fi
1466+
done
1467+
fi
14441468

14451469
# Check Docker containers
14461470
info "=== Docker Containers ==="

0 commit comments

Comments
 (0)