From 5805d04c186c1846df9e9d56e797c63289f58067 Mon Sep 17 00:00:00 2001 From: Chris Aumann Date: Sun, 14 Mar 2021 10:57:08 +0100 Subject: [PATCH 1/2] Fix cache key in shelly.yaml example --- hack/shelly.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hack/shelly.yaml b/hack/shelly.yaml index d43c5dd..c9b28ad 100644 --- a/hack/shelly.yaml +++ b/hack/shelly.yaml @@ -5,8 +5,8 @@ mqtt: metric_per_topic_config: metric_name_regex: "shellies/(?P.*)/sensor/(?P.*)" qos: 0 - cache: - timeout: 24h +cache: + timeout: 24h metrics: - prom_name: temperature # The name of the metric in a MQTT JSON message @@ -40,4 +40,4 @@ metrics: # A map of string to string for constant labels. This labels will be attached to every prometheus metric const_labels: sensor_type: shelly - # The name of the metric in prometheus \ No newline at end of file + # The name of the metric in prometheus From 6ab535ebe1264169a0f796b4aa4070803a61e080 Mon Sep 17 00:00:00 2001 From: Chris Aumann Date: Sun, 14 Mar 2021 10:58:20 +0100 Subject: [PATCH 2/2] Use upstream DefaultExpiration for go-cache --- pkg/metrics/collector.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/metrics/collector.go b/pkg/metrics/collector.go index 7a41d87..0016839 100644 --- a/pkg/metrics/collector.go +++ b/pkg/metrics/collector.go @@ -9,8 +9,6 @@ import ( "time" ) -const DefaultTimeout = 0 - type Collector interface { prometheus.Collector Observe(deviceID string, collection MetricCollection) @@ -32,7 +30,7 @@ type Metric struct { type CacheItem struct { DeviceID string - Metric Metric + Metric Metric } type MetricCollection []Metric @@ -52,10 +50,10 @@ func NewCollector(defaultTimeout time.Duration, possibleMetrics []config.MetricC func (c *MemoryCachedCollector) Observe(deviceID string, collection MetricCollection) { for _, m := range collection { item := CacheItem{ - DeviceID: deviceID, - Metric: m, + DeviceID: deviceID, + Metric: m, } - c.cache.Set(fmt.Sprintf("%s-%s", deviceID, m.Description.String()), item, DefaultTimeout) + c.cache.Set(fmt.Sprintf("%s-%s", deviceID, m.Description.String()), item, gocache.DefaultExpiration) } }