Skip to content

Commit

Permalink
enable configurable RUM destination in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPSplunk committed Jun 25, 2024
1 parent 40ea0cf commit cccb670
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ RUN npm run grpc:generate
RUN npm run build

FROM node:18-alpine AS runner
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

WORKDIR /app
RUN chown nextjs /app
RUN apk add --no-cache protobuf-dev protoc

ENV NODE_ENV=production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/utils/telemetry/Instrumentation.js ./
COPY --from=builder /app/public ./public
Expand All @@ -39,6 +40,7 @@ COPY --from=deps /app/node_modules ./node_modules

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/config_rum_and_start .

USER nextjs

Expand Down
53 changes: 53 additions & 0 deletions src/frontend/config_rum_and_start
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
chunksDir=/app/.next/static/chunks
envFile=/app/splunk_rum_env

noRumStart() {
echo "Frontend starting with no RUM support."
echo "You will probably see browser console errors."
cd /app
npm start
exit
}


if ! [ -f $envFile ]; then
echo "Config file \"$envFile\" for Splunk Rum not found."
noRumStart
fi

# Splunk Rum config file found. Try to substitute SPLUNK_RUM_PLACEHOLDER values
cd $chunksDir
mainfile=$(ls main-*.js)

if [ "$mainfile" == '' ] ; then
# Next.js main chunk file not found
echo "Did not find main file in $chunksDir"
noRumStart
fi

if echo "$mainfile" | grep -q ' ' ; then
# Uh-oh, got multiple mainfiles.
echo "Found multiple main files in $chunksDir"
noRumStart
fi

echo splunk_rum_env file contains the following values:
cat $envFile
. $envFile
sed -e "s/SPLUNK_RUM_REALM_PLACEHOLDER/$SPLUNK_RUM_REALM/" \
-e "s/SPLUNK_RUM_TOKEN_PLACEHOLDER/$SPLUNK_RUM_TOKEN/" \
-e "s/SPLUNK_RUM_APP_PLACEHOLDER/$SPLUNK_RUM_APP_NAME/" \
-e "s/SPLUNK_RUM_ENV_PLACEHOLDER/$SPLUNK_RUM_ENV/" < $mainfile > /tmp/${mainfile}.new

if [ "$?" -ne 0 ]; then
# Some error while running the sed command. Do a fallback start
echo "Encountered an error while trying to substitute RUM config placeholders"
noRumStart
fi

# All looks good! We should be able to do a regular start
cp ${mainfile} /tmp/$mainfile.orig
cp /tmp/${mainfile}.new $mainfile
cd /app
npm start

12 changes: 6 additions & 6 deletions src/frontend/splunk-rum.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import SplunkRum from '@splunk/otel-web';

SplunkRum.init({
realm: "us1",
rumAccessToken: "5UxP7X9NINTxugmccK8kaA",
applicationName: "astroshop_lab",
deploymentEnvironment: "astroshop_lab"
realm: "SPLUNK_RUM_REALM_PLACEHOLDER",
rumAccessToken: "SPLUNK_RUM_TOKEN_PLACEHOLDER",
applicationName: "SPLUNK_RUM_APP_PLACEHOLDER",
deploymentEnvironment: "SPLUNK_RUM_ENV_PLACEHOLDER"
});

//SplunkRum.init({
// realm: "us1",
// rumAccessToken: "Qad35m7E6ld6p_-O_RxwEg",
// applicationName: "astroshop",
// deploymentEnvironment: "astroshop"
// applicationName: "astroshop_lab_2",
// deploymentEnvironment: "astroshop_lab_2"
//});

0 comments on commit cccb670

Please sign in to comment.