-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmetrics.ts
More file actions
188 lines (179 loc) · 5.62 KB
/
metrics.ts
File metadata and controls
188 lines (179 loc) · 5.62 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Counter, Gauge, Histogram, Summary } from 'prom-client'
import { NetworkId, NetworkNames } from '../networks/types.js'
import type { PortalNetworkMetrics } from '../client/types.js'
import type { Metric } from 'prom-client'
export enum MetricType {
Counter,
Gauge,
Histogram,
Summary,
}
const metricTypes = {
[MetricType.Gauge]: Gauge,
[MetricType.Counter]: Counter,
[MetricType.Histogram]: Histogram,
[MetricType.Summary]: Summary,
}
interface MetricParams {
metric: MetricType
name: string
help: string
}
const createMetric = ({ metric, name, help }: MetricParams) => {
return (networks: NetworkId[]) => {
const metrics: Record<string, Metric<NetworkId>> = {}
for (const network of networks) {
const metricName = NetworkNames[network] + '_' + name
metrics[metricName] = new metricTypes[metric]({
name: 'ultralight_' + metricName,
help,
})
}
return metrics
}
}
const createMetrics = (metrics: MetricParams[], networks: NetworkId[]) => {
let m: Record<string, Metric<NetworkId>> = {}
const metricsFunctions = metrics.map(createMetric)
for (const metricFunction of metricsFunctions) {
m = { ...m, ...metricFunction(networks) }
}
return m as Record<keyof PortalNetworkMetrics, Metric<NetworkId>>
}
const ultralightMetrics = [
{
name: 'peers',
metric: MetricType.Gauge,
help: 'how many peers are in the routing table',
},
{
name: 'dbSize',
metric: MetricType.Gauge,
help: 'how many MBs are currently stored in the db',
},
{
name: 'talkReqSent',
metric: MetricType.Counter,
help: 'how many talk requests have been sent',
},
{
name: 'talkReqReceived',
metric: MetricType.Counter,
help: 'how many talk requests have been received',
},
{
name: 'utpPacketsSent',
metric: MetricType.Counter,
help: 'how many UTP packets have been sent',
},
{
name: 'utpPacketsReceived',
metric: MetricType.Counter,
help: 'how many UTP packets have been received',
},
{
name: 'utpStreamsTotal',
metric: MetricType.Gauge,
help: 'how many total UTP streams were opened',
},
{
name: 'utpWriteStreamsOpened',
metric: MetricType.Gauge,
help: 'how many UTP write streams were opened',
},
{
name: 'utpWriteStreamsCompleted',
metric: MetricType.Gauge,
help: 'how many UTP write streams were completed',
},
{
name: 'utpReadStreamsOpened',
metric: MetricType.Gauge,
help: 'how many UTP read streams were opened',
},
{
name: 'utpReadStreamsCompleted',
metric: MetricType.Gauge,
help: 'how many UTP read streams were completed',
},
]
export const setupMetrics = (
networks: NetworkId[] = [NetworkId.HistoryNetwork],
): PortalNetworkMetrics => {
const metrics = createMetrics(ultralightMetrics, [...networks])
return {
...metrics,
totalContentLookups: new Gauge<string>({
name: 'ultralight_total_content_lookups',
help: 'total number of content lookups initiated',
}),
successfulContentLookups: new Counter({
name: 'ultralight_successful_content_lookups',
help: 'how many content lookups successfully returned content',
}),
failedContentLookups: new Counter({
name: 'ultralight_failed_content_lookups',
help: 'how many content lookups failed to return content',
}),
offerMessagesSent: new Counter({
name: 'ultralight_offer_messages_sent',
help: 'how many offer messages have been sent',
}),
offerMessagesReceived: new Counter({
name: 'ultralight_offer_messages_received',
help: 'how many offer messages have been received',
}),
acceptMessagesSent: new Counter({
name: 'ultralight_accept_messages_sent',
help: 'how many accept messages have been sent',
}),
acceptMessagesReceived: new Counter({
name: 'ultralight_accept_messages_received',
help: 'how many accept messages have been received',
}),
findContentMessagesSent: new Counter({
name: 'ultralight_findContent_messages_sent',
help: 'how many findContent messages have been sent',
}),
findContentMessagesReceived: new Counter({
name: 'ultralight_findContent_messages_received',
help: 'how many findContent messages have been received',
}),
contentMessagesSent: new Counter({
name: 'ultralight_content_messages_sent',
help: 'how many content messages have been sent',
}),
contentMessagesReceived: new Counter({
name: 'ultralight_content_messages_received',
help: 'how many content messages have been received',
}),
findNodesMessagesSent: new Counter({
name: 'ultralight_findNodes_messages_sent',
help: 'how many findNodes messages have been sent',
}),
findNodesMessagesReceived: new Counter({
name: 'ultralight_findNodes_messages_received',
help: 'how many findNodes messages have been received',
}),
nodesMessagesSent: new Counter({
name: 'ultralight_nodes_messages_sent',
help: 'how many nodes messages have been sent',
}),
nodesMessagesReceived: new Counter({
name: 'ultralight_nodes_messages_received',
help: 'how many nodes messages have been received',
}),
totalBytesReceived: new Counter({
name: 'ultralight_total_bytes_received',
help: 'how many bytes have been received in Portal Network message payloads',
}),
totalBytesSent: new Counter({
name: 'ultralight_total_bytes_sent',
help: 'how many bytes have been sent in Portal Network message payloads',
}),
currentDBSize: new Gauge({
name: 'ultralight_db_size',
help: 'how many MBs are currently stored in the db',
}),
}
}