Skip to content

Commit f0457e1

Browse files
authored
Merge pull request #5 from nikiforov-soft/feature/prometheus-decouple
Decoupling prometheus output
2 parents 2abf006 + c2c5d4b commit f0457e1

22 files changed

Lines changed: 627 additions & 194 deletions

File tree

config.dist.yaml

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,76 @@ metrics:
88
tls:
99
certificateFile: ""
1010
privateKeyFile: ""
11+
stalenessInterval: 5m # Deletes metrics if we have not received any updates for them within the given interval
12+
metricsMapping:
13+
# LYWSD03MMC mappings
14+
- name: "temperature_celsius"
15+
description: "sensor value."
16+
namespace: "yasp"
17+
subsystem: "lywsd03mmc"
18+
type: Gauge
19+
labels:
20+
- device
21+
- deviceType
22+
- name: "humidity_percent"
23+
description: "sensor value."
24+
namespace: "yasp"
25+
subsystem: "lywsd03mmc"
26+
type: Gauge
27+
labels:
28+
- device
29+
- deviceType
30+
- name: "battery_percent"
31+
description: "sensor value."
32+
namespace: "yasp"
33+
subsystem: "lywsd03mmc"
34+
type: Gauge
35+
labels:
36+
- device
37+
- deviceType
38+
# P1P2 mappings
39+
- name: "temperature"
40+
description: "p1p2 target temperature."
41+
namespace: "yasp"
42+
subsystem: "p1p2"
43+
type: Gauge
44+
labels:
45+
- device
46+
- name: "status"
47+
description: "p1p2 operation status."
48+
namespace: "yasp"
49+
subsystem: "p1p2"
50+
type: Gauge
51+
labels:
52+
- device
53+
- name: "test_mode"
54+
description: "p1p2 test mode status."
55+
namespace: "yasp"
56+
subsystem: "p1p2"
57+
type: Gauge
58+
labels:
59+
- device
60+
- name: "error_code"
61+
description: "p1p2 error code."
62+
namespace: "yasp"
63+
subsystem: "p1p2"
64+
type: Gauge
65+
labels:
66+
- device
67+
- name: "mode"
68+
description: "p1p2 mode, 1 = cooling, 2 = heating."
69+
type: Gauge
70+
namespace: "yasp"
71+
subsystem: "p1p2"
72+
labels:
73+
- device
74+
- name: "fan_speed"
75+
description: "p1p2 fan speed, 1 = low, 2 = medium, 3 = high."
76+
namespace: "yasp"
77+
subsystem: "p1p2"
78+
type: Gauge
79+
labels:
80+
- device
1181
sensors:
1282
- name: Xiaomi Mi Temperature and Humidity Monitor 2
1383
enabled: false
@@ -26,7 +96,7 @@ sensors:
2696
password: ""
2797
clientId: LYWSD03MMC_Subscriber
2898
keepAlive: 5
29-
connectRetryDelay: 5s
99+
qos: 0
30100
memphis:
31101
enabled: false
32102
hostname: 127.0.0.1:6666
@@ -48,7 +118,6 @@ sensors:
48118
password: ""
49119
clientId: LYWSD03MMC_Publisher
50120
keepAlive: 5
51-
connectRetryDelay: 5s
52121
qos: 0
53122
retain: false
54123
- influxdb2:
@@ -78,45 +147,36 @@ sensors:
78147
{{- else if or (eq (index .Properties "unit") "Humidity") (eq (index .Properties "unit") "Battery") -}}
79148
_percent
80149
{{- end -}}
81-
description: "sensor value." # Templating supported
82150
namespace: "yasp"
83151
subsystem: "{{- index .Properties \"deviceType\" | ToLower -}}" # Templating supported
152+
value: "{{ index .Properties \"value\" | ToNumber }}" # Templating supported
84153
labels:
85154
device: "{{- index .Properties \"deviceName\" -}}" # Templating supported on both keys and values
86155
deviceType: "{{- index .Properties \"deviceType\" -}}"
87-
type: Gauge # Templating supported
88-
value: "{{ index .Properties \"value\" | ToNumber }}" # Templating supported
89-
condition: "" # Templating supported
90156
# Individual mapping per unit example
91157
# - name: Temperature
92-
# description: Temperature sensor value.
93158
# namespace: ""
94159
# subsystem: ""
160+
# value: "{{ index .Properties \"value\" | ToNumber }}"
95161
# labels:
96162
# device: "{{ index .Properties \"deviceName\" }}"
97163
# deviceType: "{{ index .Properties \"deviceType\" }}"
98-
# type: Gauge
99-
# value: "{{ index .Properties \"value\" | ToNumber }}"
100164
# condition: "{{ eq (index .Properties \"unit\") \"Temperature\" }}"
101165
# - name: Humidity
102-
# description: Humidity sensor value.
103166
# namespace: ""
104167
# subsystem: ""
168+
# value: "{{ index .Properties \"value\" | ToNumber }}"
105169
# labels:
106170
# device: "{{ index .Properties \"deviceName\" }}"
107171
# deviceType: "{{ index .Properties \"deviceType\" }}"
108-
# type: Gauge
109-
# value: "{{ index .Properties \"value\" | ToNumber }}"
110172
# condition: "{{ eq (index .Properties \"unit\") \"Humidity\" }}"
111173
# - name: Battery
112-
# description: Battery sensor value.
113174
# namespace: ""
114175
# subsystem: ""
176+
# value: "{{ index .Properties \"value\" | ToNumber }}"
115177
# labels:
116178
# device: "{{ index .Properties \"deviceName\" }}"
117179
# deviceType: "{{ index .Properties \"deviceType\" }}"
118-
# type: Gauge
119-
# value: "{{ index .Properties \"value\" | ToNumber }}"
120180
# condition: "{{- if eq (index .Properties \"unit\") \"Battery\" -}}true{{- else - }}false{{- end -}}"
121181
devices:
122182
- name: Your Sensor
@@ -135,54 +195,42 @@ sensors:
135195
- tcp://localhost:1883
136196
clientId: P1P2_Subscriber
137197
keepAlive: 5
138-
connectRetryDelay: 5s
198+
qos: 0
139199
outputs:
140200
- prometheus:
141201
enabled: true
142202
metricsMapping:
143203
- name: "temperature"
144-
description: "p1p2 target temperature."
145-
type: Gauge
146204
value: "{{ index .Properties \"temperature\" | ToNumber }}"
147205
namespace: "yasp"
148206
subsystem: "p1p2"
149207
labels:
150208
device: "{{ index .Properties \"bridge\" }}"
151209
- name: "status"
152-
description: "p1p2 operation status."
153-
type: Gauge
154210
value: "{{ index .Properties \"status\" | ToNumber }}"
155211
namespace: "yasp"
156212
subsystem: "p1p2"
157213
labels:
158214
device: "{{ index .Properties \"bridge\" }}"
159215
- name: "testMode"
160-
description: "p1p2 test mode status."
161-
type: Gauge
162216
value: "{{ index .Properties \"testMode\" | ToNumber }}"
163217
namespace: "yasp"
164218
subsystem: "p1p2"
165219
labels:
166220
device: "{{ index .Properties \"bridge\" }}"
167221
- name: "errorCode"
168-
description: "p1p2 error code."
169-
type: Gauge
170222
value: "{{ index .Properties \"errorCode\" | ToNumber }}"
171223
namespace: "yasp"
172224
subsystem: "p1p2"
173225
labels:
174226
device: "{{ index .Properties \"bridge\" }}"
175227
- name: "mode"
176-
description: "p1p2 mode, 1 = cooling, 2 = heating."
177-
type: Gauge
178228
value: "{{ index .Properties \"modeId\" | ToNumber }}"
179229
namespace: "yasp"
180230
subsystem: "p1p2"
181231
labels:
182232
device: "{{ index .Properties \"bridge\" }}"
183233
- name: "fanSpeed"
184-
description: "p1p2 fan speed, 1 = low, 2 = medium, 3 = high."
185-
type: Gauge
186234
value: "{{ index .Properties \"fanSpeedId\" | ToNumber }}"
187235
namespace: "yasp"
188236
subsystem: "p1p2"

config/metrics.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package config
22

3+
import (
4+
"time"
5+
)
6+
37
type Metrics struct {
4-
Enabled bool `yaml:"enabled"`
5-
ListenAddr string `yaml:"listenAddr"`
6-
Endpoint string `yaml:"endpoint"`
7-
TLS MetricsTlsConfig `yaml:"tls"`
8+
Enabled bool `yaml:"enabled"`
9+
ListenAddr string `yaml:"listenAddr"`
10+
Endpoint string `yaml:"endpoint"`
11+
TLS MetricsTlsConfig `yaml:"tls"`
12+
MetricsMapping []*MetricsMapping `yaml:"metricsMapping"`
13+
StalenessInterval time.Duration `yaml:"stalenessInterval"`
814
}
915

1016
type MetricsTlsConfig struct {

config/metrics_mapping.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package config
2+
3+
import (
4+
"time"
5+
)
6+
7+
type MetricsMapping struct {
8+
// Counter, Gauge
9+
Name string `yaml:"name"`
10+
Description string `yaml:"description"`
11+
Namespace string `yaml:"namespace"`
12+
Subsystem string `yaml:"subsystem"`
13+
Labels []string `yaml:"labels"`
14+
ConstLabels map[string]string `yaml:"constLabels"`
15+
// Histogram
16+
Buckets []float64 `yaml:"buckets"`
17+
NativeHistogramBucketFactor float64 `yaml:"nativeHistogramBucketFactor"`
18+
NativeHistogramZeroThreshold float64 `yaml:"nativeHistogramZeroThreshold"`
19+
NativeHistogramMaxBucketNumber uint32 `yaml:"nativeHistogramMaxBucketNumber"`
20+
NativeHistogramMinResetDuration time.Duration `yaml:"nativeHistogramMinResetDuration"`
21+
NativeHistogramMaxZeroThreshold float64 `yaml:"nativeHistogramMaxZeroThreshold"`
22+
NativeHistogramMaxExemplars int `yaml:"nativeHistogramMaxExemplars"`
23+
NativeHistogramExemplarTTL time.Duration `yaml:"nativeHistogramExemplarTTL"`
24+
// Summary
25+
Objectives map[float64]float64 `yaml:"objectives"`
26+
MaxAge time.Duration `yaml:"maxAge"`
27+
AgeBuckets uint32 `yaml:"ageBuckets"`
28+
BufCap uint32 `yaml:"bufCap"`
29+
// Additional context
30+
Type string `yaml:"type"`
31+
}

config/mqtt_input.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package config
22

33
import (
44
"net/url"
5-
"time"
65
)
76

87
type MqttInput struct {
9-
Enabled bool `yaml:"enabled"`
10-
Topics []string `yaml:"topics"`
11-
BrokerUrls []*Url `yaml:"brokerUrls"`
12-
Username string `yaml:"username"`
13-
Password string `yaml:"password"`
14-
ClientId string `yaml:"clientId"`
15-
KeepAlive uint16 `yaml:"keepAlive"`
16-
ConnectRetryDelay time.Duration `yaml:"connectRetryDelay"`
8+
Enabled bool `yaml:"enabled"`
9+
Topics []string `yaml:"topics"`
10+
BrokerUrls []*Url `yaml:"brokerUrls"`
11+
Username string `yaml:"username"`
12+
Password string `yaml:"password"`
13+
ClientId string `yaml:"clientId"`
14+
KeepAlive uint16 `yaml:"keepAlive"`
15+
QoS byte `yaml:"qos"`
1716
}
1817

1918
func (m *MqttInput) GetBrokerUrls() []*url.URL {

config/mqtt_output.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@ package config
22

33
import (
44
"net/url"
5-
"time"
65
)
76

87
type MqttOutput struct {
9-
Enabled bool `yaml:"enabled"`
10-
Topic string `yaml:"topic"`
11-
BrokerUrls []*Url `yaml:"brokerUrls"`
12-
Username string `yaml:"username"`
13-
Password string `yaml:"password"`
14-
ClientId string `yaml:"clientId"`
15-
KeepAlive uint16 `yaml:"keepAlive"`
16-
ConnectRetryDelay time.Duration `yaml:"connectRetryDelay"`
17-
QoS byte `yaml:"qos"`
18-
Retain bool `yaml:"bool"`
8+
Enabled bool `yaml:"enabled"`
9+
Topic string `yaml:"topic"`
10+
BrokerUrls []*Url `yaml:"brokerUrls"`
11+
Username string `yaml:"username"`
12+
Password string `yaml:"password"`
13+
ClientId string `yaml:"clientId"`
14+
KeepAlive uint16 `yaml:"keepAlive"`
15+
QoS byte `yaml:"qos"`
16+
Retain bool `yaml:"bool"`
1917
}
2018

2119
func (m *MqttOutput) GetBrokerUrls() []*url.URL {

config/prometheus.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
package config
22

3-
import (
4-
"time"
5-
)
6-
73
type Prometheus struct {
84
Enabled bool `yaml:"enabled"`
95
MetricsMapping []PrometheusMetricsMapping `yaml:"metricsMapping"`
106
}
117

128
type PrometheusMetricsMapping struct {
13-
// Counter, Gauge
14-
Name string `yaml:"name"`
15-
Description string `yaml:"description"`
16-
Namespace string `yaml:"namespace"`
17-
Subsystem string `yaml:"subsystem"`
18-
Labels map[string]string `yaml:"labels"`
19-
// Histogram
20-
Buckets []float64 `yaml:"buckets"`
21-
NativeHistogramBucketFactor float64 `yaml:"nativeHistogramBucketFactor"`
22-
NativeHistogramZeroThreshold float64 `yaml:"nativeHistogramZeroThreshold"`
23-
NativeHistogramMaxBucketNumber uint32 `yaml:"nativeHistogramMaxBucketNumber"`
24-
NativeHistogramMinResetDuration time.Duration `yaml:"nativeHistogramMinResetDuration"`
25-
NativeHistogramMaxZeroThreshold float64 `yaml:"nativeHistogramMaxZeroThreshold"`
26-
// Summary
27-
Objectives map[float64]float64 `yaml:"objectives"`
28-
MaxAge time.Duration `yaml:"maxAge"`
29-
AgeBuckets uint32 `yaml:"ageBuckets"`
30-
BufCap uint32 `yaml:"bufCap"`
31-
// Additional context
32-
Type string `yaml:"type"`
33-
Value string `yaml:"value"`
34-
Condition string `yaml:"condition"`
9+
Name string `yaml:"name"`
10+
Namespace string `yaml:"namespace"`
11+
Subsystem string `yaml:"subsystem"`
12+
Labels map[string]string `yaml:"labels"`
13+
Value string `yaml:"value"`
14+
Condition string `yaml:"condition"`
3515
}

input/impl/memphis/memphis.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func (mi *memphisInput) messageHandler(messages []*memphis.Msg, err error, ctx c
102102
logrus.
103103
WithField("headers", message.GetHeaders()).
104104
WithField("payload", string(message.Data())).
105+
WithField("source", "memphis").
105106
Debug("input received")
106107

107108
data := &input.Data{
@@ -120,7 +121,7 @@ func (mi *memphisInput) messageHandler(messages []*memphis.Msg, err error, ctx c
120121
if mi.config.HeaderPrefixes != nil {
121122
var exists bool
122123
for k, v := range message.GetHeaders() {
123-
if requiredPrefix, exists := mi.config.HeaderPrefixes[k]; exists {
124+
if requiredPrefix, hasPrefix := mi.config.HeaderPrefixes[k]; hasPrefix {
124125
matches, err := filepath.Match(requiredPrefix, v)
125126
if err != nil {
126127
return

0 commit comments

Comments
 (0)