Skip to content

Commit 442029a

Browse files
Add splunk.zc.method attribute to linux installer script (#3638)
1 parent 236b525 commit 442029a

File tree

2 files changed

+252
-173
lines changed

2 files changed

+252
-173
lines changed

internal/buildscripts/packaging/installer/install.sh

Lines changed: 133 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ if [ "$distro_codename" = "stretch" ]; then
103103
default_td_agent_version="$default_td_agent_version_stretch"
104104
fi
105105

106+
preload_path="/etc/ld.so.preload"
106107
default_instrumentation_version="latest"
107108
default_deployment_environment=""
108109
instrumentation_config_path="/usr/lib/splunk-instrumentation/instrumentation.conf"
@@ -292,23 +293,24 @@ install_yum_package() {
292293
ensure_not_installed() {
293294
local with_fluentd="$1"
294295
local with_instrumentation="$2"
295-
local agents="otelcol"
296+
local otelcol_path=$( command -v otelcol 2>/dev/null || true )
297+
local td_agent_path=$( command -v td-agent 2>/dev/null || true )
296298

297-
if [ "$with_fluentd" = "true" ]; then
298-
agents="$agents td-agent"
299+
if [ -n "$otelcol_path" ]; then
300+
echo "$otelcol_path already exists which implies that the collector is already installed." >&2
301+
echo "Please uninstall the collector, or try running this script with the '--uninstall' option." >&2
302+
exit 1
299303
fi
300304

301-
for agent in $agents; do
302-
if command -v $agent >/dev/null 2>&1; then
303-
echo "An agent binary already exists at $( command -v $agent ) which implies that the agent has already been installed." >&2
304-
echo "Please uninstall the agent and re-run this script." >&2
305-
exit 1
306-
fi
307-
done
305+
if [ "$with_fluentd" = "true" ] && [ -n "$td_agent_path" ]; then
306+
echo "$td_agent_path already exists which implies that fluentd/td-agent is already installed." >&2
307+
echo "Please uninstall fluentd/td-agent, or try running this script with the '--uninstall' option." >&2
308+
exit 1
309+
fi
308310

309311
if [ "$with_instrumentation" = "true" ] && [ -f "$instrumentation_so_path" ]; then
310-
echo "$instrumentation_so_path already exists which implies that the instrumentation library has already been installed." >&2
311-
echo "Please uninstall the instrumentation library and re-run this script" >&2
312+
echo "$instrumentation_so_path already exists which implies that auto instrumentation is already installed." >&2
313+
echo "Please uninstall auto instrumentation, or try running this script with the '--uninstall' option." >&2
312314
exit 1
313315
fi
314316
}
@@ -411,71 +413,82 @@ configure_fluentd() {
411413
fi
412414
}
413415

414-
update_deployment_environment() {
415-
local deployment_environment="$1"
416+
backup_file() {
417+
local path="$1"
416418

417-
if grep -q -E "^resource_attributes=.*deployment\.environment=${deployment_environment}(,|$)" "$instrumentation_config_path"; then
418-
echo "The 'deployment.environment=${deployment_environment}' resource attribute already exists in ${instrumentation_config_path}"
419-
else
420-
echo "Adding 'deployment.environment=${deployment_environment}' resource attribute to $instrumentation_config_path"
421-
if grep -q '^resource_attributes=' "$instrumentation_config_path"; then
422-
# traverse through existing resource attributes to add/update deployment.environment
423-
deployment_environment_found="false"
424-
attributes=""
425-
for i in $(grep '^resource_attributes=' "$instrumentation_config_path" | sed 's|^resource_attributes=||' | sed 's|,| |g'); do
426-
key="$(echo "$i" | cut -d= -f1)"
427-
value="$(echo "$i" | cut -d= -f2)"
428-
if [ "$key" = "deployment.environment" ]; then
429-
deployment_environment_found="true"
430-
value="$deployment_environment"
431-
fi
432-
attributes="${attributes},${key}=${value}"
433-
done
434-
if [ "$deployment_environment_found" != "true" ]; then
435-
attributes="${attributes},deployment.environment=${deployment_environment}"
436-
fi
437-
sed -i "s|^resource_attributes=.*|resource_attributes=${attributes#,}|" "$instrumentation_config_path"
438-
else
439-
# "resource_attributes=" line not found, simply append the line to the config file
440-
echo "resource_attributes=deployment.environment=${deployment_environment}" >> "$instrumentation_config_path"
441-
fi
419+
if [ -f "$path" ]; then
420+
ts="$(date '+%Y%m%d-%H%M%S')"
421+
echo "Backing up $path as ${path}.bak.${ts}"
422+
cp "$path" "${path}.bak.${ts}"
442423
fi
443424
}
444425

445-
update_instrumentation_option() {
446-
local option="$1"
447-
local value="$2"
426+
get_package_version() {
427+
local package="$1"
428+
local version=""
448429

449-
if grep -q -E "^${option}=.*" "$instrumentation_config_path"; then
450-
# overwrite existing option=value
451-
sed -i "s|^${option}=.*|${option}=${value}|" "$instrumentation_config_path"
430+
case "$distro" in
431+
ubuntu|debian)
432+
version="$( dpkg-query --showformat='${Version}' --show $package )"
433+
;;
434+
*)
435+
version="$( rpm -q --queryformat='%{VERSION}' $package )"
436+
;;
437+
esac
438+
439+
echo -n "$version"
440+
}
441+
442+
enable_preload() {
443+
if [ -f "$preload_path" ]; then
444+
if ! grep -q "$instrumentation_so_path" "$preload_path"; then
445+
backup_file "$preload_path"
446+
echo "Adding $instrumentation_so_path to $preload_path"
447+
echo "$instrumentation_so_path" >> "$preload_path"
448+
fi
452449
else
453-
# append option=value
454-
echo "${option}=${value}" >> "$instrumentation_config_path"
450+
echo "Adding $instrumentation_so_path to $preload_path"
451+
echo "$instrumentation_so_path" >> "$preload_path"
455452
fi
456453
}
457454

458-
update_instrumentation_config() {
455+
disable_preload() {
456+
if [ -f "$preload_path" ] && grep -q "$instrumentation_so_path" "$preload_path"; then
457+
backup_file "$preload_path"
458+
echo "Removing ${instrumentation_so_path} from ${preload_path}"
459+
sed -i -e "s|$instrumentation_so_path||" "$preload_path"
460+
if [ ! -s "$preload_path" ] || ! grep -q '[^[:space:]]' "$preload_path"; then
461+
echo "Removing empty ${preload_path}"
462+
rm -f "$preload_path"
463+
fi
464+
fi
465+
}
466+
467+
create_instrumentation_config() {
459468
local deployment_environment="$1"
469+
local version="$( get_package_version splunk-otel-auto-instrumentation )"
470+
local resource_attributes="splunk.zc.method=splunk-otel-auto-instrumentation-${version}"
471+
472+
backup_file "$instrumentation_config_path"
473+
474+
echo "Creating ${instrumentation_config_path}"
475+
cat <<EOH > $instrumentation_config_path
476+
java_agent_jar=${instrumentation_jar_path}
477+
generate_service_name=${generate_service_name}
478+
disable_telemetry=${disable_telemetry}
479+
enable_profiler=${enable_profiler}
480+
enable_profiler_memory=${enable_profiler_memory}
481+
enable_metrics=${enable_metrics}
482+
EOH
460483

461-
if [ -f "$instrumentation_config_path" ]; then
462-
ts="$(date '+%Y%m%d-%H%M%S')"
463-
echo "Backing up $instrumentation_config_path as ${instrumentation_config_path}.bak.${ts}"
464-
cp "$instrumentation_config_path" "${instrumentation_config_path}.bak.${ts}"
465-
if [ -n "$deployment_environment" ]; then
466-
update_deployment_environment "$deployment_environment"
467-
fi
468-
if [ -n "$service_name" ]; then
469-
update_instrumentation_option "service_name" "$service_name"
470-
fi
471-
update_instrumentation_option "generate_service_name" "$generate_service_name"
472-
update_instrumentation_option "disable_telemetry" "$disable_telemetry"
473-
update_instrumentation_option "enable_profiler" "$enable_profiler"
474-
update_instrumentation_option "enable_profiler_memory" "$enable_profiler_memory"
475-
update_instrumentation_option "enable_metrics" "$enable_metrics"
476-
else
477-
echo "$instrumentation_config_path not found!" >&2
478-
exit 1
484+
if [ -n "$deployment_environment" ]; then
485+
resource_attributes="${resource_attributes},deployment.environment=${deployment_environment}"
486+
fi
487+
488+
echo "resource_attributes=${resource_attributes%,}" >> $instrumentation_config_path
489+
490+
if [ -n "$service_name" ]; then
491+
echo "service_name=${service_name}" >> $instrumentation_config_path
479492
fi
480493
}
481494

@@ -626,6 +639,8 @@ If access_token is not provided, it will be prompted for on stdin.
626639
627640
Options:
628641
642+
Collector:
643+
-- <access_token> Use '--' if access_token starts with '-'.
629644
--api-url <url> Set the api endpoint URL explicitly instead of the endpoint inferred from the
630645
specified realm.
631646
(default: https://api.REALM.signalfx.com)
@@ -670,20 +685,28 @@ Options:
670685
the collector deb/rpm package from $repo_base.
671686
Specify this option to skip this step and use a pre-configured repo on the
672687
target system that provides the 'splunk-otel-collector' deb/rpm package.
673-
--skip-fluentd-repo By default, a apt/yum repo definition file will be created to download the
674-
fluentd deb/rpm package from $td_agent_repo_base.
675-
Specify this option to skip this step and use a pre-configured repo on the
676-
target system that provides the 'td-agent' deb/rpm package.
677688
--test Use the test package repo instead of the primary.
678689
--trace-url <url> Set the trace endpoint URL explicitly instead of the endpoint inferred from the
679690
specified realm.
680691
(default: https://ingest.REALM.signalfx.com/v2/trace)
681-
--uninstall Removes the Splunk OpenTelemetry Collector for Linux.
692+
693+
Fluentd:
682694
--with[out]-fluentd Whether to install and configure fluentd to forward log events to the collector.
683695
(default: --without-fluentd)
684-
--with[out]-instrumentation Whether to install and configure the splunk-otel-auto-instrumentation package.
696+
--skip-fluentd-repo By default, a apt/yum repo definition file will be created to download the
697+
fluentd deb/rpm package from $td_agent_repo_base.
698+
Specify this option to skip this step and use a pre-configured repo on the
699+
target system that provides the 'td-agent' deb/rpm package.
700+
Only applicable if the '--with-fluentd' is also specified.
701+
702+
Auto Instrumentation:
703+
--with[out]-instrumentation Whether to install the splunk-otel-auto-instrumentation package and add the
704+
libsplunk.so shared object library to /etc/ld.so.preload to enable auto
705+
instrumentation for all supported processes on the host.
685706
(default: --without-instrumentation)
686707
--deployment-environment <value> Set the 'deployment.environment' resource attribute to the specified value.
708+
If not specified, the "Environment" in the Splunk APM UI will appear as
709+
"unknown" for the auto instrumented application(s).
687710
Only applicable if the '--with-instrumentation' option is also specified.
688711
(default: empty)
689712
--service-name <name> Override the auto-generated service names for all instrumented Java applications
@@ -710,7 +733,10 @@ Options:
710733
--instrumentation-version The splunk-otel-auto-instrumentation package version to install.
711734
Only applicable if the '--with-instrumentation' option is also specified.
712735
(default: $default_instrumentation_version)
713-
-- Use '--' if access_token starts with '-'.
736+
737+
Uninstall:
738+
--uninstall Removes the Splunk OpenTelemetry Collector for Linux, Fluentd, and Splunk
739+
OpenTelemetry Auto Instrumentation packages, if installed.
714740
715741
EOH
716742
exit 0
@@ -950,11 +976,9 @@ parse_args_and_install() {
950976
uninstall="true"
951977
;;
952978
--with-fluentd)
979+
with_fluentd="true"
953980
if ! fluentd_supported; then
954981
echo "WARNING: Ignoring the --with-fluentd option since fluentd is currently not supported for ${distro}:${distro_version} ${distro_arch}." >&2
955-
with_fluentd="false"
956-
else
957-
with_fluentd="true"
958982
fi
959983
;;
960984
--without-fluentd)
@@ -1038,12 +1062,6 @@ parse_args_and_install() {
10381062
exit 0
10391063
fi
10401064

1041-
if ! fluentd_supported; then
1042-
with_fluentd="false"
1043-
fi
1044-
1045-
ensure_not_installed "$with_fluentd" "$with_instrumentation"
1046-
10471065
if [ -z "$access_token" ]; then
10481066
access_token=$(request_access_token)
10491067
fi
@@ -1064,19 +1082,22 @@ parse_args_and_install() {
10641082
hec_url="${ingest_url}/v1/log"
10651083
fi
10661084

1067-
if [ "$with_fluentd" != "true" ]; then
1085+
if [ "$with_fluentd" != "true" ] || ! fluentd_supported; then
10681086
td_agent_version=""
10691087
fi
10701088

10711089
if [ "$with_instrumentation" != "true" ]; then
10721090
instrumentation_version=""
1073-
deployment_environment=""
10741091
fi
10751092

10761093
if [ -z "$trace_url" ]; then
10771094
trace_url="${ingest_url}/v2/trace"
10781095
fi
10791096

1097+
check_support
1098+
1099+
ensure_not_installed "$with_fluentd" "$with_instrumentation"
1100+
10801101
echo "Splunk OpenTelemetry Collector Version: ${collector_version}"
10811102
if [ -n "$ballast" ]; then
10821103
echo "Ballast Size in MIB: $ballast"
@@ -1088,15 +1109,25 @@ parse_args_and_install() {
10881109
echo "API Endpoint: $api_url"
10891110
echo "Trace Endpoint: $trace_url"
10901111
echo "HEC Endpoint: $hec_url"
1091-
if [ "$with_fluentd" = "true" ]; then
1112+
if [ -n "$td_agent_version" ]; then
10921113
echo "TD Agent (Fluentd) Version: $td_agent_version"
10931114
fi
1094-
if [ "$with_instrumentation" = "true" ]; then
1115+
if [ -n "$instrumentation_version" ]; then
10951116
echo "Splunk OpenTelemetry Auto Instrumentation Version: $instrumentation_version"
10961117
if [ -n "$deployment_environment" ]; then
1097-
echo " Resource Attribute: deployment.environment=${deployment_environment}"
1118+
echo " Deployment environment: $deployment_environment"
1119+
else
1120+
echo " Deployment environment: unknown"
10981121
fi
1122+
if [ -n "$service_name" ]; then
1123+
echo " Service name: $service_name"
1124+
else
1125+
echo " Service name: auto-generated"
1126+
fi
1127+
echo " AlwaysOn Profiling enabled: $enable_profiler"
1128+
echo " AlwaysOn Memory Profiling enabled: $enable_profiler_memory"
10991129
fi
1130+
echo
11001131

11011132
if [ "${VERIFY_ACCESS_TOKEN:-true}" = "true" ] && ! verify_access_token "$access_token" "$ingest_url" "$insecure"; then
11021133
echo "Your access token could not be verified. This may be due to a network connectivity issue or an invalid access token." >&2
@@ -1106,7 +1137,9 @@ parse_args_and_install() {
11061137
install "$stage" "$collector_version" "$td_agent_version" "$skip_collector_repo" "$skip_fluentd_repo" "$instrumentation_version"
11071138

11081139
if [ "$with_instrumentation" = "true" ]; then
1109-
update_instrumentation_config "$deployment_environment"
1140+
# add libsplunk.so to /etc/ld.so.preload if it was not added automatically by the instrumentation package
1141+
enable_preload
1142+
create_instrumentation_config "$deployment_environment"
11101143
fi
11111144

11121145
create_user_group "$service_user" "$service_group"
@@ -1200,7 +1233,7 @@ parse_args_and_install() {
12001233
systemctl daemon-reload
12011234
systemctl restart splunk-otel-collector
12021235

1203-
if [ "$with_fluentd" = "true" ]; then
1236+
if [ -n "$td_agent_version" ]; then
12041237
# only start fluentd with our custom config to avoid port conflicts within the default config
12051238
systemctl stop td-agent
12061239
if [ -f "$fluent_config_path" ]; then
@@ -1214,6 +1247,7 @@ parse_args_and_install() {
12141247
fi
12151248
fi
12161249

1250+
echo
12171251
cat <<EOH
12181252
The Splunk OpenTelemetry Collector for Linux has been successfully installed.
12191253
@@ -1229,7 +1263,7 @@ must be restarted to apply the changes by running the following command as root:
12291263
12301264
EOH
12311265

1232-
if [ "$with_fluentd" = "true" ] && [ -f "$fluent_config_path" ]; then
1266+
if [ -n "$td_agent_version" ] && [ -f "$fluent_config_path" ]; then
12331267
cat <<EOH
12341268
Fluentd has been installed and configured to forward log events to the Splunk OpenTelemetry Collector.
12351269
By default, all log events with the @SPLUNK label will be forwarded to the collector.
@@ -1259,13 +1293,19 @@ The Splunk OpenTelemetry Auto Instrumentation package has been installed.
12591293
/etc/ld.so.preload has been configured for the instrumentation library at $instrumentation_so_path.
12601294
The configuration file is located at $instrumentation_config_path.
12611295
1262-
The Java application(s) on the host need to be manually started/restarted.
1296+
Reboot the system or restart the Java application(s) for auto instrumentation to take effect.
1297+
1298+
EOH
1299+
fi
1300+
1301+
if [ "$with_fluentd" = "true" ] && ! fluentd_supported; then
1302+
cat <<EOH >&2
1303+
WARNING: Fluentd was not installed since it is currently not supported for ${distro}:${distro_version} ${distro_arch}
12631304
12641305
EOH
12651306
fi
1307+
12661308
exit 0
12671309
}
12681310

1269-
check_support
1270-
12711311
parse_args_and_install $@

0 commit comments

Comments
 (0)