Skip to content

Commit

Permalink
Merge pull request #24 from Overseas-Student-Living/response-truncati…
Browse files Browse the repository at this point in the history
…ng-filter-fix

Response truncating filter fix
  • Loading branch information
iky committed Sep 28, 2017
2 parents d9b35dd + 4e21478 commit 9d095a0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
35 changes: 26 additions & 9 deletions nameko_tracer/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ class Status(Enum):
Use this name to configure entrypoint logging in ``LOGGING`` setting
of Nameko config.
"""


TRACE_KEY = 'nameko_trace'
""" Name of the log record attribute holding the serialisable details
Contains gathered entrypoint call and result details in serialisable
and compact form.
"""


Expand All @@ -51,47 +50,48 @@ class Status(Enum):
""" A key holding a dictionary of arguments passed to the entrypoint call
"""


REQUEST_REDACTED_KEY = 'call_args_redacted'
"""
A key holding a boolean value saying whether sensitive values of the
entrypoint call arguments were redacted.
"""


RESPONSE_KEY = 'response'
""" A key holding serialisable return value of the entrypoint.
"""


REQUEST_TRUNCATED_KEY = 'call_args_truncated'
"""
A key holding a boolean value saying whether the call args data were
truncated. Set by ``TruncateRequestFilter``.
"""


RESPONSE_TRUNCATED_KEY = 'response_truncated'
"""
A key holding a boolean value saying whether the result data were truncated.
Set by ``TruncateResponseFilter``.
"""


REQUEST_LENGTH_KEY = 'call_args_length'
""" A key holding the original call args data length
Set by ``TruncateRequestFilter`` to the original length of data in
``REQUEST_KEY``.
"""

RESPONSE_LENGTH_KEY = 'response_length'
""" A key holding the original result data length
Set by ``TruncateResponseFilter`` to the original length of data in
``RESPONSE_KEY``.
"""


RESPONSE_STATUS_KEY = 'response_status'
""" A key holding the result status (a value of one of ``Status`` options)
"""
Expand All @@ -108,48 +108,56 @@ class Status(Enum):
Set if the entrypoint resulted into an error
"""


EXCEPTION_PATH_KEY = 'exception_path'
""" A key holding exception path e.g. ``some.module.SomeError``
Set if the entrypoint resulted into an error
"""


EXCEPTION_VALUE_KEY = 'exception_value'
""" A key holding string representation of exception raised
Set if the entrypoint resulted into an error
"""


EXCEPTION_ARGS_KEY = 'exception_args'
""" A key holding a list of exception arguments
Set if the entrypoint resulted into an error
"""


EXCEPTION_TRACEBACK_KEY = 'exception_traceback'
""" A key holding exception traceback string
Set if the entrypoint resulted into an error
"""


EXCEPTION_EXPECTED_KEY = 'exception_expected'
""" A key holding a boolean saying whether the exception raised was one of
errors expected by the entrypoint
"""


SERVICE_NAME_KEY = 'service'
""" A key holding the name of the service
"""


ENTRYPOINT_NAME_KEY = 'entrypoint_name'
""" A key holding the entrypoint service method name e.g. ``'get_user'``
"""


ENTRYPOINT_TYPE_KEY = 'entrypoint_type'
""" A key holding the entrypoint type name e.g. ``'Rpc'``.
"""


CALL_ID_KEY = 'call_id'
""" A key holding the unique ID of the entrypoint call
"""
Expand All @@ -158,10 +166,12 @@ class Status(Enum):
""" A key holding the call ID stack ...
"""


CONTEXT_DATA_KEY = 'context_data'
""" A key holding the worker context data dictionary
"""


DEFAULT_ADAPTERS = {
'nameko.web.handlers.HttpRequestHandler': (
'nameko_tracer.adapters.HttpRequestHandlerAdapter'),
Expand All @@ -171,8 +181,15 @@ class Status(Enum):
Sets an override for Nameko built-in HttpRequestHandler. Extra overrides
coming from config are merged in.
"""


CONFIG_KEY = 'TRACER'
""" Nameko config key holding tracer configuration
"""


ADAPTERS_CONFIG_KEY = 'ADAPTERS'
"""
A key holding adapters configuration in Nameko config (under ``CONFIG_KEY``)
"""
4 changes: 4 additions & 0 deletions nameko_tracer/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class TruncateResponseFilter(BaseTruncateFilter):
lifecycle_stage = constants.Stage.response

def truncate(self, data):

if constants.RESPONSE_KEY not in data:
return data

result = utils.serialise_to_string(data[constants.RESPONSE_KEY])
length = len(result)
if length > self.max_len:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='nameko-tracer',
version='1.0.5',
version='1.0.6',
description='Nameko extension logging entrypoint processing metrics',
author='student.com',
author_email='[email protected]',
Expand Down
24 changes: 24 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,30 @@ def test_truncate_response_to_string_casting(
assert data[constants.RESPONSE_KEY] == expected_response_out


def test_truncate_response_ignores_error_response(handler, logger):

filter_ = filters.TruncateResponseFilter(entrypoints=['^spam'], max_len=5)

logger.addFilter(filter_)

extra = {
constants.TRACE_KEY: {
constants.STAGE_KEY: constants.Stage.response.value,
constants.ENTRYPOINT_NAME_KEY: 'spam',
constants.RESPONSE_STATUS_KEY: constants.Status.error.value,
},
}

logger.info('response', extra=extra)

data = getattr(handler.log_record, constants.TRACE_KEY)

assert constants.RESPONSE_TRUNCATED_KEY not in data
assert constants.RESPONSE_LENGTH_KEY not in data
assert constants.REQUEST_TRUNCATED_KEY not in data
assert constants.REQUEST_LENGTH_KEY not in data


def test_truncate_request_ignores_response_data(handler, logger):

filter_ = filters.TruncateRequestFilter(entrypoints=['^spam'], max_len=5)
Expand Down

0 comments on commit 9d095a0

Please sign in to comment.