Skip to content

Commit

Permalink
Add -collector.optical-power-in-dbm flag
Browse files Browse the repository at this point in the history
  Report optical powers in dBm instead of mW (default false -> mW)

Signed-off-by: Maximilian Wilhelm <[email protected]>
  • Loading branch information
Maximilian Wilhelm committed Aug 12, 2022
1 parent 744767c commit 639e67c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 85 deletions.
46 changes: 24 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package main
import (
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"gitlab.com/wobcom/transceiver-exporter/transceiver-collector"
"net/http"
"os"
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
transceivercollector "gitlab.com/wobcom/transceiver-exporter/transceiver-collector"
)

const version string = "1.0"
Expand All @@ -20,6 +21,7 @@ var (
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics")
collectInterfaceFeatures = flag.Bool("collector.interface-features.enable", true, "Collect interface features")
excludeInterfaces = flag.String("exclude.interfaces", "", "Comma seperated list of interfaces to exclude")
powerUnitdBm = flag.Bool("collector.optical-power-in-dbm", false, "Report optical powers in dBm instead of mW (default false -> mW)")
)

func main() {
Expand Down Expand Up @@ -57,25 +59,25 @@ func startServer() {
}

type transceiverCollectorWrapper struct {
collector *transceivercollector.TransceiverCollector
collector *transceivercollector.TransceiverCollector
}

func (t transceiverCollectorWrapper) Collect (ch chan<- prometheus.Metric) {
errs := make(chan error)
done := make(chan struct{})
go t.collector.Collect(ch, errs, done)
for {
select {
case err := <-errs:
log.Errorf("Error while collecting metrics: %v", err)
case <- done:
return
}
}
func (t transceiverCollectorWrapper) Collect(ch chan<- prometheus.Metric) {
errs := make(chan error)
done := make(chan struct{})
go t.collector.Collect(ch, errs, done)
for {
select {
case err := <-errs:
log.Errorf("Error while collecting metrics: %v", err)
case <-done:
return
}
}
}

func (t transceiverCollectorWrapper) Describe(ch chan<- *prometheus.Desc) {
t.collector.Describe(ch)
t.collector.Describe(ch)
}

func handleMetricsRequest(w http.ResponseWriter, request *http.Request) {
Expand All @@ -85,10 +87,10 @@ func handleMetricsRequest(w http.ResponseWriter, request *http.Request) {
for index, excludedIfaceName := range excludedIfaceNames {
excludedIfaceNames[index] = strings.Trim(excludedIfaceName, " ")
}
transceiverCollector := transceivercollector.NewCollector(excludedIfaceNames, *collectInterfaceFeatures)
wrapper := &transceiverCollectorWrapper{
collector: transceiverCollector,
}
transceiverCollector := transceivercollector.NewCollector(excludedIfaceNames, *collectInterfaceFeatures, *powerUnitdBm)
wrapper := &transceiverCollectorWrapper{
collector: transceiverCollector,
}

registry.MustRegister(wrapper)
promhttp.HandlerFor(registry, promhttp.HandlerOpts{
Expand Down
Loading

0 comments on commit 639e67c

Please sign in to comment.