Skip to content

Commit 3c934d3

Browse files
committed
Update Prometheus metrics. Bump (test) version.
1 parent f511d1c commit 3c934d3

File tree

9 files changed

+169
-121
lines changed

9 files changed

+169
-121
lines changed

docs/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ pygmentsUseClasses = true
5454
weight = 7
5555

5656
[params]
57-
version = "3.0.0-test.2"
57+
version = "3.0.0-test.3"
5858

docs/content/overview/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ now used for selecting and configuring the packet-forwarder backends.
9797
The MQTT integration configuration has moved to the new `[integration...]`
9898
section. This allows for adding new integrations in the future besides MQTT.
9999

100+
#### Prometheus metrics
101+
102+
The Prometheus metrics have been updated / cleaned up.
103+
100104
## v2.7.1
101105

102106
### Bugfixes

internal/backend/basicstation/backend.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +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")
115116
b.websocketWrap(b.handleGateway, w, r)
117+
bsEventCounter("disconnect")
116118
})
117119

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

202+
bsWebsocketSendCounter("dnmsg")
200203
if err := b.sendToGateway(gatewayID, pl); err != nil {
201204
return errors.Wrap(err, "send to gateway error")
202205
}
@@ -215,6 +218,7 @@ func (b *Backend) ApplyConfiguration(gwConfig gw.GatewayConfiguration) error {
215218
var gatewayID lorawan.EUI64
216219
copy(gatewayID[:], gwConfig.GatewayId)
217220

221+
bsWebsocketSendCounter("router_config")
218222
if err := b.sendToGateway(gatewayID, rc); err != nil {
219223
return errors.Wrap(err, "send router config to gateway error")
220224
}
@@ -231,6 +235,7 @@ func (b *Backend) Close() error {
231235
}
232236

233237
func (b *Backend) handleRouterInfo(r *http.Request, c *websocket.Conn) {
238+
bsWebsocketReceiveCounter("router_info")
234239
var req structs.RouterInfoRequest
235240

236241
if err := c.ReadJSON(&req); err != nil {
@@ -326,6 +331,8 @@ func (b *Backend) handleGateway(r *http.Request, c *websocket.Conn) {
326331
continue
327332
}
328333

334+
bsWebsocketReceiveCounter(string(msgType))
335+
329336
// handle message-type
330337
switch msgType {
331338
case structs.VersionMessage:
@@ -517,6 +524,7 @@ func (b *Backend) websocketWrap(handler func(*http.Request, *websocket.Conn), w
517524

518525
conn.SetReadDeadline(time.Now().Add(b.readTimeout))
519526
conn.SetPongHandler(func(string) error {
527+
bsWebsocketPingPongCounter("pong")
520528
conn.SetReadDeadline(time.Now().Add(b.readTimeout))
521529
return nil
522530
})
@@ -528,6 +536,7 @@ func (b *Backend) websocketWrap(handler func(*http.Request, *websocket.Conn), w
528536
for {
529537
select {
530538
case <-ticker.C:
539+
bsWebsocketPingPongCounter("ping")
531540
conn.SetWriteDeadline(time.Now().Add(b.writeTimeout))
532541
if err := conn.WriteMessage(websocket.PingMessage, nil); err != nil {
533542
log.WithError(err).Error("backend/basicstation: send ping message error")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package basicstation
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
6+
"github.com/brocaar/lora-gateway-bridge/internal/metrics"
7+
)
8+
9+
var (
10+
bsEventCounter func(string)
11+
bsWebsocketSendCounter func(string)
12+
bsWebsocketReceiveCounter func(string)
13+
bsWebsocketPingPongCounter func(string)
14+
)
15+
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+
}
56+
}

internal/backend/semtechudp/backend.go

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -192,25 +192,25 @@ func (b *Backend) SendDownlinkFrame(frame gw.DownlinkFrame) error {
192192
// ApplyConfiguration applies the given configuration to the gateway
193193
// (packet-forwarder).
194194
func (b *Backend) ApplyConfiguration(config gw.GatewayConfiguration) error {
195-
return gatewayConfigHandleTimer(func() error {
196-
var gatewayID lorawan.EUI64
197-
copy(gatewayID[:], config.GatewayId)
198-
199-
b.Lock()
200-
var pfConfig *pfConfiguration
201-
for i := range b.configurations {
202-
if b.configurations[i].gatewayID == gatewayID {
203-
pfConfig = &b.configurations[i]
204-
}
205-
}
206-
b.Unlock()
195+
eventCounter("configuration")
196+
197+
var gatewayID lorawan.EUI64
198+
copy(gatewayID[:], config.GatewayId)
207199

208-
if pfConfig == nil {
209-
return errGatewayDoesNotExist
200+
b.Lock()
201+
var pfConfig *pfConfiguration
202+
for i := range b.configurations {
203+
if b.configurations[i].gatewayID == gatewayID {
204+
pfConfig = &b.configurations[i]
210205
}
206+
}
207+
b.Unlock()
211208

212-
return b.applyConfiguration(*pfConfig, config)
213-
})
209+
if pfConfig == nil {
210+
return errGatewayDoesNotExist
211+
}
212+
213+
return b.applyConfiguration(*pfConfig, config)
214214
}
215215

216216
func (b *Backend) applyConfiguration(pfConfig pfConfiguration, config gw.GatewayConfiguration) error {
@@ -315,11 +315,8 @@ func (b *Backend) sendPackets() error {
315315
"protocol_version": p.data[0],
316316
}).Debug("backend/semtechudp: sending udp packet to gateway")
317317

318-
err = gatewayWriteUDPTimer(pt.String(), func() error {
319-
_, err := b.conn.WriteToUDP(p.data, p.addr)
320-
return err
321-
})
322-
318+
udpWriteCounter(pt.String())
319+
_, err = b.conn.WriteToUDP(p.data, p.addr)
323320
if err != nil {
324321
log.WithFields(log.Fields{
325322
"addr": p.addr,
@@ -349,18 +346,18 @@ func (b *Backend) handlePacket(up udpPacket) error {
349346
"protocol_version": up.data[0],
350347
}).Debug("backend/semtechudp: received udp packet from gateway")
351348

352-
return gatewayHandleTimer(pt.String(), func() error {
353-
switch pt {
354-
case packets.PushData:
355-
return b.handlePushData(up)
356-
case packets.PullData:
357-
return b.handlePullData(up)
358-
case packets.TXACK:
359-
return b.handleTXACK(up)
360-
default:
361-
return fmt.Errorf("backend/semtechudp: unknown packet type: %s", pt)
362-
}
363-
})
349+
udpReadCounter(pt.String())
350+
351+
switch pt {
352+
case packets.PushData:
353+
return b.handlePushData(up)
354+
case packets.PullData:
355+
return b.handlePullData(up)
356+
case packets.TXACK:
357+
return b.handleTXACK(up)
358+
default:
359+
return fmt.Errorf("backend/semtechudp: unknown packet type: %s", pt)
360+
}
364361
}
365362

366363
func (b *Backend) handlePullData(up udpPacket) error {

internal/backend/semtechudp/metrics.go

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,39 @@ import (
77
)
88

99
var (
10-
gatewayEventCounter func(string)
11-
gatewayWriteUDPTimer func(string, func() error) error
12-
gatewayHandleTimer func(string, func() error) error
13-
gatewayConfigHandleTimer func(func() error) error
10+
eventCounter func(string)
11+
udpWriteCounter func(string)
12+
udpReadCounter func(string)
1413
)
1514

1615
func init() {
1716
ec := metrics.MustRegisterNewCounter(
18-
"gateway_event",
19-
"Per event type counter.",
17+
"backend_semtechudp_event",
18+
"Per gateway event type counter.",
2019
[]string{"event"},
2120
)
2221

23-
wt := metrics.MustRegisterNewTimerWithError(
24-
"gateway_udp_write",
25-
"Per message-type UDP write duration tracking.",
26-
[]string{"type"},
22+
uwc := metrics.MustRegisterNewCounter(
23+
"backend_semtechudp_udp_write",
24+
"UDP packets written by packet type.",
25+
[]string{"packet_type"},
2726
)
2827

29-
ht := metrics.MustRegisterNewTimerWithError(
30-
"gateway_udp_received_handle",
31-
"Per message-type received UDP handling duration tracking.",
32-
[]string{"type"},
28+
urc := metrics.MustRegisterNewCounter(
29+
"backend_semtechudp_udp_read",
30+
"UDP packets read by packet type.",
31+
[]string{"packet_type"},
3332
)
3433

35-
ch := metrics.MustRegisterNewTimerWithError(
36-
"gateway_config_handle",
37-
"Tracks the duration of configuration handling.",
38-
[]string{},
39-
)
40-
41-
gatewayEventCounter = func(event string) {
34+
eventCounter = func(event string) {
4235
ec(prometheus.Labels{"event": event})
4336
}
4437

45-
gatewayWriteUDPTimer = func(mType string, f func() error) error {
46-
return wt(prometheus.Labels{"type": mType}, f)
47-
}
48-
49-
gatewayHandleTimer = func(mType string, f func() error) error {
50-
return ht(prometheus.Labels{"type": mType}, f)
38+
udpWriteCounter = func(pt string) {
39+
uwc(prometheus.Labels{"packet_type": pt})
5140
}
5241

53-
gatewayConfigHandleTimer = func(f func() error) error {
54-
return ch(prometheus.Labels{}, f)
42+
udpReadCounter = func(pt string) {
43+
urc(prometheus.Labels{"packet_type": pt})
5544
}
5645
}

internal/backend/semtechudp/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (c *gateways) set(gatewayID lorawan.EUI64, gw gateway) error {
5454

5555
_, ok := c.gateways[gatewayID]
5656
if !ok {
57-
gatewayEventCounter("register_gateway")
57+
eventCounter("connect")
5858
c.connectChan <- gatewayID
5959
}
6060
c.gateways[gatewayID] = gw
@@ -68,7 +68,7 @@ func (c *gateways) cleanup() error {
6868

6969
for gatewayID := range c.gateways {
7070
if c.gateways[gatewayID].lastSeen.Before(time.Now().Add(gatewayCleanupDuration)) {
71-
gatewayEventCounter("unregister_gateway")
71+
eventCounter("disconnect")
7272
c.disconnectChan <- gatewayID
7373
delete(c.gateways, gatewayID)
7474
}

0 commit comments

Comments
 (0)