Skip to content

Commit 4b43f9b

Browse files
feat: add runner_id, stack label, and write_relabel_configs for managed metrics
Parse runnerId and allowlistPrefixes from the metrics secret JSON. - Add runner_id to external_labels so every timeseries carries the Gitpod management plane runner UUID. - Add stack: gcp to external_labels for consistency with the in-process reporter. - Generate write_relabel_configs on the local remote_write target to filter metrics by allowlist prefixes. Only matching metrics are forwarded to the managed metrics pipeline. Co-authored-by: Ona <no-reply@ona.com>
1 parent 35a09e0 commit 4b43f9b

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

files/runner-cloud-init.tftpl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ write_files:
227227
scrape_timeout: 10s
228228
evaluation_interval: 15s
229229
external_labels:
230+
stack: gcp
230231
project_id: ${PROJECT_ID}
231232
region: ${REGION}
232233
runner_name: ${RUNNER_ID}
@@ -422,13 +423,21 @@ write_files:
422423
REMOTE_USER=$(echo "$SECRET_DATA" | jq -r '.user // ""')
423424
REMOTE_PASSWORD=$(echo "$SECRET_DATA" | jq -r '.password // ""')
424425
LOCAL_REMOTE_WRITE_URL=$(echo "$SECRET_DATA" | jq -r '.localRemoteWriteUrl // ""')
426+
RUNNER_ID=$(echo "$SECRET_DATA" | jq -r '.runnerId // ""')
427+
# Read allowlist as a bash array of prefixes
428+
mapfile -t ALLOWLIST_PREFIXES < <(echo "$SECRET_DATA" | jq -r '.allowlistPrefixes // [] | .[]')
425429

426-
log "Metrics enabled: $ENABLE_METRICS, URL: $REMOTE_URL, local remote write: $LOCAL_REMOTE_WRITE_URL"
430+
log "Metrics enabled: $ENABLE_METRICS, URL: $REMOTE_URL, local remote write: $LOCAL_REMOTE_WRITE_URL, runner_id: $RUNNER_ID, allowlist prefixes: ${#ALLOWLIST_PREFIXES[@]}"
427431

428432
# Generate final configuration using template substitution
429433
sed -e "s/{{INSTANCE_NAME}}/$INSTANCE_NAME/g" \
430434
/var/lib/prometheus/prometheus-template.yml > /tmp/prometheus.yml.new
431435

436+
# Add runner_id to external_labels if available
437+
if [ -n "$RUNNER_ID" ]; then
438+
sed -i "/external_labels:/a\\ runner_id: $RUNNER_ID" /tmp/prometheus.yml.new
439+
fi
440+
432441
# Build remote_write configuration from available targets
433442
HAS_REMOTE_WRITE=false
434443

@@ -460,6 +469,24 @@ write_files:
460469
HAS_REMOTE_WRITE=true
461470
fi
462471
echo " - url: $LOCAL_REMOTE_WRITE_URL" >> /tmp/prometheus.yml.new
472+
473+
# Add write_relabel_configs to filter by allowlist prefixes.
474+
# Only metrics matching these prefixes are forwarded to the
475+
# managed metrics pipeline. Uses a single regex with alternation.
476+
if [ ${#ALLOWLIST_PREFIXES[@]} -gt 0 ]; then
477+
# Build regex: (prefix1.*|prefix2.*|...)
478+
REGEX="("
479+
for i in "${!ALLOWLIST_PREFIXES[@]}"; do
480+
if [ "$i" -gt 0 ]; then REGEX+="|"; fi
481+
REGEX+="${ALLOWLIST_PREFIXES[$i]}.*"
482+
done
483+
REGEX+=")"
484+
485+
echo " write_relabel_configs:" >> /tmp/prometheus.yml.new
486+
echo " - source_labels: [__name__]" >> /tmp/prometheus.yml.new
487+
echo " regex: '$REGEX'" >> /tmp/prometheus.yml.new
488+
echo " action: keep" >> /tmp/prometheus.yml.new
489+
fi
463490
fi
464491

465492
# Check if configuration changed

0 commit comments

Comments
 (0)