Skip to content

Commit

Permalink
Merge pull request #20 from Overseas-Student-Living/fix-es-document-s…
Browse files Browse the repository at this point in the history
…erialiser

Fix the ES document serialising formatter
  • Loading branch information
iky committed Aug 24, 2017
2 parents 7e20f5c + cb60f1c commit 983c7d5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
21 changes: 15 additions & 6 deletions nameko_tracer/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ class ElasticsearchDocumentFormatter(JSONFormatter):
"""

extra_serialise_keys = (
constants.CONTEXT_DATA_KEY,
constants.REQUEST_KEY,
constants.RESPONSE_KEY)

def format(self, record):

trace = getattr(record, constants.TRACE_KEY)
trace[constants.CONTEXT_DATA_KEY] = serialise(
trace[constants.CONTEXT_DATA_KEY])
trace[constants.REQUEST_KEY] = serialise(
trace[constants.REQUEST_KEY])
trace[constants.RESPONSE_KEY] = serialise(
trace[constants.RESPONSE_KEY])

for key in self.extra_serialise_keys:
if key in trace:
trace[key] = serialise(trace[key])

if constants.ERROR_KEY in trace:
trace[constants.ERROR_KEY]['exc_args'] = serialise(
trace[constants.ERROR_KEY]['exc_args'])

return serialise(trace)
58 changes: 39 additions & 19 deletions tests/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,45 @@ def test_json_serialiser_will_deal_with_datetime(input_, expected_output):
formatters.JSONFormatter().format(log_record) == expected_output)


def test_elasticsearch_document_serialiser():
@pytest.mark.parametrize(
('key', 'value_in', 'expected_value_out'),
(
(
constants.CONTEXT_DATA_KEY,
{'should': ('be', 'serialised')},
'{"should": ["be", "serialised"]}',
),
(
constants.REQUEST_KEY,
('should', 'be', 'serialised'),
'["should", "be", "serialised"]',
),
(
constants.RESPONSE_KEY,
{'should': ('be', 'serialised')},
'{"should": ["be", "serialised"]}',
),
(
constants.ERROR_KEY,
{
'exc_args': {'should': ('be', 'serialised')},
'spam': {'should': ['NOT', 'be', 'serialised']},
},
{
'exc_args': '{"should": ["be", "serialised"]}',
'spam': {'should': ['NOT', 'be', 'serialised']},
}
),
(
'some-other-key',
{'should': ['NOT', 'be', 'serialised']},
{'should': ['NOT', 'be', 'serialised']},
),
)
)
def test_elasticsearch_document_serialiser(key, value_in, expected_value_out):

trace = {
constants.CONTEXT_DATA_KEY: {'should': ('be', 'serialised')},
constants.REQUEST_KEY: ('should', 'be', 'serialised'),
constants.RESPONSE_KEY: {'should': ('be', 'serialised')},
'some-other-key': {'should': ['NOT', 'be', 'serialised']},
}
trace = {key: value_in}

log_record = Mock()
setattr(log_record, constants.TRACE_KEY, trace)
Expand All @@ -42,15 +73,4 @@ def test_elasticsearch_document_serialiser():

document = json.loads(document)

assert (
document[constants.CONTEXT_DATA_KEY] ==
'{"should": ["be", "serialised"]}')
assert (
document[constants.REQUEST_KEY] ==
'["should", "be", "serialised"]')
assert (
document[constants.RESPONSE_KEY] ==
'{"should": ["be", "serialised"]}')
assert (
document['some-other-key'] ==
{'should': ['NOT', 'be', 'serialised']})
assert document[key] == expected_value_out

0 comments on commit 983c7d5

Please sign in to comment.