~sirn/aquacomputer-mqtt-exporter

69338e5379b2f93fb0b4d389fb556a89c1275bec — Sirn Thanabulpong 6 months ago 3c525c0 main v0.2.0
Make functions private
1 files changed, 9 insertions(+), 9 deletions(-)

M internal/aquacomputer/collector.go
M internal/aquacomputer/collector.go => internal/aquacomputer/collector.go +9 -9
@@ 44,13 44,13 @@ func (c *AquacomputerCollector) Collect(ch chan<- prometheus.Metric) {
	}
}

// GenerateName generates a new name with prefix.
func (c *AquacomputerCollector) GenerateName(name string) string {
// generateName generates a new name with prefix.
func (c *AquacomputerCollector) generateName(name string) string {
	return fmt.Sprintf("%s_%s", c.config.Metric.Prefix, name)
}

// NewCollectorFunc returns a collector function.
func (c *AquacomputerCollector) NewCollectorFunc() func(mqtt.Client, mqtt.Message) {
// newCollectorFunc returns a collector function.
func (c *AquacomputerCollector) newCollectorFunc() func(mqtt.Client, mqtt.Message) {
	extractor := regexp.MustCompile(c.config.Metric.Regexp)
	groupNames := extractor.SubexpNames()



@@ 87,7 87,7 @@ func (c *AquacomputerCollector) NewCollectorFunc() func(mqtt.Client, mqtt.Messag
			if _, ok := c.metrics[comp]; !ok {
				c.metrics[comp] = &AquacomputerMetric{
					gauge: prometheus.NewGauge(prometheus.GaugeOpts{
						Name: c.GenerateName(matched["name"]),
						Name: c.generateName(matched["name"]),
						ConstLabels: prometheus.Labels{
							"device": matched["device"],
							"name":   d.Name,


@@ 103,8 103,8 @@ func (c *AquacomputerCollector) NewCollectorFunc() func(mqtt.Client, mqtt.Messag
	}
}

// SetupPruner sets up pruner.
func (c *AquacomputerCollector) SetupPruner(interval time.Duration) {
// setupPruner sets up pruner.
func (c *AquacomputerCollector) setupPruner(interval time.Duration) {
	for range time.Tick(interval * time.Second) {
		c.Lock()
		cutoff := time.Now().Add(-1 * interval * time.Second)


@@ 128,12 128,12 @@ func NewCollector(config *config.Config, logger slogger.Logger, mqttclient *mqtt
		mqttclient: mqttclient,
	}

	if err := mqttclient.Subscribe(config.MQTT.Topic, ac.NewCollectorFunc()); err != nil {
	if err := mqttclient.Subscribe(config.MQTT.Topic, ac.newCollectorFunc()); err != nil {
		logger.Fatalf("could not subscribe to topic: %s", err)
	}

	if config.Metric.PruneInterval > 0 {
		go ac.SetupPruner(time.Duration(config.Metric.PruneInterval))
		go ac.setupPruner(time.Duration(config.Metric.PruneInterval))
	}

	return ac