Skip to content

Commit

Permalink
Merge pull request #29 from hikhvar/fix-collection
Browse files Browse the repository at this point in the history
Refactor cache datastructure to use a key per metric instead of a key…
  • Loading branch information
hikhvar authored Jan 23, 2021
2 parents 31cd757 + e5cc73f commit 90584b7
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions pkg/metrics/collector.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package metrics

import (
"go.uber.org/zap"
"time"

"fmt"
"github.com/hikhvar/mqtt2prometheus/pkg/config"
gocache "github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"time"
)

const DefaultTimeout = 0
Expand All @@ -30,6 +30,11 @@ type Metric struct {
Topic string
}

type CacheItem struct {
DeviceID string
Metric Metric
}

type MetricCollection []Metric

func NewCollector(defaultTimeout time.Duration, possibleMetrics []config.MetricConfig, logger *zap.Logger) Collector {
Expand All @@ -45,10 +50,13 @@ func NewCollector(defaultTimeout time.Duration, possibleMetrics []config.MetricC
}

func (c *MemoryCachedCollector) Observe(deviceID string, collection MetricCollection) {
if len(collection) < 1 {
return
for _, m := range collection {
item := CacheItem{
DeviceID: deviceID,
Metric: m,
}
c.cache.Set(fmt.Sprintf("%s-%s", deviceID, m.Description.String()), item, DefaultTimeout)
}
c.cache.Set(deviceID, collection, DefaultTimeout)
}

func (c *MemoryCachedCollector) Describe(ch chan<- *prometheus.Desc) {
Expand All @@ -58,20 +66,20 @@ func (c *MemoryCachedCollector) Describe(ch chan<- *prometheus.Desc) {
}

func (c *MemoryCachedCollector) Collect(mc chan<- prometheus.Metric) {
for device, metricsRaw := range c.cache.Items() {
metrics := metricsRaw.Object.(MetricCollection)
for _, metric := range metrics {
if metric.Description == nil {
c.logger.Warn("empty description", zap.String("topic", metric.Topic), zap.Float64("value", metric.Value))
}
m := prometheus.MustNewConstMetric(
metric.Description,
metric.ValueType,
metric.Value,
device,
metric.Topic,
)
mc <- prometheus.NewMetricWithTimestamp(metric.IngestTime, m)
for _, metricsRaw := range c.cache.Items() {
item := metricsRaw.Object.(CacheItem)
device, metric := item.DeviceID, item.Metric
if metric.Description == nil {
c.logger.Warn("empty description", zap.String("topic", metric.Topic), zap.Float64("value", metric.Value))
}
m := prometheus.MustNewConstMetric(
metric.Description,
metric.ValueType,
metric.Value,
device,
metric.Topic,
)
mc <- prometheus.NewMetricWithTimestamp(metric.IngestTime, m)

}
}

0 comments on commit 90584b7

Please sign in to comment.