This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
metrics.go
93 lines (82 loc) · 2.19 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package exchange
import (
"github.com/TheThingsNetwork/api/protocol"
"github.com/TheThingsNetwork/api/protocol/lorawan"
"github.com/prometheus/client_golang/prometheus"
)
var info = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "ttn",
Subsystem: "bridge",
Name: "info",
Help: "Information about the TTN environment.",
}, []string{
"build_date", "git_commit", "id", "version",
},
)
var connectedGateways = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: "ttn",
Subsystem: "bridge",
Name: "connected_gateways",
Help: "Number of connected gateways.",
},
)
var handledCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "ttn",
Subsystem: "bridge",
Name: "messages_handled_total",
Help: "Total number of messages handled.",
}, []string{"message_type"},
)
func mTypeToString(mType lorawan.MType) string {
switch mType {
case lorawan.MType_JOIN_REQUEST:
return "JoinRequest"
case lorawan.MType_JOIN_ACCEPT:
return "JoinAccept"
case lorawan.MType_UNCONFIRMED_UP:
return "UnconfirmedUp"
case lorawan.MType_UNCONFIRMED_DOWN:
return "UnconfirmedDown"
case lorawan.MType_CONFIRMED_UP:
return "ConfirmedUp"
case lorawan.MType_CONFIRMED_DOWN:
return "ConfirmedDown"
case 6:
return "RejoinRequest"
case 7:
return "Proprietary"
default:
return "Unknown"
}
}
func messageType(msg *protocol.Message) string {
if msg := msg.GetLoRaWAN(); msg != nil {
return mTypeToString(msg.GetMType())
}
return "Unknown"
}
type message interface {
UnmarshalPayload() error
GetPayload() []byte
GetMessage() *protocol.Message
}
func registerHandled(msg message) {
msg.UnmarshalPayload()
handledCounter.WithLabelValues(messageType(msg.GetMessage())).Inc()
}
func registerStatus() {
handledCounter.WithLabelValues("GatewayStatus").Inc()
}
func init() {
prometheus.MustRegister(info)
prometheus.MustRegister(connectedGateways)
prometheus.MustRegister(handledCounter)
for mType := lorawan.MType(0); mType < 8; mType++ {
handledCounter.WithLabelValues(mTypeToString(mType)).Add(0)
}
}