Files
gochat/backend/scripts/prometheus_exporter.sh
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

101 lines
4.0 KiB
Bash

#!/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