forked from elgris/microservice-app-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (53 loc) · 1.94 KB
/
main.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
import time
import redis
import os
import json
import requests
from py_zipkin.zipkin import zipkin_span, ZipkinAttrs, generate_random_64bit_string
import time
import random
def log_message(message):
time_delay = random.randrange(0, 2000)
time.sleep(time_delay / 1000)
print('message received after waiting for {}ms: {}'.format(time_delay, message))
if __name__ == '__main__':
redis_host = os.environ['REDIS_HOST']
redis_port = int(os.environ['REDIS_PORT'])
redis_channel = os.environ['REDIS_CHANNEL']
zipkin_url = os.environ['ZIPKIN_URL'] if 'ZIPKIN_URL' in os.environ else ''
def http_transport(encoded_span):
requests.post(
zipkin_url,
data=encoded_span,
headers={'Content-Type': 'application/x-thrift'},
)
pubsub = redis.Redis(host=redis_host, port=redis_port, db=0).pubsub()
pubsub.subscribe([redis_channel])
for item in pubsub.listen():
try:
message = json.loads(str(item['data'].decode("utf-8")))
except Exception as e:
log_message(e)
continue
if not zipkin_url or 'zipkinSpan' not in message:
log_message(message)
continue
span_data = message['zipkinSpan']
try:
with zipkin_span(
service_name='log-message-processor',
zipkin_attrs=ZipkinAttrs(
trace_id=span_data['_traceId']['value'],
span_id=generate_random_64bit_string(),
parent_span_id=span_data['_spanId'],
is_sampled=span_data['_sampled']['value'],
flags=None
),
span_name='save_log',
transport_handler=http_transport,
sample_rate=100
):
log_message(message)
except Exception as e:
print('did not send data to Zipkin: {}'.format(e))
log_message(message)