Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/fix cache config #53

Merged
merged 2 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions hack/shelly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mqtt:
metric_per_topic_config:
metric_name_regex: "shellies/(?P<deviceid>.*)/sensor/(?P<metricname>.*)"
qos: 0
cache:
timeout: 24h
cache:
timeout: 24h
metrics:
- prom_name: temperature
# The name of the metric in a MQTT JSON message
Expand Down Expand Up @@ -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
# The name of the metric in prometheus
10 changes: 4 additions & 6 deletions pkg/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"time"
)

const DefaultTimeout = 0

type Collector interface {
prometheus.Collector
Observe(deviceID string, collection MetricCollection)
Expand All @@ -32,7 +30,7 @@ type Metric struct {

type CacheItem struct {
DeviceID string
Metric Metric
Metric Metric
}

type MetricCollection []Metric
Expand All @@ -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)
}
}

Expand Down