Skip to content

Commit

Permalink
Merge pull request #56 from hikhvar/web-config
Browse files Browse the repository at this point in the history
Add experimental support for web-config-file
  • Loading branch information
hikhvar authored Mar 24, 2021
2 parents d801ff7 + 48fb74e commit a25cf3f
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
# Test oldest and newest supported version
go-version: [1.13.x, 1.16.x]
go-version: [1.14.x, 1.16.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
18 changes: 17 additions & 1 deletion cmd/mqtt2prometheus.go
Original file line number Diff line number Diff line change
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
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ go 1.14

require (
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/prometheus/procfs v0.6.0 // indirect
github.com/thedevsaddam/gojsonq/v2 v2.5.2
go.uber.org/zap v1.16.0
gopkg.in/yaml.v2 v2.2.5
golang.org/x/sys v0.0.0-20210324051608-47abb6519492 // indirect
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit a25cf3f

Please sign in to comment.