Skip to content

Commit

Permalink
Add experimental support for web-config-file
Browse files Browse the repository at this point in the history
Fixes #55
  • Loading branch information
hikhvar committed Mar 21, 2021
1 parent d801ff7 commit 1de4d0b
Show file tree
Hide file tree
Showing 4 changed files with 327 additions and 3 deletions.
18 changes: 17 additions & 1 deletion cmd/mqtt2prometheus.go
Expand Up @@ -15,11 +15,14 @@ import (
"go.uber.org/zap/zapcore"

mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/go-kit/kit/log"
kitzap "github.com/go-kit/kit/log/zap"
"github.com/hikhvar/mqtt2prometheus/pkg/config"
"github.com/hikhvar/mqtt2prometheus/pkg/metrics"
"github.com/hikhvar/mqtt2prometheus/pkg/mqttclient"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/exporter-toolkit/web"
)

// These variables are set by goreleaser at linking time.
Expand Down Expand Up @@ -56,6 +59,11 @@ var (
"console",
"set the desired log output format. Valid values are 'console' and 'json'",
)
webConfigFlag = flag.String(
"web-config-file",
"",
"[EXPERIMENTAL] Path to configuration file that can enable TLS or authentication for metric scraping.",
)
)

func main() {
Expand Down Expand Up @@ -119,8 +127,12 @@ func main() {
prometheus.MustRegister(ingest.Collector())
prometheus.MustRegister(collector)
http.Handle("/metrics", promhttp.Handler())
s := &http.Server{
Addr: getListenAddress(),
Handler: http.DefaultServeMux,
}
go func() {
err = http.ListenAndServe(getListenAddress(), nil)
err = web.ListenAndServe(s, *webConfigFlag, setupGoKitLogger(logger))
if err != nil {
logger.Fatal("Error while serving http", zap.Error(err))
}
Expand Down Expand Up @@ -183,6 +195,10 @@ func mustSetupLogger() *zap.Logger {
return logger
}

func setupGoKitLogger(l *zap.Logger) log.Logger {
return kitzap.NewZapSugarLogger(l, zap.NewAtomicLevelAt(*logLevelFlag).Level())
}

func setupExtractor(cfg config.Config) (metrics.Extractor, error) {
parser := metrics.NewParser(cfg.Metrics)
if cfg.MQTT.ObjectPerTopicConfig != nil {
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Expand Up @@ -3,10 +3,13 @@ module github.com/hikhvar/mqtt2prometheus
go 1.14

require (
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415 // indirect
github.com/eclipse/paho.mqtt.golang v1.1.1
github.com/go-kit/kit v0.10.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.6.0
github.com/prometheus/client_golang v1.7.1
github.com/prometheus/exporter-toolkit v0.5.1
github.com/thedevsaddam/gojsonq/v2 v2.5.2
go.uber.org/zap v1.16.0
gopkg.in/yaml.v2 v2.2.5
gopkg.in/yaml.v2 v2.4.0
)

0 comments on commit 1de4d0b

Please sign in to comment.