github.com/MetalBlockchain/metalgo@v1.11.9/scripts/run_prometheus.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -euo pipefail
     4  
     5  # Starts a prometheus instance in agent-mode, forwarding to a central
     6  # instance. Intended to enable metrics collection from temporary networks running
     7  # locally and in CI.
     8  #
     9  # The prometheus instance will remain running in the background and will forward
    10  # metrics to the central instance for all tmpnet networks.
    11  #
    12  # To stop it:
    13  #
    14  #   $ kill -9 `cat ~/.tmpnet/prometheus/run.pid` && rm ~/.tmpnet/prometheus/run.pid
    15  #
    16  
    17  # e.g.,
    18  # PROMETHEUS_ID=<id> PROMETHEUS_PASSWORD=<password> ./scripts/run_prometheus.sh
    19  if ! [[ "$0" =~ scripts/run_prometheus.sh ]]; then
    20    echo "must be run from repository root"
    21    exit 255
    22  fi
    23  
    24  PROMETHEUS_WORKING_DIR="${HOME}/.tmpnet/prometheus"
    25  PIDFILE="${PROMETHEUS_WORKING_DIR}"/run.pid
    26  
    27  # First check if an agent-mode prometheus is already running. A single instance can collect
    28  # metrics from all local temporary networks.
    29  if pgrep --pidfile="${PIDFILE}" -f 'prometheus.*enable-feature=agent' &> /dev/null; then
    30    echo "prometheus is already running locally with --enable-feature=agent"
    31    exit 0
    32  fi
    33  
    34  PROMETHEUS_URL="${PROMETHEUS_URL:-https://prometheus-experimental.avax-dev.network}"
    35  if [[ -z "${PROMETHEUS_URL}" ]]; then
    36    echo "Please provide a value for PROMETHEUS_URL"
    37    exit 1
    38  fi
    39  
    40  PROMETHEUS_ID="${PROMETHEUS_ID:-}"
    41  if [[ -z "${PROMETHEUS_ID}" ]]; then
    42    echo "Please provide a value for PROMETHEUS_ID"
    43    exit 1
    44  fi
    45  
    46  PROMETHEUS_PASSWORD="${PROMETHEUS_PASSWORD:-}"
    47  if [[ -z "${PROMETHEUS_PASSWORD}" ]]; then
    48    echo "Plase provide a value for PROMETHEUS_PASSWORD"
    49    exit 1
    50  fi
    51  
    52  # This was the LTS version when this script was written. Probably not
    53  # much reason to update it unless something breaks since the usage
    54  # here is only to collect metrics from temporary networks.
    55  VERSION="2.45.3"
    56  
    57  # Ensure the prometheus command is locally available
    58  CMD=prometheus
    59  if ! command -v "${CMD}" &> /dev/null; then
    60    # Try to use a local version
    61    CMD="${PWD}/bin/prometheus"
    62    if ! command -v "${CMD}" &> /dev/null; then
    63      echo "prometheus not found, attempting to install..."
    64  
    65      # Determine the arch
    66      if which sw_vers &> /dev/null; then
    67        echo "on macos, only amd64 binaries are available so rosetta is required on apple silicon machines."
    68        echo "to avoid using rosetta, install via homebrew: brew install prometheus"
    69        DIST=darwin
    70      else
    71        ARCH="$(uname -i)"
    72        if [[ "${ARCH}" != "x86_64" ]]; then
    73          echo "on linux, only amd64 binaries are available. manual installation of prometheus is required."
    74          exit 1
    75        else
    76          DIST="linux"
    77        fi
    78      fi
    79  
    80      # Install the specified release
    81      PROMETHEUS_FILE="prometheus-${VERSION}.${DIST}-amd64"
    82      URL="https://github.com/prometheus/prometheus/releases/download/v${VERSION}/${PROMETHEUS_FILE}.tar.gz"
    83      curl -s -L "${URL}" | tar zxv -C /tmp > /dev/null
    84      mkdir -p "$(dirname "${CMD}")"
    85      cp /tmp/"${PROMETHEUS_FILE}/prometheus" "${CMD}"
    86    fi
    87  fi
    88  
    89  # Configure prometheus
    90  FILE_SD_PATH="${PROMETHEUS_WORKING_DIR}/file_sd_configs"
    91  mkdir -p "${FILE_SD_PATH}"
    92  
    93  echo "writing configuration..."
    94  cat >"${PROMETHEUS_WORKING_DIR}"/prometheus.yaml <<EOL
    95  # my global config
    96  global:
    97    # Make sure this value takes into account the network-shutdown-delay in tests/fixture/e2e/env.go
    98    scrape_interval: 10s # Default is every 1 minute.
    99    evaluation_interval: 10s # The default is every 1 minute.
   100    scrape_timeout: 5s # The default is every 10s
   101  
   102  scrape_configs:
   103    - job_name: "avalanchego"
   104      metrics_path: "/ext/metrics"
   105      file_sd_configs:
   106        - files:
   107            - '${FILE_SD_PATH}/*.json'
   108  
   109  remote_write:
   110    - url: "${PROMETHEUS_URL}/api/v1/write"
   111      basic_auth:
   112        username: "${PROMETHEUS_ID}"
   113        password: "${PROMETHEUS_PASSWORD}"
   114  EOL
   115  
   116  echo "starting prometheus..."
   117  cd "${PROMETHEUS_WORKING_DIR}"
   118  nohup "${CMD}" --config.file=prometheus.yaml --web.listen-address=localhost:0 --enable-feature=agent > prometheus.log 2>&1 &
   119  echo $! > "${PIDFILE}"
   120  echo "running with pid $(cat "${PIDFILE}")"