-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexporter.py
239 lines (177 loc) · 6.58 KB
/
exporter.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import os
from autobahn.twisted.component import Component, run
from autobahn.twisted.wamp import Session
from prometheus_client import Gauge
from prometheus_client.twisted import MetricsResource
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from twisted.web.resource import Resource
from twisted.web.server import Site
g_active_sessions = Gauge('active_session_count', 'Number of sessions currently active', ['router_url', 'realm'])
g_registration_callees = Gauge('active_callee_count', 'Number of sessions currently attached to the registration',
['router_url', 'realm', 'uri'])
g_subscription_subscribers = Gauge('active_subscription_count',
'Number of sessions currently subscribed to the subscription',
['router_url', 'realm', 'uri'])
settings = {
"wamp_url": os.environ.get('WAMP_URL'),
"wamp_realm": os.environ.get('WAMP_REALM'),
"wamp_principal": os.environ.get('WAMP_PRINCIPAL'),
"wamp_ticket": os.environ.get('WAMP_TICKET'),
}
component = Component(
transports=settings.get('wamp_url'),
realm=settings.get('wamp_realm'),
authentication={
"ticket": {
"authid": settings.get('wamp_principal'),
"ticket": settings.get('wamp_ticket'),
}
}
)
meta = {
"session": None,
"session_details": None,
"registrations": {},
"subscriptions": {},
}
def update_session_count():
session_count = yield meta["session"].call("wamp.session.count")
g_active_sessions.labels(settings.get('wamp_url'), settings.get('wamp_realm')).set(session_count)
def init_registration_callee_count():
rpc_list = yield meta['session'].call('wamp.registration.list')
for id in rpc_list['exact']:
yield from create_registration_callee(id)
def create_registration_callee(id):
info = yield meta['session'].call('wamp.registration.get', id)
meta['registrations'][id] = info['uri']
yield from update_registration_callee_count(id)
def remove_registration_callee(id):
uri = meta['registrations'].get(id, None)
if uri:
del meta['registrations'][id]
try:
g_registration_callees.remove(
settings.get('wamp_url'),
settings.get('wamp_realm'),
uri
)
except:
pass
def update_registration_callee_count(id):
uri = meta['registrations'].get(id)
if not uri:
return # registration right after create, count will be updated by create
try:
callees = yield meta['session'].call('wamp.registration.count_callees', id)
g_registration_callees.labels(
settings.get('wamp_url'),
settings.get('wamp_realm'),
uri
).set(callees)
except:
remove_registration_callee(id)
def init_subscription_subscriber_count():
rpc_list = yield meta['session'].call('wamp.subscription.list')
for id in rpc_list['exact']:
yield from create_subscription(id)
def create_subscription(id):
info = yield meta['session'].call('wamp.subscription.get', id)
meta['subscriptions'][id] = info['uri']
yield from update_subscription_subscriber_count(id)
def remove_subscription(id):
uri = meta['subscriptions'].get(id, None)
if uri:
del meta['subscriptions'][id]
try:
g_subscription_subscribers.remove(
settings.get('wamp_url'),
settings.get('wamp_realm'),
uri
)
except:
pass
def update_subscription_subscriber_count(id):
uri = meta['subscriptions'].get(id)
if not uri:
return # subscription right after create, count will be updated by create
try:
subscribers = yield meta['session'].call('wamp.subscription.count_subscribers', id)
g_subscription_subscribers.labels(
settings.get('wamp_url'),
settings.get('wamp_realm'),
uri
).set(subscribers)
except:
remove_subscription(id)
"""
lifecycle callbacks
"""
@component.on_join
@inlineCallbacks
def joined(session: Session, details):
meta['session_details'] = details
meta["session"] = session
yield from update_session_count()
yield from init_registration_callee_count()
yield from init_subscription_subscriber_count()
@component.subscribe('wamp.session.on_join')
@inlineCallbacks
def on_join(*args, **kwargs):
yield from update_session_count()
@component.subscribe('wamp.session.on_leave')
@inlineCallbacks
def on_leave(*args, **kwargs):
yield from update_session_count()
@component.on_leave
# @inlineCallbacks
def left(session, details):
print("session closed")
@component.on_connect
# @inlineCallbacks
def connected(session, details):
print("connected")
"""
RPC
"""
@component.subscribe('wamp.registration.on_create')
@inlineCallbacks
def on_registration_create(session_id, info, **kwargs):
yield from create_registration_callee(info.get('id'))
@component.subscribe('wamp.registration.on_register')
@inlineCallbacks
def on_registration_register(session_id, registration_id, **kwargs):
yield from update_registration_callee_count(registration_id)
@component.subscribe('wamp.registration.on_unregister')
@inlineCallbacks
def on_registration_unregister(session_id, registration_id, **kwargs):
yield from update_registration_callee_count(registration_id)
@component.subscribe('wamp.registration.on_delete')
@inlineCallbacks
def on_registration_delete(session_id, registration_id, **kwargs):
yield from update_registration_callee_count(registration_id)
"""
Pub/Sub
"""
@component.subscribe('wamp.subscription.on_create')
@inlineCallbacks
def on_subscription_create(session_id, info, **kwargs):
yield from create_subscription(info.get('id'))
@component.subscribe('wamp.subscription.on_subscribe')
@inlineCallbacks
def on_subscription_subscribe(session_id, subscription_id, **kwargs):
yield from update_subscription_subscriber_count(subscription_id)
@component.subscribe('wamp.subscription.on_unsubscribe')
@inlineCallbacks
def on_subscription_unsubscribe(session_id, subscription_id, **kwargs):
yield from update_subscription_subscriber_count(subscription_id)
@component.subscribe('wamp.subscription.on_delete')
@inlineCallbacks
def on_subscription_delete(session_id, subscription_id, **kwargs):
yield from update_subscription_subscriber_count(subscription_id)
if __name__ == "__main__":
root = Resource()
root.putChild(b'metrics', MetricsResource())
factory = Site(root)
reactor.listenTCP(9123, factory)
run([component])