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

Expose exporters collectors #175

Merged
merged 1 commit into from
Jun 2, 2022
Merged
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
27 changes: 14 additions & 13 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type NATSExporter struct {
opts *NATSExporterOptions
doneWg sync.WaitGroup
http net.Listener
collectors []prometheus.Collector
Collectors []prometheus.Collector
servers []*collector.CollectedServer
mode uint8
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (ne *NATSExporter) registerCollector(system, endpoint string, nc prometheus
}
} else {
collector.Debugf("Registered collector for system %s, endpoint: %s", system, endpoint)
ne.collectors = append(ne.collectors, nc)
ne.Collectors = append(ne.Collectors, nc)
}
}

Expand All @@ -163,9 +163,9 @@ func (ne *NATSExporter) AddServer(id, url string) error {
return nil
}

// initializeCollectors initializes the collectors for the exporter.
// InitializeCollectors initializes the Collectors for the exporter.
// Caller must lock
func (ne *NATSExporter) initializeCollectors() error {
func (ne *NATSExporter) InitializeCollectors() error {
opts := ne.opts

if len(ne.servers) == 0 {
Expand All @@ -176,7 +176,7 @@ func (ne *NATSExporter) initializeCollectors() error {
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")
return fmt.Errorf("no Collectors specfied")
}
if opts.GetReplicatorVarz && opts.GetVarz {
return fmt.Errorf("replicatorVarz cannot be used with varz")
Expand Down Expand Up @@ -220,13 +220,14 @@ func (ne *NATSExporter) initializeCollectors() error {
return nil
}

// ClearCollectors unregisters the collectors
// caller must lock
func (ne *NATSExporter) clearCollectors() {
if ne.collectors != nil {
for _, c := range ne.collectors {
func (ne *NATSExporter) ClearCollectors() {
if ne.Collectors != nil {
for _, c := range ne.Collectors {
prometheus.Unregister(c)
}
ne.collectors = nil
ne.Collectors = nil
}
}

Expand All @@ -238,13 +239,13 @@ func (ne *NATSExporter) Start() error {
return nil
}

if err := ne.initializeCollectors(); err != nil {
ne.clearCollectors()
if err := ne.InitializeCollectors(); err != nil {
ne.ClearCollectors()
return err
}

if err := ne.startHTTP(); err != nil {
ne.clearCollectors()
ne.ClearCollectors()
return fmt.Errorf("error serving http: %v", err)
}

Expand Down Expand Up @@ -444,7 +445,7 @@ func (ne *NATSExporter) Stop() {
if err := ne.http.Close(); err != nil {
collector.Debugf("Did not close HTTP: %v", err)
}
ne.clearCollectors()
ne.ClearCollectors()
ne.doneWg.Done()
ne.mode = modeStopped
}