-
Notifications
You must be signed in to change notification settings - Fork 66
/
telemetry.py
194 lines (168 loc) · 5.45 KB
/
telemetry.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
# coding=utf-8
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import asyncio
import sys
import uamqp
from uuid import uuid4
from knack.log import get_logger
from typing import List
from azext_iot.constants import VERSION, USER_AGENT
from azext_iot.monitor.models.target import Target
from azext_iot.monitor.utility import get_loop
logger = get_logger(__name__)
DEBUG = False
def start_single_monitor(
target: Target,
enqueued_time_utc,
on_start_string: str,
on_message_received,
timeout=0,
):
"""
:param on_message_received:
A callback to process messages as they arrive from the service.
It takes a single argument, a ~uamqp.message.Message object.
"""
return start_multiple_monitors(
targets=[target],
enqueued_time_utc=enqueued_time_utc,
on_start_string=on_start_string,
on_message_received=on_message_received,
timeout=timeout,
)
def start_multiple_monitors(
targets: List[Target],
on_start_string: str,
enqueued_time_utc,
on_message_received,
timeout=0,
):
"""
:param on_message_received:
A callback to process messages as they arrive from the service.
It takes a single argument, a ~uamqp.message.Message object.
"""
coroutines = [
_initiate_event_monitor(
target=target,
enqueued_time_utc=enqueued_time_utc,
on_message_received=on_message_received,
timeout=timeout,
)
for target in targets
]
loop = get_loop()
future = asyncio.gather(*coroutines, return_exceptions=True)
result = None
try:
print(on_start_string, flush=True)
future.add_done_callback(lambda _: _stop_and_suppress_eloop(loop))
result = loop.run_until_complete(future)
except KeyboardInterrupt:
print("Stopping event monitor...", flush=True)
try:
# TODO: remove when deprecating
# pylint: disable=no-member
tasks = asyncio.all_tasks(loop)
for t in tasks: # pylint: disable=no-member
t.cancel()
loop.run_forever()
except RuntimeError:
pass # no running loop anymore
finally:
if result:
errors = result[0]
if errors and errors[0]:
logger.debug(errors)
raise RuntimeError(errors[0])
async def _initiate_event_monitor(
target: Target, enqueued_time_utc, on_message_received, timeout=0
):
if not target.partitions:
logger.warning("No Event Hub partitions found to listen on.")
return
coroutines = []
async with uamqp.ConnectionAsync(
target.hostname,
sasl=target.auth,
debug=DEBUG,
container_id=_get_container_id(),
properties=_get_conn_props(),
) as conn:
for p in target.partitions:
coroutines.append(
_monitor_events(
target=target,
connection=conn,
partition=p,
enqueued_time_utc=enqueued_time_utc,
on_message_received=on_message_received,
timeout=timeout,
)
)
return await asyncio.gather(*coroutines, return_exceptions=True)
async def _monitor_events(
target: Target,
connection,
partition,
enqueued_time_utc,
on_message_received,
timeout=0,
):
source = uamqp.address.Source(
"amqps://{}/{}/ConsumerGroups/{}/Partitions/{}".format(
target.hostname, target.path, target.consumer_group, partition
)
)
source.set_filter(
bytes(
"amqp.annotation.x-opt-enqueuedtimeutc > " + str(enqueued_time_utc), "utf8"
)
)
exp_cancelled = False
receive_client = uamqp.ReceiveClientAsync(
source,
auth=target.auth,
timeout=timeout,
prefetch=0,
client_name=_get_container_id(),
debug=DEBUG,
)
try:
if connection:
await receive_client.open_async(connection=connection)
async for msg in receive_client.receive_messages_iter_async():
on_message_received(msg)
except asyncio.CancelledError:
exp_cancelled = True
await receive_client.close_async()
except uamqp.errors.LinkDetach as ld:
if isinstance(ld.description, bytes):
ld.description = str(ld.description, "utf8")
raise RuntimeError(ld.description)
except KeyboardInterrupt:
logger.info("Keyboard interrupt, closing monitor on partition %s", partition)
exp_cancelled = True
await receive_client.close_async()
raise
finally:
if not exp_cancelled:
await receive_client.close_async()
logger.info("Closed monitor on partition %s", partition)
def _stop_and_suppress_eloop(loop):
try:
loop.stop()
except Exception:
pass
def _get_conn_props():
return {
"product": USER_AGENT,
"version": VERSION,
"framework": "Python {}.{}.{}".format(*sys.version_info[0:3]),
"platform": sys.platform,
}
def _get_container_id():
return "{}/{}".format(USER_AGENT, str(uuid4()))