Skip to content

Add reload logic to collector container start script. #4099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2025
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
42 changes: 30 additions & 12 deletions internal/collector/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package collector

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"

Expand All @@ -16,6 +17,8 @@ import (
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)

const configDirectory = "/etc/otel-collector"

// AddToConfigMap populates the shared ConfigMap with fields needed to run the Collector.
func AddToConfigMap(
ctx context.Context,
Expand Down Expand Up @@ -50,7 +53,7 @@ func AddToPod(
// Create volume and volume mount for otel collector config
configVolumeMount := corev1.VolumeMount{
Name: "collector-config",
MountPath: "/etc/otel-collector",
MountPath: configDirectory,
ReadOnly: true,
}
configVolume := corev1.Volume{Name: configVolumeMount.Name}
Expand Down Expand Up @@ -144,22 +147,37 @@ func AddToPod(

// startCommand generates the command script used by the collector container
func startCommand(includeLogrotate bool) []string {
var startScript = `
/otelcol-contrib --config /etc/otel-collector/config.yaml
`

var logrotateCommand string
if includeLogrotate {
startScript = `
/otelcol-contrib --config /etc/otel-collector/config.yaml &
logrotateCommand = `logrotate -s /tmp/logrotate.status /etc/logrotate.d/logrotate.conf`
}

var startScript = fmt.Sprintf(`
OTEL_PIDFILE=/tmp/otel.pid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the otelcontrib process have a name? Or did we want the PID to check if it's running (without recourse to pgrep)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly not sure of the best way to do this. I used some prior art in our codebase as a starting point


start_otel_collector() {
echo "Starting OTel Collector"
/otelcol-contrib --config %s/config.yaml &
echo $! > $OTEL_PIDFILE
}
start_otel_collector

exec {fd}<> <(:||:)
while read -r -t 5 -u "${fd}" ||:; do
logrotate -s /tmp/logrotate.status /etc/logrotate.d/logrotate.conf
%s
Comment on lines -157 to +167
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌱 Perhaps this could always be [[ -r /etc/logrotate.d/logrotate.conf ]] && logrotate ... and the choice to rotate or not would be at the mounting of the config.

if [[ "${directory}" -nt "/proc/self/fd/${fd}" ]] && kill -HUP $(head -1 ${OTEL_PIDFILE?});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 What happens if Bash is terminated? Is the entire container torn down, including the collector?

If so, we can keep the collector pid in a variable in memory.

# start
otel ... &
OTEL_PID=$!

# here
kill -HUP ${OTEL_PID}

# below
[[ ! -e /proc/${OTEL_PID} ]]

then
echo "OTel configuration changed..."
exec {fd}>&- && exec {fd}<> <(:||:)
stat --format='Loaded configuration dated %%y' "${directory}"
fi
if [[ ! -e /proc/$(head -1 ${OTEL_PIDFILE?}) ]] ; then
start_otel_collector
fi
done
`
}
`, configDirectory, logrotateCommand)

wrapper := `monitor() {` + startScript + `}; export -f monitor; exec -a "$0" bash -ceu monitor`
wrapper := `monitor() {` + startScript + `}; export directory="$1"; export -f monitor; exec -a "$0" bash -ceu monitor`

return []string{"bash", "-ceu", "--", wrapper, "collector"}
return []string{"bash", "-ceu", "--", wrapper, "collector", configDirectory}
}
Loading