|
16 | 16 |
|
17 | 17 | """Module containing a Logstask cASO messenger."""
|
18 | 18 |
|
| 19 | +import json |
| 20 | +import datetime |
19 | 21 | import socket
|
20 | 22 |
|
21 | 23 | from oslo_config import cfg
|
22 | 24 | from oslo_log import log
|
23 |
| -import six |
24 | 25 |
|
25 | 26 | from caso import exception
|
26 | 27 | import caso.messenger
|
27 |
| -#add json lib |
28 |
| -import json |
29 |
| -#add datetime lib |
30 |
| -import datetime |
| 28 | + |
31 | 29 |
|
32 | 30 | opts = [
|
33 | 31 | cfg.StrOpt("host", default="localhost", help="Logstash host to send records to."),
|
@@ -55,26 +53,21 @@ def push(self, records):
|
55 | 53 |
|
56 | 54 | # NOTE(acostantini): code for the serialization and push of the
|
57 | 55 | # records in logstash. JSON format to be used and encoding UTF-8
|
58 |
| - """Serialization of records to be sent to logstash""" |
59 | 56 | if not records:
|
60 | 57 | return
|
61 | 58 |
|
62 |
| - #Actual timestamp to be added on each record |
63 |
| - cdt = datetime.datetime.now() |
| 59 | + # Actual timestamp to be added on each record |
64 | 60 | ct = int(datetime.datetime.now().timestamp())
|
65 | 61 |
|
66 |
| - #Open the connection with LS |
| 62 | + # Open the connection with LS |
67 | 63 | self.sock.connect((self.host, self.port))
|
68 | 64 |
|
69 |
| - """Push records to logstash using tcp.""" |
70 | 65 | try:
|
71 |
| - for record in records: |
72 |
| - #serialization of record |
73 |
| - rec=record.serialization_message() |
74 |
| - #cASO timestamp added to each record |
75 |
| - rec['caso-timestamp']=ct |
76 |
| - #Send the record to LS |
77 |
| - self.sock.send((json.dumps(rec)+'\n').encode('utf-8')) |
| 66 | + for record in records: |
| 67 | + # serialization of record |
| 68 | + rec = record.serialization_message() |
| 69 | + rec["caso-timestamp"] = ct |
| 70 | + self.sock.send((json.dumps(rec) + "\n").encode("utf-8")) |
78 | 71 | except socket.error as e:
|
79 | 72 | raise exception.LogstashConnectionError(
|
80 | 73 | host=self.host, port=self.port, exception=e
|
|
0 commit comments