#!/bin/bash # GoChat Prometheus Metrics Exporter Helper Script # Reference: Chatwoot monitoring setup pattern # Assists in configuring Prometheus scraping for GoChat metrics # # Usage: # ./scripts/prometheus_exporter.sh [--generate-config] [--scrape] [--check] # # Metrics exposed on port 9090 at /metrics (via Gin + prometheus client) # Key metrics: # - gochat_http_requests_total (counter, by method/path/status) # - gochat_http_request_duration_seconds (histogram) # - gochat_conversations_active (gauge) # - gochat_messages_sent_total (counter, by channel) # - gochat_websocket_connections (gauge) # - gochat_worker_jobs_processed_total (counter, by queue) # - gochat_worker_jobs_failed_total (counter, by queue) # - gochat_db_query_duration_seconds (histogram) # - gochat_redis_commands_total (counter) # - go_* (standard Go runtime metrics) set -euo pipefail GOCHAT_HOST="${GOCHAT_HOST:-localhost}" GOCHAT_METRICS_PORT="${GOCHAT_METRICS_PORT:-9090}" METRICS_URL="http://${GOCHAT_HOST}:${GOCHAT_METRICS_PORT}/metrics" PROMETHEUS_CONFIG_DIR="${PROMETHEUS_CONFIG_DIR:-/etc/prometheus}" TIMEOUT="${TIMEOUT:-5}" # Parse arguments ACTION="check" while [[ $# -gt 0 ]]; do case "$1" in --generate-config) ACTION="generate-config"; shift ;; --scrape) ACTION="scrape"; shift ;; --check) ACTION="check"; shift ;; --help) echo "Usage: $0 [--generate-config] [--scrape] [--check]"; exit 0 ;; *) echo "Unknown option: $1"; exit 1 ;; esac done case "${ACTION}" in generate-config) echo "# GoChat Prometheus scrape configuration" echo "# Add this to your prometheus.yml scrape_configs section" echo "" echo "scrape_configs:" echo " - job_name: 'gochat'" echo " scrape_interval: 15s" echo " scrape_timeout: 10s" echo " metrics_path: '/metrics'" echo " static_configs:" echo " - targets:" echo " - '${GOCHAT_HOST}:${GOCHAT_METRICS_PORT}'" echo " labels:" echo " app: 'gochat'" echo " env: '${GOCHAT_ENV:-production}'" echo "" echo "# For Kubernetes with ServiceMonitor (preferred):" echo "# The Helm chart creates a ServiceMonitor automatically when" echo "# metrics.enabled=true and metrics.serviceMonitor.enabled=true" echo "# No manual Prometheus config needed — operator handles discovery." ;; scrape) echo "=== Scraping GoChat metrics from ${METRICS_URL} ===" curl -sf --max-time "${TIMEOUT}" "${METRICS_URL}" | head -50 || { echo "ERROR: Failed to scrape metrics endpoint at ${METRICS_URL}" exit 1 } echo "" echo "--- Full metrics available at: ${METRICS_URL} ---" ;; check) echo "=== Checking GoChat metrics endpoint ===" # Verify endpoint is reachable curl -sf --max-time "${TIMEOUT}" "${METRICS_URL}" > /dev/null 2>&1 && { echo "✅ Metrics endpoint is reachable at ${METRICS_URL}" # Count available metric families metric_count=$(curl -sf --max-time "${TIMEOUT}" "${METRICS_URL}" 2>/dev/null | grep -c "^# TYPE" || echo "0") echo " Available metric families: ${metric_count}" # Check for key GoChat metrics for metric in gochat_http_requests_total gochat_conversations_active gochat_messages_sent_total gochat_websocket_connections gochat_worker_jobs_processed_total; do curl -sf --max-time "${TIMEOUT}" "${METRICS_URL}" 2>/dev/null | grep -q "^${metric}" && { echo " ✅ ${metric} present" } || { echo " ⚠️ ${metric} not found" } done } || { echo "❌ Metrics endpoint unreachable at ${METRICS_URL}" exit 1 } ;; *) echo "Unknown action: ${ACTION}" exit 1 ;; esac