项目作者: nats-io

项目描述 :
A Prometheus exporter for NATS metrics
高级语言: Go
项目地址: git://github.com/nats-io/prometheus-nats-exporter.git
创建时间: 2016-05-24T03:49:23Z
项目社区:https://github.com/nats-io/prometheus-nats-exporter

开源协议:Apache License 2.0

下载


License Build Coverage

The Prometheus NATS Exporter

The Prometheus NATS Exporter consists of both a package and an application that
exports NATS server metrics
to Prometheus for monitoring.
The exporter aggregates metrics from the NATS server server monitoring endpoints you choose (varz, connz, subz,
routez, healthz…) into a single Prometheus exporter endpoint.

For an alternative approach that uses a System account instead of HTTP monitoring endpoints, see also: NATS Surveyor

Build

  1. make build

If you want to run tests, you can do this.

  1. make test
  2. make lint
  3. # If you want to see the coverage locally, then run this.
  4. # make test-cover

Run

Start the prometheus-nats-exporter executable, and poll the varz metrics
endpoints of the NATS server located on localhost configured with a monitor
port of 5555.

  1. prometheus-nats-exporter -varz "http://localhost:5555"

To run with docker, you can use the following image:

  1. docker run natsio/prometheus-nats-exporter:latest

Usage

  1. prometheus-nats-exporter <flags> url
  2. # ./prometheus-nats-exporter
  3. Usage: ./prometheus-nats-exporter <flags> url
  4. -D Enable debug log level.
  5. -DV
  6. Enable debug and trace log levels.
  7. -V Enable trace log level.
  8. -a string
  9. Network host to listen on. (default "0.0.0.0")
  10. -accstatz
  11. Get accstatz metrics.
  12. -addr string
  13. Network host to listen on. (default "0.0.0.0")
  14. -connz
  15. Get connection metrics.
  16. -connz_detailed connz
  17. Get detailed connection metrics for each client. Enables flag connz implicitly.
  18. -gatewayz
  19. Get gateway metrics.
  20. -healthz
  21. Get health metrics.
  22. -healthz_js_enabled_only
  23. Get health metrics with js-enabled-only=true.
  24. -healthz_js_server_only
  25. Get health metrics with js-server-only=true.
  26. -http_pass string
  27. Set the password for HTTP scrapes. NATS bcrypt supported.
  28. -http_user string
  29. Enable basic auth and set user name for HTTP scrapes.
  30. -jsz string
  31. Select JetStream metrics to filter (e.g streams, accounts, consumers)
  32. -l string
  33. Log file name.
  34. -leafz
  35. Get leaf metrics.
  36. -log string
  37. Log file name.
  38. -p int
  39. Port to listen on. (default 7777)
  40. -path string
  41. URL path from which to serve scrapes. (default "/metrics")
  42. -port int
  43. Port to listen on. (default 7777)
  44. -prefix string
  45. Replace the default prefix for all the metrics.
  46. -r string
  47. Remote syslog address to write log statements.
  48. -remote_syslog string
  49. Write log statements to a remote syslog.
  50. -ri int
  51. Interval in seconds to retry NATS Server monitor URL. (default 30)
  52. -routez
  53. Get route metrics.
  54. -s Write log statements to the syslog.
  55. -subz
  56. Get subscription metrics.
  57. -syslog
  58. Write log statements to the syslog.
  59. -tlscacert string
  60. Client certificate CA for verification (used with HTTPS).
  61. -tlscert string
  62. Server certificate file (Enables HTTPS).
  63. -tlskey string
  64. Private key for server certificate (used with HTTPS).
  65. -use_internal_server_id
  66. Enables using ServerID from /varz
  67. -use_internal_server_name
  68. Enables using ServerName from /varz
  69. -varz
  70. Get general metrics.
  71. -version
  72. Show exporter version and exit.

The URL parameter

The url parameter is a standard url. Both http and https (when TLS is
configured) is supported.

e.g.
http://denver1.foobar.com:8222

Monitoring

The NATS Prometheus exporter exposes metrics through an HTTP interface, and will
default to:
http://0.0.0.0:7777/metrics.

When --http_user and --http_pass is used, you will need to set the username
password in prometheus. See basic_auth in the prometheus configuration
documentation. If using a bcrypted password use a very low cost as scrapes
occur frequently.

It will return output that is readable by Prometheus.

The returned data looks like this:

  1. # HELP gnatsd_varz_in_bytes in_bytes
  2. # TYPE gnatsd_varz_in_bytes gauge
  3. gnatsd_varz_in_bytes{server_id="http://localhost:8222"} 0
  4. # HELP gnatsd_varz_in_msgs in_msgs
  5. # TYPE gnatsd_varz_in_msgs gauge
  6. gnatsd_varz_in_msgs{server_id="http://localhost:8222"} 0
  7. # HELP gnatsd_varz_max_connections max_connections
  8. # TYPE gnatsd_varz_max_connections gauge
  9. gnatsd_varz_max_connections{server_id="http://localhost:8222"} 65536

The NATS Prometheus Exporter API

The NATS prometheus exporter also provides a simple and easy to use API that
allows it to run embedded in your code.

Import the exporter package

  1. // import the API like this
  2. import (
  3. "github.com/nats-io/prometheus-nats-exporter/exporter"
  4. )

API Usage

In just a few lines of code, configure and launch an instance of the exporter.

  1. // Get the default options, and set what you need to. The listen address and port
  2. // is how prometheus can poll for collected data.
  3. opts := exporter.GetDefaultExporterOptions()
  4. opts.ListenAddress = "localhost"
  5. opts.ListenPort = 8888
  6. opts.GetVarz = true
  7. opts.NATSServerURL = "http://localhost:8222"
  8. // create an exporter instance, ready to be launched.
  9. exp := exporter.NewExporter(opts)
  10. // start collecting data
  11. exp.Start()
  12. // when done, simply call Stop()
  13. exp.Stop()
  14. // For convenience, you can block until the exporter is stopped
  15. exp.WaitUntilDone()

Monitoring Walkthrough

For additional information, refer to the walkthrough of
monitoring NATS with Prometheus and Grafana.