1 | import * as Prometheus from 'prom-client'
|
2 | import { AccountInfo } from '../types/accounts'
|
3 |
|
4 | function mergeAccountLabels (account: { accountId: string, accountInfo: AccountInfo }, labels: Prometheus.labelValues): Prometheus.labelValues {
|
5 | labels['account'] = account.accountId
|
6 | labels['asset'] = account.accountInfo.assetCode
|
7 | labels['scale'] = account.accountInfo.assetScale
|
8 | return labels
|
9 | }
|
10 |
|
11 | export class AccountCounter extends Prometheus.Counter {
|
12 | constructor (configuration: Prometheus.CounterConfiguration) {
|
13 | configuration.labelNames = (configuration.labelNames || [])
|
14 | configuration.labelNames.push('account', 'asset', 'scale')
|
15 | super(configuration)
|
16 | }
|
17 | increment (account: { accountId: string, accountInfo: AccountInfo }, labels: Prometheus.labelValues, value?: number) {
|
18 | return this.inc(mergeAccountLabels(account, labels), value)
|
19 | }
|
20 | }
|
21 |
|
22 | export class AccountGauge extends Prometheus.Gauge {
|
23 | constructor (configuration: Prometheus.GaugeConfiguration) {
|
24 | configuration.labelNames = (configuration.labelNames || [])
|
25 | configuration.labelNames.push('account', 'asset', 'scale')
|
26 | super(configuration)
|
27 | }
|
28 | setValue (account: { accountId: string, accountInfo: AccountInfo }, labels: Prometheus.labelValues, value: number) {
|
29 | return this.set(mergeAccountLabels(account, labels), value)
|
30 | }
|
31 | }
|
32 |
|
33 | export default class Stats {
|
34 | public incomingDataPackets: AccountCounter
|
35 | public incomingDataPacketValue: AccountCounter
|
36 | public outgoingDataPackets: AccountCounter
|
37 | public outgoingDataPacketValue: AccountCounter
|
38 | public incomingMoney: AccountGauge
|
39 | public outgoingMoney: AccountGauge
|
40 | public rateLimitedPackets: AccountCounter
|
41 | public rateLimitedMoney: AccountCounter
|
42 | public balance: AccountGauge
|
43 | private registry: Prometheus.Registry
|
44 |
|
45 | constructor () {
|
46 | this.registry = new (Prometheus.Registry)()
|
47 |
|
48 | this.incomingDataPackets = new AccountCounter({
|
49 | name: 'ilp_connector_incoming_ilp_packets',
|
50 | help: 'Total number of incoming ILP packets',
|
51 | labelNames: [ 'result', 'code'],
|
52 | registers: [this.registry]
|
53 | })
|
54 |
|
55 | this.incomingDataPacketValue = new AccountCounter({
|
56 | name: 'ilp_connector_incoming_ilp_packet_value',
|
57 | help: 'Total value of incoming ILP packets',
|
58 | labelNames: [ 'result', 'code'],
|
59 | registers: [this.registry]
|
60 | })
|
61 |
|
62 | this.outgoingDataPackets = new AccountCounter({
|
63 | name: 'ilp_connector_outgoing_ilp_packets',
|
64 | help: 'Total number of outgoing ILP packets',
|
65 | labelNames: [ 'result', 'code' ],
|
66 | registers: [this.registry]
|
67 | })
|
68 |
|
69 | this.outgoingDataPacketValue = new AccountCounter({
|
70 | name: 'ilp_connector_outgoing_ilp_packet_value',
|
71 | help: 'Total value of outgoing ILP packets',
|
72 | labelNames: [ 'result', 'code' ],
|
73 | registers: [this.registry]
|
74 | })
|
75 |
|
76 | this.incomingMoney = new AccountGauge({
|
77 | name: 'ilp_connector_incoming_money',
|
78 | help: 'Total of incoming money',
|
79 | labelNames: [ 'result' ],
|
80 | registers: [this.registry]
|
81 | })
|
82 |
|
83 | this.outgoingMoney = new AccountGauge({
|
84 | name: 'ilp_connector_outgoing_money',
|
85 | help: 'Total of outgoing money',
|
86 | labelNames: [ 'result' ],
|
87 | registers: [this.registry]
|
88 | })
|
89 |
|
90 | this.rateLimitedPackets = new AccountCounter({
|
91 | name: 'ilp_connector_rate_limited_ilp_packets',
|
92 | help: 'Total of rate limited ILP packets',
|
93 | registers: [this.registry]
|
94 | })
|
95 |
|
96 | this.rateLimitedMoney = new AccountCounter({
|
97 | name: 'ilp_connector_rate_limited_money',
|
98 | help: 'Total of rate limited money requests',
|
99 | registers: [this.registry]
|
100 | })
|
101 |
|
102 | this.balance = new AccountGauge({
|
103 | name: 'ilp_connector_balance',
|
104 | help: 'Balances on peer account',
|
105 | registers: [this.registry]
|
106 | })
|
107 | }
|
108 |
|
109 | getStatus () {
|
110 | return this.registry.getMetricsAsJSON()
|
111 | }
|
112 |
|
113 | getRegistry () {
|
114 | return this.registry
|
115 | }
|
116 | }
|