Skip to content

Commit

Permalink
fix(interfaces-regex): compile at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
rwxd committed Nov 24, 2023
1 parent 003b14d commit e2fd2ee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
28 changes: 26 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"regexp"
"strings"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -23,9 +24,12 @@ var (
excludeInterfaces = flag.String("exclude.interfaces", "", "Comma seperated list of interfaces to exclude")
includeInterfaces = flag.String("include.interfaces", "", "Comma seperated list of interfaces to include")
excludeInterfacesRegex = flag.String("exclude.interfaces-regex", "", "Regex of interfaces to exclude")
includeInterfaceRegex = flag.String("include.interfaces-regex", "", "Regex of interfaces to include")
includeInterfacesRegex = flag.String("include.interfaces-regex", "", "Regex of interfaces to include")
excludeInterfacesDown = flag.Bool("exclude.interfaces-down", false, "Don't report on interfaces being management DOWN")
powerUnitdBm = flag.Bool("collector.optical-power-in-dbm", false, "Report optical powers in dBm instead of mW (default false -> mW)")

excludeInterfacesRegexCompiled = &regexp.Regexp{}
includeInterfacesRegexCompiled = &regexp.Regexp{}
)

func main() {
Expand All @@ -36,6 +40,10 @@ func main() {
os.Exit(0)
}

if err := compileRegexFlags(); err != nil {
log.Fatalf(err.Error())
}

startServer()
}

Expand All @@ -46,6 +54,21 @@ func printVersion() {
fmt.Println("Metrics Exporter for pluggable transceivers on Linux based hosts / switches")
}

// compileRegexFlags compiles the cli regex flags into the global variables
// and returns an error if the regex is invalid
func compileRegexFlags() error {
var err error
excludeInterfacesRegexCompiled, err = regexp.Compile(*excludeInterfacesRegex)
if err != nil {
return fmt.Errorf("error compiling exclude.interfaces-regex: %v", err)
}
includeInterfacesRegexCompiled, err = regexp.Compile(*includeInterfacesRegex)
if err != nil {
return fmt.Errorf("error compiling include.interfaces-regex: %v", err)
}
return nil
}

func startServer() {
log.Infof("Starting transceiver-exporter (version: %s)\n", version)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -101,7 +124,8 @@ func handleMetricsRequest(w http.ResponseWriter, request *http.Request) {
includedIfaceNames[index] = strings.Trim(includedIfaceName, " ")
}
}
transceiverCollector := transceivercollector.NewCollector(excludedIfaceNames, includedIfaceNames, *excludeInterfacesRegex, *includeInterfaceRegex, *excludeInterfacesDown, *collectInterfaceFeatures, *powerUnitdBm)

transceiverCollector := transceivercollector.NewCollector(excludedIfaceNames, includedIfaceNames, excludeInterfacesRegexCompiled, includeInterfacesRegexCompiled, *excludeInterfacesDown, *collectInterfaceFeatures, *powerUnitdBm)
wrapper := &transceiverCollectorWrapper{
collector: transceiverCollector,
}
Expand Down
20 changes: 5 additions & 15 deletions transceiver-collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ var laserLabels = []string{"interface", "laser_index"}
type TransceiverCollector struct {
excludeInterfaces []string
includeInterfaces []string
excludeInterfacesRegex string
includeInterfacesRegex string
excludeInterfacesRegex *regexp.Regexp
includeInterfacesRegex *regexp.Regexp
excludeInterfacesDown bool
collectInterfaceFeatures bool
powerUnitdBm bool
Expand Down Expand Up @@ -177,7 +177,7 @@ func init() {
}

// NewCollector initializes a new TransceiverCollector
func NewCollector(excludeInterfaces []string, includeInterfaces []string, excludeInterfacesRegex, includeInterfacesRegex string, excludeInterfacesDown bool, collectInterfaceFeatures bool, powerUnitdBm bool) *TransceiverCollector {
func NewCollector(excludeInterfaces []string, includeInterfaces []string, excludeInterfacesRegex, includeInterfacesRegex *regexp.Regexp, excludeInterfacesDown bool, collectInterfaceFeatures bool, powerUnitdBm bool) *TransceiverCollector {
laserTxPowerThresholdsSupportedDesc = prometheus.NewDesc(prefix+"laser_tx_power_supports_thresholds_bool", "1 if thresholds for the laser tx power are supported", laserLabels, nil)
laserRxPowerThresholdsSupportedDesc = prometheus.NewDesc(prefix+"laser_rx_power_supports_thresholds_bool", "1 if thresholds for the laser rx power are supported", laserLabels, nil)
if powerUnitdBm {
Expand Down Expand Up @@ -308,16 +308,6 @@ func (t *TransceiverCollector) getMonitoredInterfaces() ([]string, error) {
return []string{}, errors.New("Cannot include and exclude interfaces at the same time")
}

interfacesExcludeRegexCompiled, err := regexp.Compile(t.excludeInterfacesRegex)
if err != nil {
return []string{}, errors.Wrapf(err, "Could not compile regex %s", t.excludeInterfacesRegex)
}

interfacesIncludeRegexCompiled, err := regexp.Compile(t.includeInterfacesRegex)
if err != nil {
return []string{}, errors.Wrapf(err, "Could not compile regex %s", t.includeInterfacesRegex)
}

ifaceNames := []string{}
for _, iface := range interfaces {
if iface.Flags&net.FlagLoopback > 0 {
Expand All @@ -332,10 +322,10 @@ func (t *TransceiverCollector) getMonitoredInterfaces() ([]string, error) {
if InterfacesIncluded && !contains(t.includeInterfaces, iface.Name) {
continue
}
if len(t.excludeInterfacesRegex) > 0 && interfacesExcludeRegexCompiled.MatchString(iface.Name) {
if len(t.excludeInterfacesRegex.String()) > 0 && t.excludeInterfacesRegex.MatchString(iface.Name) {
continue
}
if len(t.includeInterfacesRegex) > 0 && !interfacesIncludeRegexCompiled.MatchString(iface.Name) {
if len(t.includeInterfacesRegex.String()) > 0 && !t.includeInterfacesRegex.MatchString(iface.Name) {
continue
}

Expand Down

0 comments on commit e2fd2ee

Please sign in to comment.