Skip to content

Logstashsupport #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions caso/messenger/logstash.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

"""Module containing a Logstask cASO messenger."""

import json
import datetime

Check warning on line 20 in caso/messenger/logstash.py

View check run for this annotation

Codecov / codecov/patch

caso/messenger/logstash.py#L19-L20

Added lines #L19 - L20 were not covered by tests
import socket

from oslo_config import cfg
from oslo_log import log
import six

from caso import exception
import caso.messenger
Expand Down Expand Up @@ -49,11 +50,24 @@
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def push(self, records):
"""Push records to logstash using tcp."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring needs to be added again, as flake8 will complain


# NOTE(acostantini): code for the serialization and push of the
# records in logstash. JSON format to be used and encoding UTF-8
if not records:
return

Check warning on line 57 in caso/messenger/logstash.py

View check run for this annotation

Codecov / codecov/patch

caso/messenger/logstash.py#L57

Added line #L57 was not covered by tests

# Actual timestamp to be added on each record
ct = int(datetime.datetime.now().timestamp())

Check warning on line 60 in caso/messenger/logstash.py

View check run for this annotation

Codecov / codecov/patch

caso/messenger/logstash.py#L60

Added line #L60 was not covered by tests

# Open the connection with LS
self.sock.connect((self.host, self.port))

Check warning on line 63 in caso/messenger/logstash.py

View check run for this annotation

Codecov / codecov/patch

caso/messenger/logstash.py#L63

Added line #L63 was not covered by tests

try:
self.sock.connect((self.host, self.port))
for _, record in six.iteritems(records):
self.sock.sendall(record.as_json() + "\n")
for record in records:
# serialization of record
rec = record.serialization_message()
rec["caso-timestamp"] = ct
self.sock.send((json.dumps(rec) + "\n").encode("utf-8"))

Check warning on line 70 in caso/messenger/logstash.py

View check run for this annotation

Codecov / codecov/patch

caso/messenger/logstash.py#L68-L70

Added lines #L68 - L70 were not covered by tests
except socket.error as e:
raise exception.LogstashConnectionError(
host=self.host, port=self.port, exception=e
Expand Down
15 changes: 15 additions & 0 deletions caso/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@
"""Render record as the expected SSM message."""
raise NotImplementedError("Method not implemented")

def serialization_message(self):
"""Render record as the expected logstash message."""
opts = {

Check warning on line 53 in caso/record.py

View check run for this annotation

Codecov / codecov/patch

caso/record.py#L53

Added line #L53 was not covered by tests
"by_alias": True,
"exclude_none": True,
}
# NOTE(acostatnini): part related to the definition of the logstash message to
# be serialized before to send data
# NOTE(aloga): do not iter over the dictionary returned by record.dict() as this
# is just a dictionary representation of the object, where no serialization is
# done. In order to get objects correctly serialized we need to convert to JSON,
# then reload the model
serialized_record = json.loads(self.json(**opts))
return serialized_record

Check warning on line 64 in caso/record.py

View check run for this annotation

Codecov / codecov/patch

caso/record.py#L63-L64

Added lines #L63 - L64 were not covered by tests


class _ValidCloudStatus(str, enum.Enum):
"""This is a private class to enum valid cloud statuses."""
Expand Down
Loading