NATS Surveyor

repository·main·Indexed 43 days ago

https://github.com/nats-io/nats-surveyor

A Prometheus exporter for NATS that polls servers for Statz messages to provide a comprehensive view of NATS deployments without requiring sidecars. It supports authentication via system accounts, configuration through CLI flags, environment variables, or files, and includes specialized support for JetStream (JSZ) metrics to manage high cardinality.

Tokens
6.5K
Snippets
14
Records
21
Agent score
52%

What's inside NATS Surveyor

  1. What is NATS Surveyor?

    main

    NATS Surveyor is a Prometheus exporter that polls NATS servers for Statz messages to generate monitoring data. It allows a single exporter to connect to any NATS server and provide a complete picture of a NATS deployment without requiring sidecars or additional monitoring components.

    Requirement: System accounts must be enabled in your NATS configuration to use Surveyor.

  2. Manage High Cardinality with JSZ Metrics

    main

    Since v0.9.1, Surveyor supports collecting JetStream (JSZ) stream and consumer metrics. Because collecting all metrics from all replicas can cause high cardinality issues in Prometheus, use the following options to limit data:

    • --jsz=streams: Collects only stream metrics (skips consumer metrics).
    • --jsz-leaders-only: Fetches metrics from stream and consumer leaders only, skipping replica data.
    • --jsz-filter <comma-separated-list>: Limits exported consumer metrics to a specific subset.

    Available consumer metrics for filtering: consumer_delivered_consumer_seq, consumer_delivered_stream_seq, consumer_ack_floor_consumer_seq, consumer_ack_floor_stream_seq, consumer_num_ack_pending, consumer_num_pending, consumer_num_redelivered, consumer_num_waiting.

    # Example: Collect only leader metrics and specific consumer status metrics
    nats-surveyor --jsz=all \
                  --jsz-leaders-only \
                  --jsz-filter=consumer_num_pending,consumer_num_ack_pending,consumer_num_waiting
  3. Observe NATS Services using JSON files

    main

    You can observe specific NATS services by creating .json files in the observations directory. The files are watched and updated automatically using fsnotify.

    Each file must provide exactly one authentication method (JWT, Credential file, NKey, Token, or Username/Password) and can optionally include TLS configuration.

    {
      "name":       "my service",
      "topic":      "email.subscribe.>",
      "jwt":        "jwt portion of creds, must include seed also",
      "seed":       "seed portion of creds, must include jwt also",
      "credential": "/path/to/file.creds",
      "nkey":       "nkey seed",
      "token":       "token",
      "username":    "username, must include password also",
      "password":    "password, must include user also",
      "tls_ca":      "/path/to/ca.pem, defaults to surveyor's ca if one exists",
      "tls_cert":    "/path/to/cert.pem, defaults to surveyor's cert if one exists",
      "tls_key":     "/path/to/key.pem, defaults to surveyor's key if one exists"
    }
  4. Understand NATS Surveyor Metrics Format

    main

    Surveyor outputs metrics in the format nats_core_NNNN_metric, where NNNN represents the component type: server, route, or gateway.

    To facilitate filtering in Prometheus, metrics include the following labels:

    • server_cluster
    • server_name
    • server_id
    • server_route_name (for routes)
    • server_gateway_name (for gateways)

    Connectivity Monitoring: The nats_up metric is used to monitor the health of the connection. It returns 1 when connected to NATS. If it returns 0 and no other NATS metrics are present, it indicates a connectivity issue between the exporter and the NATS system, helping you distinguish between an exporter failure and a NATS connectivity problem.

  5. Monitor JetStream on a per-account basis

    main

    To monitor JetStream, create .json files in the jetstream directory. Files are watched via fsnotify.

    Requirement: Ensure the provided user has access to the $JS.EVENT.> subject.

    Only one authentication method is required per file.

    {
      "name":       "my account",
      "jwt":        "jwt portion of creds, must include seed also",
      "seed":       "seed portion of creds, must include jwt also",
      "credential": "/path/to/file.creds",
      "nkey":       "nkey seed",
      "token":       "token",
      "username":    "username, must include password also",
      "password":    "password, must include user also",
      "tls_ca":      "/path/to/ca.pem, defaults to surveyor's ca if one exists",
      "tls_cert":    "/path/to/cert.pem, defaults to surveyor's cert if one exists",
      "tls_key":     "/path/to/key.pem, defaults to surveyor's key if one exists"
    }
  6. Configure NATS Surveyor via Config Files

    main

    Surveyor uses Viper to read configuration files, supporting JSON, TOML, YAML, HCL, envfile, and Java properties.

    To use a specific file, pass the --config flag. If not specified, Surveyor looks for files at /etc/nats-surveyor/nats-surveyor[.ext] or ./nats-surveyor[.ext].

    The configuration format mirrors the CLI flags. For example, a nats-surveyor.yaml file might look like this:

    servers: nats://127.0.0.1:4222
    accounts: true
    log-level: debug
  7. Manage the NATS Surveyor Docker lifecycle

    main

    Use the following commands to manage your deployment:

    • Start (pulling latest images): docker compose up --pull always (run from the docker-compose directory).
    • Stop (keep containers): docker compose stop.
    • Restart (after stopping): docker compose up.
    • Cleanup (remove containers): docker compose down.
    # Start
    docker compose up --pull always
    
    # Stop but keep containers
    docker compose stop
    
    # Restart
    docker compose up
    
    # Remove everything
    docker compose down
  8. Deploy the NATS Surveyor stack using Docker Compose

    main

    The NATS Surveyor stack (comprising Grafana, Prometheus, and NATS Surveyor) can be deployed using Docker Compose. You can authenticate using either a NATS credentials file or a username/password.

    Using a credentials file: Set NATS_SURVEYOR_CREDS to the path of your SYS.creds file and NATS_SURVEYOR_SERVERS to your NATS server URL.

    Using username/password: Set NATS_SURVEYOR_USER and NATS_SURVEYOR_PASSWORD along with NATS_SURVEYOR_SERVERS.

    Using the survey.sh helper script: Run the script from the docker-compose directory with the following arguments: <url> <server count> <system credentials>.

    # Using credentials file
    NATS_SURVEYOR_CREDS=/path/to/SYS.creds NATS_SURVEYOR_SERVERS=nats://host.docker.internal:4222 docker compose up --pull always
    
    # Using username/password
    NATS_SURVEYOR_USER=system NATS_SURVEYOR_PASSWORD=s3cret NATS_SURVEYOR_SERVERS=nats://host.docker.internal:4222 docker compose up --pull always
    
    # Using the survey.sh helper script
    ./survey.sh nats://host.docker.internal:4222 1 ./SYS.creds
  9. Access the NATS Surveyor Dashboard

    main

    Once the stack is running, access the Grafana dashboard via your browser:

    1. Navigate to http://127.0.0.1:3000 (or the hostname of the remote host).
    2. Log in with default credentials:
      • User: admin
      • Password: admin
    3. Navigate to "Manage dashboards" and select the NATS Surveyor dashboard to monitor your deployment.
  10. Configure NATS Surveyor via Environment Variables

    main

    Surveyor supports environment variables prefixed with NATS_SURVEYOR_. To map a flag to an environment variable, convert the flag name to uppercase and replace dashes with underscores.

    Examples:

    • --servers $\rightarrow$ NATS_SURVEYOR_SERVERS
    • --accounts $\rightarrow$ NATS_SURVEYOR_ACCOUNTS
    • --log-level $\rightarrow$ NATS_SURVEYOR_LOG_LEVEL
    • --user $\rightarrow$ NATS_SURVEYOR_USER
    • --password $\rightarrow$ NATS_SURVEYOR_PASSWORD
    NATS_SURVEYOR_SERVERS=nats://127.0.0.1:4222
    NATS_SURVEYOR_ACCOUNTS=true
    NATS_SURVEYOR_LOG_LEVEL=debug
    NATS_SURVEYOR_USER=myuser
    NATS_SURVEYOR_PASSWORD=mypassword
  11. Authenticate with System Accounts

    main

    To access the necessary Statz messages, Surveyor must authenticate using a NATS system account. You can provide credentials using one of the following four methods:

    1. Chained Credentials File: Use the --creds flag to provide a file containing both the JWT and the NKey seed.
    2. Direct JWT and Seed: Use the --jwt and --seed flags to provide the user JWT and NKey seed directly.
    3. Token File: Use the --token-file flag to point to a file containing a bearer token, combined with the --user flag.
    4. Username and Password: Use the --user and --password flags for standard authentication.
    # Method 1: Credentials file
    ./nats-surveyor --creds ./test/SYS.creds
    
    # Method 2: Direct JWT and Seed
    ./nats-surveyor --jwt $NATS_USER_JWT --seed $NATS_NKEY_SEED
    
    # Method 3: Token file
    ./nats-surveyor --user sys --token-file /var/run/secrets/tokens/<token>
  12. Configure nats-surveyor via environment variables

    main

    All CLI flags can be set using environment variables. The prefix for these variables is NATS_SURVEYOR_. Hyphens in flag names are replaced with underscores.

    For example, the --servers flag can be set via NATS_SURVEYOR_SERVERS.