diff --git a/Dockerfile b/Dockerfile index b9c991dec..c723a0884 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,6 +19,17 @@ RUN mkdir -p /opt/node_exporter && \ tar xvz --strip-components=1 -C /opt/node_exporter && \ ln -s /opt/node_exporter/node_exporter /usr/local/bin/ +# Install OpenTelemetry Collector +ENV OTEL_VERSION=0.97.0 +RUN curl -sSL https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v${OTEL_VERSION}/otelcol-contrib_${OTEL_VERSION}_linux_amd64.tar.gz \ + -o otelcol.tar.gz && \ + tar -xzf otelcol.tar.gz && \ + mv otelcol-contrib /usr/local/bin/ && \ + rm otelcol.tar.gz + +# Create directory for OpenTelemetry config +RUN mkdir -p /etc/otel + # Install cloudflared RUN curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb > cloudflared.deb \ && dpkg -i cloudflared.deb \ @@ -38,6 +49,9 @@ RUN mkdir -p .git && npm install # Copy the rest of the application code COPY . . +# Copy OpenTelemetry configuration +COPY otel-collector-config.yaml /etc/otel/config.yaml + # Build the client-side application RUN npm run build-prod diff --git a/metric-exporter.sh b/metric-exporter.sh deleted file mode 100755 index e3ac7370e..000000000 --- a/metric-exporter.sh +++ /dev/null @@ -1,103 +0,0 @@ -#!/bin/bash - -# Metric Collector for Prometheus Pushgateway -# This script collects metrics from Node Exporter and application sources -# and pushes them to a Prometheus Pushgateway with custom labels. - -# Configuration -NODE_EXPORTER_URL="http://localhost:9100/metrics" -APP_METRICS_URL="http://localhost:9090/metrics" -PUSHGATEWAY_BASE_URL="https://mon.openfront.io/pushgateway/metrics" -AUTH=$MON_USERNAME:$MON_PASSWORD -INTERVAL=15 # seconds - -# Function to fetch metrics from Node Exporter -fetch_node_exporter_metrics() { - curl -s --connect-timeout 5 --max-time 10 "$NODE_EXPORTER_URL" || - echo "# Error fetching Node Exporter metrics" -} - -# Function to fetch metrics from your application -fetch_app_metrics() { - curl -s --connect-timeout 5 --max-time 10 "$APP_METRICS_URL" || - echo "# Error fetching application metrics" -} - -# Function to push metrics to Pushgateway -push_metrics() { - local metrics=$1 - local job_name=$2 - - echo "Pushing $job_name metrics to Pushgateway..." - - # Create a temporary file for the metrics - TEMP_FILE=$(mktemp) - echo "$metrics" > "$TEMP_FILE" - - # Push to Pushgateway with instance label - curl -s -u "$AUTH" --data-binary @"$TEMP_FILE" \ - "$PUSHGATEWAY_BASE_URL/job/$job_name/instance/$HOST" - - # Check if push was successful - if [ $? -eq 0 ]; then - echo "$job_name metrics pushed successfully" - else - echo "Error pushing $job_name metrics" - fi - - # Remove temporary file - rm "$TEMP_FILE" -} - -# Function to add labels to metrics -add_labels() { - local metrics=$1 - - # First, handle metrics with existing labels - metrics=$(echo "$metrics" | sed -E 's/(\{[^}]*)\}/\1,env="'$ENV'",host="'$HOST'",subdomain="'$SUBDOMAIN'"}/g') - - # Then, handle metrics with no existing labels - metrics=$(echo "$metrics" | sed -E 's/^([a-zA-Z0-9_:]+)[ \t]+([0-9.e+-]+)$/\1{env="'$ENV'",host="'$HOST'",subdomain="'$SUBDOMAIN'"} \2/g') - - echo "$metrics" -} - -# Main function to collect and push metrics -collect_and_push_metrics() { - echo "Starting metrics collection cycle at $(date)" - - # Get metrics from both sources - NODE_METRICS=$(fetch_node_exporter_metrics) - APP_METRICS=$(fetch_app_metrics) - - # Clean up metrics (remove headers etc.) - NODE_METRICS=$(echo "$NODE_METRICS" | grep -v "^Fetching") - APP_METRICS=$(echo "$APP_METRICS" | grep -v "^Fetching") - - # Add labels to metrics - NODE_METRICS=$(add_labels "$NODE_METRICS") - APP_METRICS=$(add_labels "$APP_METRICS") - - # Push to Pushgateway separately - push_metrics "$NODE_METRICS" "node_exporter" - push_metrics "$APP_METRICS" "app_metrics" - - echo "Metrics collection cycle completed at $(date)" -} - -# Main execution -echo "===== Starting metrics collector =====" -echo "Environment: $ENV, HOST: $HOST, Subdomain: $SUBDOMAIN" -echo "Collecting and pushing metrics every $INTERVAL seconds" -echo "Node Exporter URL: $NODE_EXPORTER_URL" -echo "App Metrics URL: $APP_METRICS_URL" -echo "Pushgateway URL: $PUSHGATEWAY_BASE_URL" - -# Wait for app to be ready. -sleep 30 - -# Then set up interval loop -while true; do - sleep $INTERVAL - collect_and_push_metrics -done \ No newline at end of file diff --git a/otel-collector-config.yaml b/otel-collector-config.yaml new file mode 100644 index 000000000..2ffc17da6 --- /dev/null +++ b/otel-collector-config.yaml @@ -0,0 +1,63 @@ +receivers: + prometheus: + config: + scrape_configs: + - job_name: "node_exporter" + scrape_interval: 15s + static_configs: + - targets: ["localhost:9100"] + relabel_configs: + - target_label: "host" + replacement: "${HOST}" + - target_label: "env" + replacement: "${ENV}" + - target_label: "subdomain" + replacement: "${SUBDOMAIN}" + - target_label: "container_id" + replacement: "${HOSTNAME}" + + - job_name: "app_metrics" + scrape_interval: 15s + static_configs: + - targets: ["localhost:9090"] + relabel_configs: + - target_label: "host" + replacement: "${HOST}" + - target_label: "env" + replacement: "${ENV}" + - target_label: "subdomain" + replacement: "${SUBDOMAIN}" + - target_label: "container_id" + replacement: "${HOSTNAME}" + - target_label: "service" + replacement: "${SERVICE_NAME}" + +processors: + batch: + timeout: 10s + resource: + attributes: + - key: service.name + value: "${SERVICE_NAME}" + - key: container.id + value: "${HOSTNAME}" + +exporters: + prometheuspushgateway: + endpoint: "https://mon.openfront.io/pushgateway" + namespace: "${SERVICE_NAME}" + job: "${SERVICE_NAME}" + grouping_key: + host: "${HOST}" + env: "${ENV}" + subdomain: "${SUBDOMAIN}" + container_id: "${HOSTNAME}" + headers: + Authorization: "Basic ${BASIC_AUTH}" + +service: + pipelines: + metrics: + receivers: [prometheus] + processors: [batch, resource] + exporters: [prometheuspushgateway] diff --git a/supervisord.conf b/supervisord.conf index 16c3eb212..34f7cd70b 100644 --- a/supervisord.conf +++ b/supervisord.conf @@ -38,10 +38,9 @@ autorestart=true stdout_logfile=/var/log/node_exporter.log stderr_logfile=/var/log/node_exporter-err.log - -[program:metrics_exporter] -command=/usr/src/app/metric-exporter.sh +[program:otelcol] +command=/usr/local/bin/otelcol-contrib --config=/etc/otel/config.yaml autostart=true autorestart=true -stdout_logfile=/var/log/metrics-exporter.log -stderr_logfile=/var/log/metrics-exporter-err.log +stderr_logfile=/var/log/supervisor/otelcol-err.log +stdout_logfile=/var/log/supervisor/otelcol-out.log