Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
VURU authored and mcosta74 committed Mar 6, 2023
1 parent a9d9d46 commit 8b67d7d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 26 deletions.
50 changes: 50 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,56 @@ func GetServerIDFromVarz(endpoint string, retryInterval time.Duration) string {
return id
}

func GetServerNameFromVarz(endpoint string, retryInterval time.Duration) string {
getServerName := func() (string, error) {
resp, err := http.DefaultClient.Get(endpoint + "/varz")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var response map[string]interface{}
err = json.Unmarshal(body, &response)
if err != nil {
return "", err
}
serverName, ok := response["server_name"]
if !ok {
Fatalf("Could not find server name in /varz")
}
id, ok := serverName.(string)
if !ok {
Fatalf("Invalid server_name type in /varz: %+v", serverName)
}

return id, nil
}

var id string
var err error
id, err = getServerName()
if err == nil {
return id
}

// Retry periodically until available, in case it never starts
// then a liveness check against the NATS Server itself should
// detect that an restart the server, in terms of the exporter
// we just wait for it to eventually be available.
for range time.NewTicker(retryInterval).C {
id, err = getServerName()
if err != nil {
Errorf("Could not find server id: %s", err)
continue
}
break
}
return id
}

// Describe the metric to the Prometheus server.
func (nc *NATSCollector) Describe(ch chan<- *prometheus.Desc) {
nc.Lock()
Expand Down
53 changes: 27 additions & 26 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,32 @@ const (
// NATSExporterOptions are options to configure the NATS collector
type NATSExporterOptions struct {
collector.LoggerOptions
ListenAddress string
ListenPort int
ScrapePath string
GetHealthz bool
GetConnz bool
GetConnzDetailed bool
GetVarz bool
GetSubz bool
GetRoutez bool
GetGatewayz bool
GetLeafz bool
GetReplicatorVarz bool
GetStreamingChannelz bool
GetStreamingServerz bool
GetJszFilter string
RetryInterval time.Duration
CertFile string
KeyFile string
CaFile string
NATSServerURL string
NATSServerTag string
HTTPUser string // User in metrics scrape by prometheus.
HTTPPassword string
Prefix string
UseInternalServerID bool
ListenAddress string
ListenPort int
ScrapePath string
GetHealthz bool
GetConnz bool
GetConnzDetailed bool
GetVarz bool
GetSubz bool
GetRoutez bool
GetGatewayz bool
GetLeafz bool
GetReplicatorVarz bool
GetStreamingChannelz bool
GetStreamingServerz bool
GetJszFilter string
RetryInterval time.Duration
CertFile string
KeyFile string
CaFile string
NATSServerURL string
NATSServerTag string
HTTPUser string // User in metrics scrape by prometheus.
HTTPPassword string
Prefix string
UseInternalServerID bool
UseInternalServerName bool
}

// NATSExporter collects NATS metrics
Expand Down Expand Up @@ -175,7 +176,7 @@ func (ne *NATSExporter) InitializeCollectors() error {
}

getJsz := opts.GetJszFilter != ""
if !opts.GetHealthz && !opts.GetConnz && !opts.GetConnzDetailed && !opts.GetRoutez &&
if !opts.GetConnz && !opts.GetRoutez &&
!opts.GetSubz && !opts.GetVarz && !opts.GetGatewayz && !opts.GetLeafz &&
!opts.GetStreamingChannelz && !opts.GetStreamingServerz && !opts.GetReplicatorVarz && !getJsz {
return fmt.Errorf("no Collectors specfied")
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func main() {
flag.StringVar(&opts.HTTPPassword, "http_pass", "", "Set the password for HTTP scrapes. NATS bcrypt supported.")
flag.StringVar(&opts.Prefix, "prefix", "", "Replace the default prefix for all the metrics.")
flag.BoolVar(&opts.UseInternalServerID, "use_internal_server_id", false, "Enables using ServerID from /varz")
flag.BoolVar(&opts.UseInternalServerName, "use_internal_server_name", false, "Enables using ServerName from /varz")
flag.Parse()

opts.RetryInterval = time.Duration(retryInterval) * time.Second
Expand Down Expand Up @@ -165,6 +166,13 @@ necessary.`)
if err := exp.AddServer(id, url); err != nil {
collector.Fatalf("Unable to setup server in exporter: %s, %s: %v", id, url, err)
}
} else if len(args) == 1 && opts.UseInternalServerID {
// Pick the server id from the /varz endpoint info.
url := flag.Args()[0]
id := collector.GetServerNameFromVarz(url, opts.RetryInterval)
if err := exp.AddServer(id, url); err != nil {
collector.Fatalf("Unable to setup server in exporter: %s, %s: %v", id, url, err)
}
} else {
// For each URL specified, add the NATS server with the optional ID.
for _, arg := range args {
Expand Down

0 comments on commit 8b67d7d

Please sign in to comment.