Skip to content

Commit 5a1ac96

Browse files
committed
feat: ability to disable per client metrics
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
1 parent 2fc896b commit 5a1ac96

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

pkg/collector/openvpn.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type OpenVPNCollector struct {
1212
logger log.Logger
1313
name string
1414
statusFile string
15+
collectClientMetrics bool
1516
LastUpdated *prometheus.Desc
1617
ConnectedClients *prometheus.Desc
1718
BytesReceived *prometheus.Desc
@@ -21,11 +22,12 @@ type OpenVPNCollector struct {
2122
}
2223

2324
// NewOpenVPNCollector returns a new OpenVPNCollector
24-
func NewOpenVPNCollector(logger log.Logger, name string, statusFile string) *OpenVPNCollector {
25+
func NewOpenVPNCollector(logger log.Logger, name string, statusFile string, collectClientMetrics bool) *OpenVPNCollector {
2526
return &OpenVPNCollector{
26-
logger: logger,
27-
statusFile: statusFile,
28-
name: name,
27+
logger: logger,
28+
statusFile: statusFile,
29+
name: name,
30+
collectClientMetrics: collectClientMetrics,
2931

3032
LastUpdated: prometheus.NewDesc(
3133
prometheus.BuildFQName(namespace, "", "last_updated"),
@@ -70,10 +72,12 @@ func NewOpenVPNCollector(logger log.Logger, name string, statusFile string) *Ope
7072
func (c *OpenVPNCollector) Describe(ch chan<- *prometheus.Desc) {
7173
ch <- c.LastUpdated
7274
ch <- c.ConnectedClients
73-
ch <- c.BytesSent
74-
ch <- c.BytesReceived
75-
ch <- c.ConnectedSince
7675
ch <- c.MaxBcastMcastQueueLen
76+
if c.collectClientMetrics {
77+
ch <- c.BytesSent
78+
ch <- c.BytesReceived
79+
ch <- c.ConnectedSince
80+
}
7781
}
7882

7983
// Collect is called by the Prometheus registry when collecting metrics.
@@ -100,24 +104,26 @@ func (c *OpenVPNCollector) Collect(ch chan<- prometheus.Metric) {
100104
"bytesReceived", client.BytesReceived,
101105
"bytesSent", client.BytesSent,
102106
)
103-
ch <- prometheus.MustNewConstMetric(
104-
c.BytesReceived,
105-
prometheus.GaugeValue,
106-
client.BytesReceived,
107-
c.name, client.CommonName,
108-
)
109-
ch <- prometheus.MustNewConstMetric(
110-
c.BytesSent,
111-
prometheus.GaugeValue,
112-
client.BytesSent,
113-
c.name, client.CommonName,
114-
)
115-
ch <- prometheus.MustNewConstMetric(
116-
c.ConnectedSince,
117-
prometheus.GaugeValue,
118-
float64(client.ConnectedSince.Unix()),
119-
c.name, client.CommonName,
120-
)
107+
if c.collectClientMetrics {
108+
ch <- prometheus.MustNewConstMetric(
109+
c.BytesReceived,
110+
prometheus.GaugeValue,
111+
client.BytesReceived,
112+
c.name, client.CommonName,
113+
)
114+
ch <- prometheus.MustNewConstMetric(
115+
c.BytesSent,
116+
prometheus.GaugeValue,
117+
client.BytesSent,
118+
c.name, client.CommonName,
119+
)
120+
ch <- prometheus.MustNewConstMetric(
121+
c.ConnectedSince,
122+
prometheus.GaugeValue,
123+
float64(client.ConnectedSince.Unix()),
124+
c.name, client.CommonName,
125+
)
126+
}
121127
}
122128
level.Debug(c.logger).Log(
123129
"updatedAt", status.UpdatedAt,

pkg/command/command.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ func Run() error {
6868
Usage: "The OpenVPN status file(s) to export (example test:./example/version1.status )",
6969
Required: true,
7070
},
71+
&cli.BoolFlag{
72+
Name: "disable-client-metrics",
73+
Usage: "Disables per client (bytes_received, bytes_sent, connected_since) metrics",
74+
},
7175
}
7276

7377
app.Before = func(c *cli.Context) error {
74-
cfg.StatusFile = c.StringSlice("status-file")
78+
cfg.StatusCollector.StatusFile = c.StringSlice("status-file")
79+
cfg.StatusCollector.ExportClientMetrics = !c.Bool("disable-client-metrics")
7580
return nil
7681
}
7782

@@ -105,7 +110,7 @@ func run(c *cli.Context, cfg *config.Config) error {
105110
version.GoVersion,
106111
version.Started,
107112
))
108-
for _, statusFile := range cfg.StatusFile {
113+
for _, statusFile := range cfg.StatusCollector.StatusFile {
109114
serverName, statusFile := parseStatusFileSlice(statusFile)
110115

111116
level.Info(logger).Log(
@@ -117,6 +122,7 @@ func run(c *cli.Context, cfg *config.Config) error {
117122
logger,
118123
serverName,
119124
statusFile,
125+
cfg.StatusCollector.ExportClientMetrics,
120126
))
121127
}
122128

pkg/config/config.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ type Logs struct {
1414

1515
// Config defines the general configuration object
1616
type Config struct {
17-
Server Server
18-
Logs Logs
19-
StatusFile []string
17+
Server Server
18+
Logs Logs
19+
StatusCollector StatusCollector
20+
}
21+
22+
// StatusCollector contains configuration for the OpenVPN status collector
23+
type StatusCollector struct {
24+
ExportClientMetrics bool
25+
StatusFile []string
2026
}
2127

2228
// Load initializes a default configuration struct.

0 commit comments

Comments
 (0)