8
8
import logging
9
9
import coloredlogs
10
10
11
+ from dto import ArenaLoggerDtoValidator
11
12
from flask import Flask , request , jsonify
12
13
from flask_restful import Resource , Api
13
14
from config import Config
@@ -44,6 +45,7 @@ def run(self):
44
45
mongodb_collection = mongodb_db [self .mongodb_collection ]
45
46
46
47
print ("Checking for newly streamed messages during at least {} seconds..." .format (self .delay ))
48
+
47
49
try :
48
50
for message in self .kafka_consumer :
49
51
print ()
@@ -52,10 +54,9 @@ def run(self):
52
54
for event in message .value ['harena-log-stream' ]:
53
55
mongodb_collection .insert_one (event )
54
56
print ()
55
- except :
57
+ except Exception as e :
56
58
print ("Object is not a JSON or does not have an 'harena-log-stream' array" )
57
- print ()
58
-
59
+ print ("Exception trace message: " + str (e ))
59
60
60
61
print ("Waiting time ({} seconds) for new messages ended." .format (self .delay ))
61
62
mongodb_client .close ()
@@ -69,7 +70,6 @@ def __init__(self, kafka_producer):
69
70
70
71
LOGGER .debug ("IndexResource initialized" )
71
72
72
-
73
73
def get (self ):
74
74
message = {"message" : "Welcome to the Harena Logger module" ,
75
75
"kafka_bootstrap_connected" : self .kafka_producer .bootstrap_connected ()
@@ -84,25 +84,27 @@ def __init__(self, kafka_producer, target_topic):
84
84
self .target_topic = target_topic
85
85
86
86
87
-
88
87
@cross_origin (origin = '*' )
89
88
def post (self ):
90
89
91
90
try :
92
91
# To do: properly evaluate message body parsing
93
92
message = request .get_json ()
94
93
message ['server_timestamp' ] = "{}" .format (int (round (time .time () * 1000 )))
95
-
94
+
95
+ ArenaLoggerDtoValidator .validateDto (message )
96
96
# Asynchronous by default
97
97
future = self .kafka_producer .send (self .target_topic , json .dumps (message ).encode ('utf-8' ))
98
98
99
99
# Block for 'synchronous' sends
100
100
record_metadata = future .get (timeout = 10 )
101
- except KafkaError :
101
+ except KafkaError as ke :
102
102
# Decide what to do if produce request failed...
103
- log .exception ()
104
- except :
103
+ logging .exception (ke )
104
+ except Exception as e :
105
105
print ("could not validate the json" )
106
+ print ("Exception trace message: " + str (e ))
107
+ logging .exception (e )
106
108
107
109
# Successful result returns assigned partition and offset
108
110
# print(future)
@@ -120,14 +122,11 @@ def get(self):
120
122
}
121
123
return message
122
124
123
-
124
125
if __name__ == '__main__' :
125
126
126
-
127
127
kafka_producer = None
128
128
kafka_consumer = None
129
129
130
-
131
130
while True :
132
131
try :
133
132
kafka_producer = KafkaProducer (bootstrap_servers = Config .HARENA_LOGGER_KAFKA_BROKERS )
@@ -137,15 +136,14 @@ def get(self):
137
136
value_deserializer = lambda m : json .loads (m .decode ('utf-8' )),
138
137
consumer_timeout_ms = Config .HARENA_LOGGER_INTERVAL_S * 1000 )
139
138
break
140
- except :
139
+ except Exception as e :
140
+ print ("Exception trace message: " + str (e ))
141
141
pass
142
142
143
143
print ("Could not exchange metadata with Kafka bootstrap servers for the first time. Retrying..." )
144
144
time .sleep (1 )
145
145
146
146
147
-
148
-
149
147
consumer_thread = KafkaMongodbAppender (mongodb_server_url = Config .HARENA_LOGGER_MONGODB_URL ,
150
148
mongodb_database = Config .HARENA_LOGGER_MONGODB_DB ,
151
149
mongodb_collection = Config .HARENA_LOGGER_MONGODB_COLLECTION ,
0 commit comments