Skip to content

Commit 59497bd

Browse files
committed
Update + document Prometheus metrics.
1 parent 056bd57 commit 59497bd

File tree

13 files changed

+277
-252
lines changed

13 files changed

+277
-252
lines changed

docs/content/backends/basic-station.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,27 @@ Basic Station. The flow for this is:
3434

3535
* The Basic Station does not send RX / TX stats
3636

37+
## Prometheus metrics
38+
39+
The Semtech Basic Station packet-forwarder backend exposes several [Prometheus](https://prometheus.io/)
40+
metrics for monitoring.
41+
42+
### backend_basicstation_websocket_ping_pong_count
43+
44+
The number of WebSocket Ping/Pong requests sent and received (per event type).
45+
46+
### backend_basicstation_websocket_received_count
47+
48+
The number of WebSocket messages received by the backend (per msgtype).
49+
50+
### backend_basicstation_websocket_sent_count
51+
52+
The number of WebSocket messages sent by the backend (per msgtype).
53+
54+
### backend_basicstation_gateway_connect_count
55+
56+
The number of gateway connections received by the backend.
57+
58+
### backend_basicstation_gateway_disconnect_count
59+
60+
The number of gateways that disconnected from the backend.

docs/content/backends/semtech-udp.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,24 @@ This works as follow:
6565
For the above to work, you must configure the _Mandaged packet-forwarder configuration_
6666
section in the `lora-gateway-bridge.toml`.
6767

68+
## Prometheus metrics
69+
70+
The Semtech UDP packet-forwarder backend exposes several [Prometheus](https://prometheus.io/)
71+
metrics for monitoring.
72+
73+
### backend_semtechudp_udp_sent_count
74+
75+
The number of UDP packets sent by the backend (per packet_type).
76+
77+
78+
### backend_semtechudp_udp_received_count
79+
80+
The number of UDP packets received by the backend (per packet_type).
81+
82+
### backend_semtechudp_gateway_connect_count
83+
84+
The number of gateway connections received by the backend.
85+
86+
### backend_semtechudp_gateway_disconnect_count
87+
88+
The number of gateways that disconnected from the backend.

docs/content/integrate/metrics.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: Prometheus metrics
3+
description: Prometheus metrics exposed by the MQTT backend.
4+
menu:
5+
main:
6+
parent: integrate
7+
weight: 4
8+
---
9+
10+
# Prometheus metrics
11+
12+
Independent from the chosen MQTT authentication type, the MQTT integration
13+
expses the following [Prometheus](https://prometheus.io/) metrics for monitoring.
14+
15+
### integration_mqtt_event_count
16+
17+
The number of gateway events published by the MQTT integration (per event).
18+
19+
### integration_mqtt_command_count
20+
21+
The number of commands received by the MQTT integration (per command).
22+
23+
### integration_mqtt_connect_count
24+
25+
The number of times the integration connected to the MQTT broker.
26+
27+
### integration_mqtt_disconnect_count
28+
29+
The number of times the integration disconnected from the MQTT broker.
30+
31+
### integration_mqtt_reconnect_count
32+
33+
The number of times the integration reconnected to the MQTT broker (this also increments the disconnect and connect counters).
34+

go.mod

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@ require (
77
github.com/brocaar/lorawan v0.0.0-20190402092148-5bca41b178e9
88
github.com/dgrijalva/jwt-go v3.2.0+incompatible
99
github.com/eclipse/paho.mqtt.golang v1.2.0
10-
github.com/golang/protobuf v1.3.1
10+
github.com/go-kit/kit v0.9.0 // indirect
11+
github.com/golang/protobuf v1.3.2
1112
github.com/goreleaser/goreleaser v0.106.0
1213
github.com/gorilla/websocket v1.4.0
1314
github.com/pkg/errors v0.8.1
14-
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829
15-
github.com/sirupsen/logrus v1.4.1
15+
github.com/prometheus/client_golang v1.0.0
16+
github.com/prometheus/common v0.6.0 // indirect
17+
github.com/prometheus/procfs v0.0.3 // indirect
18+
github.com/sirupsen/logrus v1.4.2
1619
github.com/spf13/cobra v0.0.3
1720
github.com/spf13/viper v1.3.2
21+
github.com/stretchr/objx v0.2.0 // indirect
1822
github.com/stretchr/testify v1.3.0
23+
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
1924
golang.org/x/lint v0.0.0-20190409202823-959b441ac422
25+
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
26+
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
27+
golang.org/x/text v0.3.2 // indirect
28+
golang.org/x/tools v0.0.0-20190709211700-7b25e351ac0e // indirect
2029
)

go.sum

Lines changed: 41 additions & 0 deletions
Large diffs are not rendered by default.

internal/backend/basicstation/backend.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ func NewBackend(conf config.Config) (*Backend, error) {
112112
b.websocketWrap(b.handleRouterInfo, w, r)
113113
})
114114
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
115-
bsEventCounter("connect")
115+
connectCounter().Inc()
116116
b.websocketWrap(b.handleGateway, w, r)
117-
bsEventCounter("disconnect")
117+
disconnectCounter().Inc()
118118
})
119119

120120
// using net.Listen makes it easier to test as we can bind to ":0" and
@@ -199,7 +199,7 @@ func (b *Backend) SendDownlinkFrame(df gw.DownlinkFrame) error {
199199
var gatewayID lorawan.EUI64
200200
copy(gatewayID[:], df.TxInfo.GatewayId)
201201

202-
bsWebsocketSendCounter("dnmsg")
202+
websocketSendCounter("dnmsg").Inc()
203203
if err := b.sendToGateway(gatewayID, pl); err != nil {
204204
return errors.Wrap(err, "send to gateway error")
205205
}
@@ -218,7 +218,7 @@ func (b *Backend) ApplyConfiguration(gwConfig gw.GatewayConfiguration) error {
218218
var gatewayID lorawan.EUI64
219219
copy(gatewayID[:], gwConfig.GatewayId)
220220

221-
bsWebsocketSendCounter("router_config")
221+
websocketSendCounter("router_config").Inc()
222222
if err := b.sendToGateway(gatewayID, rc); err != nil {
223223
return errors.Wrap(err, "send router config to gateway error")
224224
}
@@ -235,7 +235,7 @@ func (b *Backend) Close() error {
235235
}
236236

237237
func (b *Backend) handleRouterInfo(r *http.Request, c *websocket.Conn) {
238-
bsWebsocketReceiveCounter("router_info")
238+
websocketReceiveCounter("router_info").Inc()
239239
var req structs.RouterInfoRequest
240240

241241
if err := c.ReadJSON(&req); err != nil {
@@ -331,7 +331,7 @@ func (b *Backend) handleGateway(r *http.Request, c *websocket.Conn) {
331331
continue
332332
}
333333

334-
bsWebsocketReceiveCounter(string(msgType))
334+
websocketReceiveCounter(string(msgType)).Inc()
335335

336336
// handle message-type
337337
switch msgType {
@@ -524,7 +524,7 @@ func (b *Backend) websocketWrap(handler func(*http.Request, *websocket.Conn), w
524524

525525
conn.SetReadDeadline(time.Now().Add(b.readTimeout))
526526
conn.SetPongHandler(func(string) error {
527-
bsWebsocketPingPongCounter("pong")
527+
websocketPingPongCounter("pong").Inc()
528528
conn.SetReadDeadline(time.Now().Add(b.readTimeout))
529529
return nil
530530
})
@@ -536,7 +536,7 @@ func (b *Backend) websocketWrap(handler func(*http.Request, *websocket.Conn), w
536536
for {
537537
select {
538538
case <-ticker.C:
539-
bsWebsocketPingPongCounter("ping")
539+
websocketPingPongCounter("ping").Inc()
540540
conn.SetWriteDeadline(time.Now().Add(b.writeTimeout))
541541
if err := conn.WriteMessage(websocket.PingMessage, nil); err != nil {
542542
log.WithError(err).Error("backend/basicstation: send ping message error")

internal/backend/basicstation/metrics.go

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,52 @@ package basicstation
22

33
import (
44
"github.com/prometheus/client_golang/prometheus"
5-
6-
"github.com/brocaar/lora-gateway-bridge/internal/metrics"
5+
"github.com/prometheus/client_golang/prometheus/promauto"
76
)
87

98
var (
10-
bsEventCounter func(string)
11-
bsWebsocketSendCounter func(string)
12-
bsWebsocketReceiveCounter func(string)
13-
bsWebsocketPingPongCounter func(string)
9+
ppc = promauto.NewCounterVec(prometheus.CounterOpts{
10+
Name: "backend_basicstation_websocket_ping_pong_count",
11+
Help: "The number of WebSocket Ping/Pong requests sent and received (per event type).",
12+
}, []string{"type"})
13+
14+
wsr = promauto.NewCounterVec(prometheus.CounterOpts{
15+
Name: "backend_basicstation_websocket_received_count",
16+
Help: "The number of WebSocket messages received by the backend (per msgtype).",
17+
}, []string{"msgtype"})
18+
19+
wss = promauto.NewCounterVec(prometheus.CounterOpts{
20+
Name: "backend_basicstation_websocket_sent_count",
21+
Help: "The number of WebSocket messages sent by the backend (per msgtype).",
22+
}, []string{"msgtype"})
23+
24+
gwc = prometheus.NewCounter(prometheus.CounterOpts{
25+
Name: "backend_basicstation_gateway_connect_count",
26+
Help: "The number of gateway connections received by the backend.",
27+
})
28+
29+
gwd = prometheus.NewCounter(prometheus.CounterOpts{
30+
Name: "backend_basicstation_gateway_disconnect_count",
31+
Help: "The number of gateways that disconnected from the backend.",
32+
})
1433
)
1534

16-
func init() {
17-
ec := metrics.MustRegisterNewCounter(
18-
"backend_basicstation_event",
19-
"Per gateway event type counter.",
20-
[]string{"event"},
21-
)
22-
23-
wsc := metrics.MustRegisterNewCounter(
24-
"backend_basicstation_websocket_send",
25-
"Per message-type websocket write counter.",
26-
[]string{"msgtype"},
27-
)
28-
29-
wrc := metrics.MustRegisterNewCounter(
30-
"backend_basicstation_websocket_receive",
31-
"Per message-type websocket receive counter.",
32-
[]string{"msgtype"},
33-
)
34-
35-
ppc := metrics.MustRegisterNewCounter(
36-
"backend_basicstation_websocket_ping_pong",
37-
"Websocket Ping/Pong counter.",
38-
[]string{"type"},
39-
)
40-
41-
bsEventCounter = func(event string) {
42-
ec(prometheus.Labels{"event": event})
43-
}
44-
45-
bsWebsocketReceiveCounter = func(msgtype string) {
46-
wsc(prometheus.Labels{"msgtype": msgtype})
47-
}
48-
49-
bsWebsocketSendCounter = func(msgtype string) {
50-
wrc(prometheus.Labels{"msgtype": msgtype})
51-
}
52-
53-
bsWebsocketPingPongCounter = func(typ string) {
54-
ppc(prometheus.Labels{"type": typ})
55-
}
35+
func websocketPingPongCounter(typ string) prometheus.Counter {
36+
return ppc.With(prometheus.Labels{"type": typ})
37+
}
38+
39+
func websocketReceiveCounter(msgtype string) prometheus.Counter {
40+
return wsr.With(prometheus.Labels{"msgtype": msgtype})
41+
}
42+
43+
func websocketSendCounter(msgtype string) prometheus.Counter {
44+
return wss.With(prometheus.Labels{"msgtype": msgtype})
45+
}
46+
47+
func connectCounter() prometheus.Counter {
48+
return gwc
49+
}
50+
51+
func disconnectCounter() prometheus.Counter {
52+
return gwd
5653
}

internal/backend/semtechudp/backend.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ func (b *Backend) SendDownlinkFrame(frame gw.DownlinkFrame) error {
194194
// ApplyConfiguration applies the given configuration to the gateway
195195
// (packet-forwarder).
196196
func (b *Backend) ApplyConfiguration(config gw.GatewayConfiguration) error {
197-
eventCounter("configuration")
198-
199197
var gatewayID lorawan.EUI64
200198
copy(gatewayID[:], config.GatewayId)
201199

@@ -317,7 +315,6 @@ func (b *Backend) sendPackets() error {
317315
"protocol_version": p.data[0],
318316
}).Debug("backend/semtechudp: sending udp packet to gateway")
319317

320-
udpWriteCounter(pt.String())
321318
_, err = b.conn.WriteToUDP(p.data, p.addr)
322319
if err != nil {
323320
log.WithFields(log.Fields{
@@ -326,6 +323,8 @@ func (b *Backend) sendPackets() error {
326323
"protocol_version": p.data[0],
327324
}).WithError(err).Error("backend/semtechudp: write to udp error")
328325
}
326+
327+
udpWriteCounter(pt.String()).Inc()
329328
}
330329
return nil
331330
}
@@ -348,7 +347,7 @@ func (b *Backend) handlePacket(up udpPacket) error {
348347
"protocol_version": up.data[0],
349348
}).Debug("backend/semtechudp: received udp packet from gateway")
350349

351-
udpReadCounter(pt.String())
350+
udpReadCounter(pt.String()).Inc()
352351

353352
switch pt {
354353
case packets.PushData:

internal/backend/semtechudp/metrics.go

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,43 @@ package semtechudp
22

33
import (
44
"github.com/prometheus/client_golang/prometheus"
5-
6-
"github.com/brocaar/lora-gateway-bridge/internal/metrics"
5+
"github.com/prometheus/client_golang/prometheus/promauto"
76
)
87

98
var (
10-
eventCounter func(string)
11-
udpWriteCounter func(string)
12-
udpReadCounter func(string)
9+
uwc = promauto.NewCounterVec(prometheus.CounterOpts{
10+
Name: "backend_semtechudp_udp_sent_count",
11+
Help: "The number of UDP packets sent by the backend (per packet_type).",
12+
}, []string{"packet_type"})
13+
14+
urc = promauto.NewCounterVec(prometheus.CounterOpts{
15+
Name: "backend_semtechudp_udp_received_count",
16+
Help: "The number of UDP packets received by the backend (per packet_type).",
17+
}, []string{"packet_type"})
18+
19+
gwc = promauto.NewCounter(prometheus.CounterOpts{
20+
Name: "backend_semtechudp_gateway_connect_count",
21+
Help: "The number of gateway connections received by the backend.",
22+
})
23+
24+
gwd = promauto.NewCounter(prometheus.CounterOpts{
25+
Name: "backend_semtechudp_gateway_diconnect_count",
26+
Help: "The number of gateways that disconnected from the backend.",
27+
})
1328
)
1429

15-
func init() {
16-
ec := metrics.MustRegisterNewCounter(
17-
"backend_semtechudp_event",
18-
"Per gateway event type counter.",
19-
[]string{"event"},
20-
)
21-
22-
uwc := metrics.MustRegisterNewCounter(
23-
"backend_semtechudp_udp_write",
24-
"UDP packets written by packet type.",
25-
[]string{"packet_type"},
26-
)
27-
28-
urc := metrics.MustRegisterNewCounter(
29-
"backend_semtechudp_udp_read",
30-
"UDP packets read by packet type.",
31-
[]string{"packet_type"},
32-
)
33-
34-
eventCounter = func(event string) {
35-
ec(prometheus.Labels{"event": event})
36-
}
37-
38-
udpWriteCounter = func(pt string) {
39-
uwc(prometheus.Labels{"packet_type": pt})
40-
}
41-
42-
udpReadCounter = func(pt string) {
43-
urc(prometheus.Labels{"packet_type": pt})
44-
}
30+
func udpWriteCounter(pt string) prometheus.Counter {
31+
return uwc.With(prometheus.Labels{"packet_type": pt})
32+
}
33+
34+
func udpReadCounter(pt string) prometheus.Counter {
35+
return urc.With(prometheus.Labels{"packet_type": pt})
36+
}
37+
38+
func connectCounter() prometheus.Counter {
39+
return gwc
40+
}
41+
42+
func disconnectCounter() prometheus.Counter {
43+
return gwd
4544
}

0 commit comments

Comments
 (0)