1 2
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 2
  labels['account'] = account.accountId
6 2
  labels['asset'] = account.accountInfo.assetCode
7 2
  labels['scale'] = account.accountInfo.assetScale
8 2
  return labels
9
}
10

11 2
export class AccountCounter extends Prometheus.Counter {
12
  constructor (configuration: Prometheus.CounterConfiguration) {
13 2
    configuration.labelNames = (configuration.labelNames || [])
14 2
    configuration.labelNames.push('account', 'asset', 'scale')
15 2
    super(configuration)
16
  }
17
  increment (account: { accountId: string, accountInfo: AccountInfo }, labels: Prometheus.labelValues, value?: number) {
18 2
    return this.inc(mergeAccountLabels(account, labels), value)
19
  }
20
}
21

22 2
export class AccountGauge extends Prometheus.Gauge {
23
  constructor (configuration: Prometheus.GaugeConfiguration) {
24 2
    configuration.labelNames = (configuration.labelNames || [])
25 2
    configuration.labelNames.push('account', 'asset', 'scale')
26 2
    super(configuration)
27
  }
28
  setValue (account: { accountId: string, accountInfo: AccountInfo }, labels: Prometheus.labelValues, value: number) {
29 2
    return this.set(mergeAccountLabels(account, labels), value)
30
  }
31
}
32

33 2
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 2
    this.registry = new (Prometheus.Registry)()
47

48 2
    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 2
    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 2
    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 2
    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 2
    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 2
    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 2
    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 2
    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 2
    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 2
    return this.registry.getMetricsAsJSON()
111
  }
112

113
  getRegistry () {
114 0
    return this.registry
115
  }
116
}

Read our documentation on viewing source code .

Loading