Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefix to nats metrics with -prefix #64

Merged
merged 4 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Usage of ./prometheus-nats-exporter:
URL path from which to serve scrapes. (default "/metrics")
-port int
Port to listen on. (default 7777)
-prefix
Set the prefix for all the metrics.
-r string
Remote syslog address to write log statements.
-remote_syslog string
Expand Down
17 changes: 9 additions & 8 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

const (
namespace = "gnatsd"
)
var namespace = "gnatsd"

// CollectedServer is a NATS server polled by this collector
type CollectedServer struct {
Expand All @@ -46,10 +44,13 @@ type NATSCollector struct {
// Based on our current integration, we're going to treat all metrics as gauges.
// We are going to call the set message on the gauge when we receive an updated
// metrics pull.
func newPrometheusGaugeVec(subsystem string, name string, help string) (metric *prometheus.GaugeVec) {
func newPrometheusGaugeVec(subsystem string, name string, help string, prefix string) (metric *prometheus.GaugeVec) {
if help == "" {
help = name
}
if prefix != "" {
namespace = prefix
}
opts := prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Expand Down Expand Up @@ -162,7 +163,7 @@ func (nc *NATSCollector) Collect(ch chan<- prometheus.Metric) {
// For each NATS Metrics endpoint (/*z) get the first URL
// to determine the list of possible metrics.
// TODO: flatten embedded maps.
func (nc *NATSCollector) initMetricsFromServers() {
func (nc *NATSCollector) initMetricsFromServers(namespace string) {
var response map[string]interface{}

nc.Stats = make(map[string]interface{})
Expand Down Expand Up @@ -191,7 +192,7 @@ func (nc *NATSCollector) initMetricsFromServers() {
i := response[k]
switch v := i.(type) {
case float64: // all json numbers are handled here.
nc.Stats[k] = newPrometheusGaugeVec(nc.endpoint, k, "")
nc.Stats[k] = newPrometheusGaugeVec(nc.endpoint, k, "", namespace)
case string:
// do nothing
default:
Expand All @@ -204,7 +205,7 @@ func (nc *NATSCollector) initMetricsFromServers() {

// NewCollector creates a new NATS Collector from a list of monitoring URLs.
// Each URL should be to a specific endpoint (e.g. varz, connz, subsz, or routez)
func NewCollector(endpoint string, servers []*CollectedServer) prometheus.Collector {
func NewCollector(endpoint string, servers []*CollectedServer, namespace string) prometheus.Collector {
if isStreamingEndpoint(endpoint) {
return newStreamingCollector(endpoint, servers)
}
Expand All @@ -230,7 +231,7 @@ func NewCollector(endpoint string, servers []*CollectedServer) prometheus.Collec
}
}

nc.initMetricsFromServers()
nc.initMetricsFromServers(namespace)

return nc
}
12 changes: 6 additions & 6 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func verifyCollector(url string, endpoint string, cases map[string]float64, t *t
ID: "id",
URL: url,
}
coll := NewCollector(endpoint, servers)
coll := NewCollector(endpoint, servers, "test")

// now collect the metrics
c := make(chan prometheus.Metric)
Expand Down Expand Up @@ -116,7 +116,7 @@ func getLabelValues(url string, endpoint string, metricNames []string) (map[stri
ID: "id",
URL: url,
}
coll := NewCollector(endpoint, servers)
coll := NewCollector(endpoint, servers, "test")
coll.Collect(metrics)
close(metrics)

Expand Down Expand Up @@ -202,10 +202,10 @@ func TestRegister(t *testing.T) {
// check duplicates do not panic
servers = append(servers, cs)

NewCollector("varz", servers)
NewCollector("varz", servers, "test")

// test idenpotency.
nc := NewCollector("varz", servers)
nc := NewCollector("varz", servers, "test")

// test without a server (no error).
if err := prometheus.Register(nc); err == nil {
Expand All @@ -218,14 +218,14 @@ func TestRegister(t *testing.T) {
defer s.Shutdown()

// test collect with a server
nc = NewCollector("varz", servers)
nc = NewCollector("varz", servers, "test")
if err := prometheus.Register(nc); err != nil {
t.Fatalf("Got unexpected error: %v", err)
}
prometheus.Unregister(nc)

// test collect with an invalid endpoint
nc = NewCollector("GARBAGE", servers)
nc = NewCollector("GARBAGE", servers, "test")
if err := prometheus.Register(nc); err == nil {
t.Fatalf("Did not get expected error.")
defer prometheus.Unregister(nc)
Expand Down
3 changes: 2 additions & 1 deletion exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type NATSExporterOptions struct {
NATSServerTag string
HTTPUser string // User in metrics scrape by prometheus.
HTTPPassword string
Prefix string
}

//NATSExporter collects NATS metrics
Expand Down Expand Up @@ -108,7 +109,7 @@ func NewExporter(opts *NATSExporterOptions) *NATSExporter {
}

func (ne *NATSExporter) createCollector(endpoint string) {
ne.registerCollector(endpoint, collector.NewCollector(endpoint, ne.servers))
ne.registerCollector(endpoint, collector.NewCollector(endpoint, ne.servers, ne.opts.Prefix))
}

func (ne *NATSExporter) registerCollector(endpoint string, nc prometheus.Collector) {
Expand Down
1 change: 1 addition & 0 deletions prometheus_nats_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func main() {
flag.StringVar(&opts.CaFile, "tlscacert", "", "Client certificate CA for verification (used with HTTPS).")
flag.StringVar(&opts.HTTPUser, "http_user", "", "Enable basic auth and set user name for HTTP scrapes.")
flag.StringVar(&opts.HTTPPassword, "http_pass", "", "Set the password for HTTP scrapes. NATS bcrypt supported.")
flag.StringVar(&opts.Prefix, "prefix", "", "Set the prefix for all the metrics.")
flag.Parse()

opts.RetryInterval = time.Duration(retryInterval) * time.Second
Expand Down