Skip to content

Commit

Permalink
Support multiple mqtt2prometheus instances per host by incorporate th…
Browse files Browse the repository at this point in the history
…e pid into the mqtt client ID
  • Loading branch information
Christoph Petrausch committed Sep 19, 2020
1 parent 9e4ab53 commit 31b233b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ static_build:

container:
docker build -t mqtt2prometheus:latest .

test_release:
goreleaser --rm-dist --skip-validate --skip-publish
10 changes: 10 additions & 0 deletions cmd/mqtt2prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func main() {
mqttClientOptions.AddBroker(cfg.MQTT.Server).SetClientID(hostName).SetCleanSession(true)
mqttClientOptions.SetUsername(cfg.MQTT.User)
mqttClientOptions.SetPassword(cfg.MQTT.Password)
mqttClientOptions.SetClientID(mustMQTTClientID())

collector := metrics.NewCollector(cfg.Cache.Timeout, cfg.Metrics)
ingest := metrics.NewIngest(collector, cfg.Metrics, cfg.MQTT.DeviceIDRegex)
Expand Down Expand Up @@ -127,3 +128,12 @@ func mustShowVersion() {
panic(err)
}
}

func mustMQTTClientID() string {
host, err := os.Hostname()
if err != nil {
panic(fmt.Sprintf("failed to get hostname: %w", err))
}
pid := os.Getpid()
return fmt.Sprintf("%s-%d", host, pid)
}

0 comments on commit 31b233b

Please sign in to comment.