Skip to content

Commit

Permalink
Fix log forwarding for driver flavour
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Gildein <robert.gildein@canonical.com>
  • Loading branch information
rgildein committed Oct 14, 2024
1 parent 984dd8e commit 349f37a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
44 changes: 29 additions & 15 deletions images/charmed-spark/bin/sparkd.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
#!/bin/bash

function get_loki_url {
if [ ! -z "$LOKI_URL" ]; then
echo "$(<<< "$LOKI_URL" sed -e 's`[][\\/.*^$]`\\&`g')"
elif [ -f "$SPARK_CONF_DIR/spark.properties" ]; then
loki_url="$(grep -oP '(?<=spark.executorEnv.LOKI_URL=).*' $SPARK_CONF_DIR/spark.properties)"
echo "$(<<< "$loki_url" sed -e 's/\\:/:/g' -e 's`[][\\/.*^$]`\\&`g')"
fi
}

function get_log_layer {
LOG_LAYER_FILE="/opt/pebble/log-layer.yaml"
ESCAPED_LOKI_URL="$(<<< "$LOKI_URL" sed -e 's`[][\\/.*^$]`\\&`g')"
sed -e "s/\$LOKI_URL/$ESCAPED_LOKI_URL/g" \
local loki_url=$1
local log_layer_file=${2-"/opt/pebble/log-layer.yaml"}
sed -e "s/\$LOKI_URL/$loki_url/g" \
-e "s/\$FLAVOUR/$FLAVOUR/g" \
-e "s/\$SPARK_APPLICATION_ID/$SPARK_APPLICATION_ID/g" \
-e "s/\$SPARK_USER/$SPARK_USER/g" \
-e "s/\$SPARK_EXECUTOR_POD_NAME/$SPARK_EXECUTOR_POD_NAME/g" \
$LOG_LAYER_FILE
-e "s/\$HOSTNAME/$HOSTNAME/g" \
$log_layer_file
}

function log_forwarding {
local loki_url=$(get_loki_url)
if [ ! -z "$loki_url" ]; then
echo "Log-forwarding to Loki is enabled"
local rendered_log_layer=$(get_log_layer $loki_url)
echo "$rendered_log_layer" | tee /tmp/rendered_log_layer.yaml
pebble add logging /tmp/rendered_log_layer.yaml
else
echo "Log-forwarding to Loki is disabled."
fi
}

function finish {
Expand All @@ -19,26 +41,18 @@ function finish {
}
trap finish EXIT

if [ ! -z "${LOKI_URL}" ]
then
echo "Configuring log-forwarding to Loki."
RENDERED_LOG_LAYER=$(get_log_layer)
echo "$RENDERED_LOG_LAYER" | tee /tmp/rendered_log_layer.yaml
pebble add logging /tmp/rendered_log_layer.yaml
else
echo "Log-forwarding to Loki is disabled."
fi

FLAVOUR=$1

echo "Running script with ${FLAVOUR} flavour"

case "${FLAVOUR}" in
driver|executor)
log_forwarding
pushd /opt/spark
./entrypoint.sh "$@"
;;
"")
log_forwarding
# Infinite sleep to allow pebble to be running indefinitely
sleep inf
;;
Expand Down
3 changes: 2 additions & 1 deletion images/charmed-spark/pebble/log-layer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ log-targets:
services: [all]
labels:
product: charmed-spark
flavour: $FLAVOUR
app: spark
app_id: $SPARK_APPLICATION_ID
user: $SPARK_USER
pod: $SPARK_EXECUTOR_POD_NAME
hostname: $HOSTNAME

0 comments on commit 349f37a

Please sign in to comment.