Skip to content

Commit

Permalink
Add flags to get health metrics with different optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienVdG authored and Jarema committed Dec 6, 2024
1 parent 72c7679 commit c2ee798
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ prometheus-nats-exporter <flags> url
Get detailed connection metrics for each client. Enables flag "-connz" implicitly.
-healthz
Get health metrics.
-healthz_js_enabled_only
Get health metrics with js-enabled-only=true flag.
-healthz_js_server_only
Get health metrics with js-server-only=true flag.
-gatewayz
Get gateway metrics.
-accstatz
Expand Down
16 changes: 13 additions & 3 deletions collector/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import (
)

const (
healthzEndpoint = "healthz"
healthzEndpoint = "healthz"
healthzJsEnabledOnlyEndpoint = "healthz_js_enabled_only"
healthzJsServerOnlyEndpoint = "healthz_js_server_only"
)

func isHealthzEndpoint(system, endpoint string) bool {
return system == CoreSystem && endpoint == healthzEndpoint
return system == CoreSystem && (endpoint == healthzEndpoint || endpoint == healthzJsEnabledOnlyEndpoint || endpoint == healthzJsServerOnlyEndpoint)
}

type healthzCollector struct {
Expand All @@ -49,11 +51,19 @@ func newHealthzCollector(system, endpoint string, servers []*CollectedServer) pr
),
}

healthzURLPathAndQuerryArgs := healthzEndpoint
switch endpoint {
case healthzJsEnabledOnlyEndpoint:
healthzURLPathAndQuerryArgs = healthzEndpoint + "?js-enabled-only=true"
case healthzJsServerOnlyEndpoint:
healthzURLPathAndQuerryArgs = healthzEndpoint + "?js-server-only=true"
}

nc.servers = make([]*CollectedServer, len(servers))
for i, s := range servers {
nc.servers[i] = &CollectedServer{
ID: s.ID,
URL: s.URL + "/" + healthzEndpoint,
URL: s.URL + "/" + healthzURLPathAndQuerryArgs,
}
}

Expand Down
62 changes: 35 additions & 27 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,35 @@ 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
GetAccstatz 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
UseServerName bool
ListenAddress string
ListenPort int
ScrapePath string
GetHealthz bool
GetHealthzJsEnabledOnly bool
GetHealthzJsServerOnly bool
GetConnz bool
GetConnzDetailed bool
GetVarz bool
GetSubz bool
GetRoutez bool
GetGatewayz bool
GetAccstatz 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
UseServerName bool
}

// NATSExporter collects NATS metrics
Expand Down Expand Up @@ -193,6 +195,12 @@ func (ne *NATSExporter) InitializeCollectors() error {
if opts.GetHealthz {
ne.createCollector(collector.CoreSystem, "healthz")
}
if opts.GetHealthzJsEnabledOnly {
ne.createCollector(collector.CoreSystem, "healthz_js_enabled_only")
}
if opts.GetHealthzJsServerOnly {
ne.createCollector(collector.CoreSystem, "healthz_js_server_only")
}
if opts.GetConnzDetailed {
ne.createCollector(collector.CoreSystem, "connz_detailed")
} else if opts.GetConnz {
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func updateOptions(debugAndTrace, useSysLog bool, opts *exporter.NATSExporterOpt
}

metricsSpecified := opts.GetConnz || opts.GetVarz || opts.GetSubz || opts.GetHealthz ||
opts.GetHealthzJsEnabledOnly || opts.GetHealthzJsServerOnly ||
opts.GetRoutez || opts.GetGatewayz || opts.GetAccstatz || opts.GetLeafz || opts.GetStreamingChannelz ||
opts.GetStreamingServerz || opts.GetReplicatorVarz || opts.GetJszFilter == ""
if !metricsSpecified {
Expand Down Expand Up @@ -120,6 +121,8 @@ func main() {
flag.BoolVar(&opts.GetConnzDetailed, "connz_detailed", false,
"Get detailed connection metrics for each client. Enables flag `connz` implicitly.")
flag.BoolVar(&opts.GetHealthz, "healthz", false, "Get health metrics.")
flag.BoolVar(&opts.GetHealthzJsEnabledOnly, "healthz_js_enabled_only", false, "Get health metrics with js-enabled-only=true.")
flag.BoolVar(&opts.GetHealthzJsServerOnly, "healthz_js_server_only", false, "Get health metrics with js-server-only=true.")
flag.BoolVar(&opts.GetReplicatorVarz, "replicatorVarz", false, "Get replicator general metrics.")
flag.BoolVar(&opts.GetGatewayz, "gatewayz", false, "Get gateway metrics.")
flag.BoolVar(&opts.GetAccstatz, "accstatz", false, "Get accstatz metrics.")
Expand Down

0 comments on commit c2ee798

Please sign in to comment.