Skip to content

Commit

Permalink
feat: ability to disable per client metrics
Browse files Browse the repository at this point in the history
As the common_name can have a high cardinality on high traffic
openvpn servers with a lot of clients, this allows to drop that
metric and only expose metrics with a lower cardinality
  • Loading branch information
patrickjahns committed Apr 26, 2020
1 parent 2fc896b commit 5a1ac96
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
56 changes: 31 additions & 25 deletions pkg/collector/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type OpenVPNCollector struct {
logger log.Logger
name string
statusFile string
collectClientMetrics bool
LastUpdated *prometheus.Desc
ConnectedClients *prometheus.Desc
BytesReceived *prometheus.Desc
Expand All @@ -21,11 +22,12 @@ type OpenVPNCollector struct {
}

// NewOpenVPNCollector returns a new OpenVPNCollector
func NewOpenVPNCollector(logger log.Logger, name string, statusFile string) *OpenVPNCollector {
func NewOpenVPNCollector(logger log.Logger, name string, statusFile string, collectClientMetrics bool) *OpenVPNCollector {
return &OpenVPNCollector{
logger: logger,
statusFile: statusFile,
name: name,
logger: logger,
statusFile: statusFile,
name: name,
collectClientMetrics: collectClientMetrics,

LastUpdated: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "last_updated"),
Expand Down Expand Up @@ -70,10 +72,12 @@ func NewOpenVPNCollector(logger log.Logger, name string, statusFile string) *Ope
func (c *OpenVPNCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.LastUpdated
ch <- c.ConnectedClients
ch <- c.BytesSent
ch <- c.BytesReceived
ch <- c.ConnectedSince
ch <- c.MaxBcastMcastQueueLen
if c.collectClientMetrics {
ch <- c.BytesSent
ch <- c.BytesReceived
ch <- c.ConnectedSince
}
}

// Collect is called by the Prometheus registry when collecting metrics.
Expand All @@ -100,24 +104,26 @@ func (c *OpenVPNCollector) Collect(ch chan<- prometheus.Metric) {
"bytesReceived", client.BytesReceived,
"bytesSent", client.BytesSent,
)
ch <- prometheus.MustNewConstMetric(
c.BytesReceived,
prometheus.GaugeValue,
client.BytesReceived,
c.name, client.CommonName,
)
ch <- prometheus.MustNewConstMetric(
c.BytesSent,
prometheus.GaugeValue,
client.BytesSent,
c.name, client.CommonName,
)
ch <- prometheus.MustNewConstMetric(
c.ConnectedSince,
prometheus.GaugeValue,
float64(client.ConnectedSince.Unix()),
c.name, client.CommonName,
)
if c.collectClientMetrics {
ch <- prometheus.MustNewConstMetric(
c.BytesReceived,
prometheus.GaugeValue,
client.BytesReceived,
c.name, client.CommonName,
)
ch <- prometheus.MustNewConstMetric(
c.BytesSent,
prometheus.GaugeValue,
client.BytesSent,
c.name, client.CommonName,
)
ch <- prometheus.MustNewConstMetric(
c.ConnectedSince,
prometheus.GaugeValue,
float64(client.ConnectedSince.Unix()),
c.name, client.CommonName,
)
}
}
level.Debug(c.logger).Log(
"updatedAt", status.UpdatedAt,
Expand Down
10 changes: 8 additions & 2 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ func Run() error {
Usage: "The OpenVPN status file(s) to export (example test:./example/version1.status )",
Required: true,
},
&cli.BoolFlag{
Name: "disable-client-metrics",
Usage: "Disables per client (bytes_received, bytes_sent, connected_since) metrics",
},
}

app.Before = func(c *cli.Context) error {
cfg.StatusFile = c.StringSlice("status-file")
cfg.StatusCollector.StatusFile = c.StringSlice("status-file")
cfg.StatusCollector.ExportClientMetrics = !c.Bool("disable-client-metrics")
return nil
}

Expand Down Expand Up @@ -105,7 +110,7 @@ func run(c *cli.Context, cfg *config.Config) error {
version.GoVersion,
version.Started,
))
for _, statusFile := range cfg.StatusFile {
for _, statusFile := range cfg.StatusCollector.StatusFile {
serverName, statusFile := parseStatusFileSlice(statusFile)

level.Info(logger).Log(
Expand All @@ -117,6 +122,7 @@ func run(c *cli.Context, cfg *config.Config) error {
logger,
serverName,
statusFile,
cfg.StatusCollector.ExportClientMetrics,
))
}

Expand Down
12 changes: 9 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ type Logs struct {

// Config defines the general configuration object
type Config struct {
Server Server
Logs Logs
StatusFile []string
Server Server
Logs Logs
StatusCollector StatusCollector
}

// StatusCollector contains configuration for the OpenVPN status collector
type StatusCollector struct {
ExportClientMetrics bool
StatusFile []string
}

// Load initializes a default configuration struct.
Expand Down

0 comments on commit 5a1ac96

Please sign in to comment.