Exemplo n.º 1
0
class TypesEchoAction(Action):
    request_schema = fields.Dictionary(
        {
            'an_int': fields.Integer(),
            'a_float': fields.Float(),
            'a_bool': fields.Boolean(),
            'a_bytes': fields.ByteString(),
            'a_string': fields.UnicodeString(),
            'a_datetime': fields.DateTime(),
            'a_date': fields.Date(),
            'a_time': fields.Time(),
            'a_list': fields.List(fields.Anything(), max_length=0),
            'a_dict': fields.Nullable(fields.Dictionary({})),
        },
        optional_keys=(
            'an_int', 'a_float', 'a_bool', 'a_bytes', 'a_string', 'a_datetime', 'a_date', 'a_time', 'a_list', 'a_dict',
        ),
    )

    response_schema = fields.Dictionary(
        {'r_{}'.format(k): v for k, v in six.iteritems(request_schema.contents)},
        optional_keys=('r_{}'.format(k) for k in request_schema.optional_keys),
    )

    def run(self, request):
        return {'r_{}'.format(k): v for k, v in six.iteritems(request.body)}
Exemplo n.º 2
0
class SettingsToTest(settings.Settings):
    schema: settings.SettingsSchema = {
        'one': fields.Dictionary({
            'a': fields.ClassConfigurationSchema(base_class=ClassUsingAttrs27HintsToTest, description='Nifty schema.'),
            'b': fields.PythonPath(value_schema=fields.UnicodeString(), description='Must be a path, yo.'),
            'c': fields.TypeReference(base_classes=ClassHoldingSigsToTest, description='Refer to that thing!'),
        }),
        'two': fields.SchemalessDictionary(key_type=fields.UnicodeString(), value_type=fields.Boolean()),
        'three': fields.List(fields.Integer()),
        'four': fields.Nullable(fields.Set(fields.ByteString())),
        'five': fields.Any(fields.Integer(), fields.Float()),
        'six': fields.ObjectInstance(valid_type=ClassUsingAttrs27HintsToTest, description='Y u no instance?'),
        'seven': fields.Polymorph(
            'thing',
            {
                'thing1': fields.Dictionary({'z': fields.Boolean()}, allow_extra_keys=True),
                'thing2': fields.Dictionary({'y': fields.Boolean()}, allow_extra_keys=True, optional_keys=('y', )),
            },
        ),
    }

    defaults: settings.SettingsData = {
        'one': {
            'b': 'foo.bar:Class',
        },
        'three': [1, 5, 7],
    }
Exemplo n.º 3
0
class FakeActionTwo(object):
    """Test action documentation"""

    request_schema = fields.UnicodeString(description='Be weird.')

    response_schema = fields.Dictionary({
        'okay':
        fields.Boolean(description='Whether it is okay'),
        'reason':
        fields.Nullable(
            fields.UnicodeString(description='Why it is not okay')),
    })
Exemplo n.º 4
0
class BaseStatusAction(Action):
    """
    Standard base action for status checks. Returns health check and version information.

    If you want to use the status action use `StatusActionFactory(version)`, passing in the version of your service
    and, optionally, the build of your service. If you do not specify an action with name `status` in your server,
    this will be done on your behalf.

    If you want to make a custom status action, subclass this class, make `self._version` return your service's version
    string, `self._build` optionally return your service's build string, and add any additional health check methods
    you desire. Health check methods must start with `check_`.

    Health check methods accept a single argument, the request object (an instance of `ActionRequest`), and return a
    list of tuples in the format `(is_error, code, description)` (or a false-y value if there are no problems):

    - `is_error`: `True` if this is an error, `False` if it is a warning.
    - `code`: Invariant string for this error, like "MYSQL_FAILURE"
    - `description`: Human-readable description of the problem, like "Could not connect to host on port 1234"

    Health check methods can also write to the `self.diagnostics` dictionary to add additional data which will be sent
    back with the response if they like. They are responsible for their own key management in this situation.

    This base status action comes with a disabled-by-default health check method named `_check_client_settings` (the
    leading underscore disables it), which calls `status` on all other services that this service is configured to call
    (using `verbose: False`, which guarantees no further recursive status checking) and includes those responses in
    this action's response. To enable this health check, simply reference it as a new, valid `check_` method name, like
    so:

    .. code:: python

        class MyStatusAction(BaseStatusAction):
            ...
            check_client_settings = BaseStatusAction._check_client_settings
    """
    def __init__(self, *args, **kwargs):
        """
        Constructs a new base status action. Concrete status actions can override this if they want, but must call
        `super`.

        :param settings: The server settings object
        :type settings: dict
        """
        super(BaseStatusAction, self).__init__(*args, **kwargs)

        self.diagnostics = {}

    @abc.abstractproperty
    def _version(self):
        raise NotImplementedError(
            'version must be defined using StatusActionFactory')

    @property
    def _build(self):
        return None

    description = (
        'Returns version info for the service, Python, PySOA, and Conformity. If the service has a build string, that '
        'is also returned. If the service has defined additional health check behavior and the `verbose` request '
        'attribute is not set to `False`, those additional health checks are performed and returned in the '
        '`healthcheck` response attribute. If the `verbose` request attribute is set to `False`, the additional '
        'health checks are not performed and `healthcheck` is not included in the response (importantly, the `check_` '
        'methods are not invoked).')

    request_schema = fields.Nullable(
        fields.Dictionary(
            {
                'verbose':
                fields.Boolean(
                    description=
                    'If specified and False, this instructs the status action to return only the baseline '
                    'status information (Python, service, PySOA, and other library versions) and omit any of '
                    'the health check operations (no `healthcheck` attribute will be included in the '
                    'response). This provides a useful way to obtain the service version very quickly without '
                    'executing the often time-consuming code necessary for the full health check. It defaults '
                    'to True, which means "return everything."', ),
            },
            optional_keys=('verbose', ),
        ))

    response_schema = fields.Dictionary(
        {
            'build':
            fields.UnicodeString(
                description='The version build string, if applicable.'),
            'conformity':
            fields.UnicodeString(
                description='The version of Conformity in use.'),
            'healthcheck':
            fields.Dictionary(
                {
                    'warnings':
                    fields.List(
                        fields.Tuple(
                            fields.UnicodeString(
                                description='The invariant warning code'),
                            fields.UnicodeString(
                                description='The readable warning description'
                            ),
                        ),
                        description=
                        'A list of any warnings encountered during the health checks.',
                    ),
                    'errors':
                    fields.List(
                        fields.Tuple(
                            fields.UnicodeString(
                                description='The invariant error code'),
                            fields.UnicodeString(
                                description='The readable error description'),
                        ),
                        description=
                        'A list of any errors encountered during the health checks.',
                    ),
                    'diagnostics':
                    fields.SchemalessDictionary(
                        key_type=fields.UnicodeString(),
                        description=
                        'A dictionary containing any additional diagnostic information output by the '
                        'health check operations.',
                    ),
                },
                optional_keys=('warnings', 'errors', 'diagnostics'),
                description=
                'Information about any additional health check operations performed.',
            ),
            'pysoa':
            fields.UnicodeString(description='The version of PySOA in use.'),
            'python':
            fields.UnicodeString(description='The version of Python in use.'),
            'version':
            fields.UnicodeString(
                description='The version of the responding service.'),
        },
        optional_keys=(
            'build',
            'healthcheck',
        ),
    )

    def run(self, request):
        """
        Adds version information for Conformity, PySOA, Python, and the service to the response, then scans the class
        for `check_` methods and runs them (unless `verbose` is `False`).

        :param request: The request object
        :type request: EnrichedActionRequest

        :return: The response
        """
        status = {
            'conformity': six.text_type(conformity.__version__),
            'pysoa': six.text_type(pysoa.__version__),
            'python': six.text_type(platform.python_version()),
            'version': self._version,
        }

        if self._build:
            status['build'] = self._build

        if not request.body or request.body.get('verbose', True) is True:
            errors = []
            warnings = []
            self.diagnostics = {}

            # Find all things called "check_<something>" on this class.
            # We can't just scan __dict__ because of class inheritance.
            check_methods = [
                getattr(self, x) for x in dir(self) if x.startswith('check_')
            ]
            for check_method in check_methods:
                # Call the check, and see if it returned anything
                try:
                    problems = check_method(request)
                except TypeError as e:
                    raise RuntimeError(
                        'Status action check_* methods must accept a single argument of type ActionRequest',
                        e,
                    )
                if problems:
                    for is_error, code, description in problems:
                        # Parcel out the values into the right return list
                        if is_error:
                            errors.append((code, description))
                        else:
                            warnings.append((code, description))

            status['healthcheck'] = {
                'errors': errors,
                'warnings': warnings,
                'diagnostics': self.diagnostics,
            }

        return status

    def _check_client_settings(self, request):
        """
        This method checks any client settings configured for this service to call other services, calls the `status`
        action of each configured service with `verbose: False` (which guarantees no further recursive status checking),
        adds that diagnostic information, and reports any problems. To include this check in your status action, define
        `check_client_settings = BaseStatusAction._check_client_settings` in your status action class definition.
        """
        if not request.client.settings:
            # There's no need to even add diagnostic details if no client settings are configured
            return

        self.diagnostics['services'] = {}

        service_names = list(six.iterkeys(request.client.settings))
        try:
            job_responses = request.client.call_jobs_parallel(
                [{
                    'service_name': service_name,
                    'actions': [{
                        'action': 'status',
                        'body': {
                            'verbose': False
                        }
                    }]
                } for service_name in service_names],
                timeout=2,
                catch_transport_errors=True,
                raise_action_errors=False,
                raise_job_errors=False,
            )
        except Exception as e:
            return [(True, 'CHECK_SERVICES_UNKNOWN_ERROR', six.text_type(e))]

        problems = []
        for i, service_name in enumerate(service_names):
            response = job_responses[i]
            if isinstance(response, Exception):
                problems.append((True, '{}_TRANSPORT_ERROR'.format(
                    service_name.upper()), six.text_type(response)), )
            elif response.errors:
                problems.append((True, '{}_CALL_ERROR'.format(
                    service_name.upper()), six.text_type(response.errors)), )
            elif response.actions[0].errors:
                problems.append(
                    (True, '{}_STATUS_ERROR'.format(service_name.upper()),
                     six.text_type(response.actions[0].errors)), )
            else:
                self.diagnostics['services'][service_name] = response.actions[
                    0].body

        return problems
Exemplo n.º 5
0
class ServerSettings(SOASettings):
    """
    Settings specific to servers
    """

    schema = {
        'transport':
        BasicClassSchema(BaseServerTransport),
        'middleware':
        fields.List(
            BasicClassSchema(ServerMiddleware),
            description=
            'The list of all `ServerMiddleware` objects that should be applied to requests processed by '
            'this server',
        ),
        'client_routing':
        fields.SchemalessDictionary(
            key_type=fields.UnicodeString(),
            value_type=fields.SchemalessDictionary(),
            description=
            'Client settings for sending requests to other services; keys should be service names, and '
            'values should be the corresponding configuration dicts, which will be validated using the '
            'PolymorphicClientSettings schema',
        ),
        'logging':
        fields.Dictionary(
            {
                'version':
                fields.Integer(gte=1, lte=1),
                'formatters':
                fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=fields.Dictionary(
                        {
                            'format': fields.UnicodeString(),
                            'datefmt': fields.UnicodeString(),
                        },
                        optional_keys=('datefmt', ),
                    ),
                ),
                'filters':
                fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=fields.Dictionary(
                        {
                            '()':
                            fields.Anything(
                                description='The optional filter class'),
                            'name':
                            fields.UnicodeString(
                                description='The optional filter name'),
                        },
                        optional_keys=('()', 'name'),
                    ),
                ),
                'handlers':
                fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=fields.Dictionary(
                        {
                            'class': fields.UnicodeString(),
                            'level': fields.UnicodeString(),
                            'formatter': fields.UnicodeString(),
                            'filters': fields.List(fields.UnicodeString()),
                        },
                        optional_keys=('level', 'formatter', 'filters'),
                        allow_extra_keys=True,
                    ),
                ),
                'loggers':
                fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=_logger_schema,
                ),
                'root':
                _logger_schema,
                'incremental':
                fields.Boolean(),
                'disable_existing_loggers':
                fields.Boolean(),
            },
            optional_keys=(
                'version',
                'formatters',
                'filters',
                'handlers',
                'root',
                'loggers',
                'incremental',
            ),
            description=
            'Settings for service logging, which should follow the standard Python logging configuration',
        ),
        'harakiri':
        fields.Dictionary(
            {
                'timeout':
                fields.Integer(
                    gte=0,
                    description=
                    'Seconds of inactivity before harakiri is triggered; 0 to disable, defaults to 300',
                ),
                'shutdown_grace':
                fields.Integer(
                    gt=0,
                    description=
                    'Seconds to forcefully shutdown after harakiri is triggered if shutdown does not occur',
                ),
            },
            description=
            'Instructions for automatically terminating a server process when request processing takes '
            'longer than expected.',
        ),
        'request_log_success_level':
        log_level_schema(
            description=
            'The logging level at which full request and response contents will be logged for successful '
            'requests', ),
        'request_log_error_level':
        log_level_schema(
            description=
            'The logging level at which full request and response contents will be logged for requests '
            'whose responses contain errors (setting this to a more severe level than '
            '`request_log_success_level` will allow you to easily filter for unsuccessful requests)',
        ),
        'heartbeat_file':
        fields.Nullable(
            fields.UnicodeString(
                description=
                'If specified, the server will create a heartbeat file at the specified path on startup, '
                'update the timestamp in that file after the processing of every request or every time '
                'idle operations are processed, and delete the file when the server shuts down. The file name '
                'can optionally contain the specifier {{pid}}, which will be replaced with the server process '
                'PID.', )),
        'extra_fields_to_redact':
        fields.Set(
            fields.UnicodeString(),
            description=
            'Use this field to supplement the set of fields that are automatically redacted/censored in '
            'request and response fields with additional fields that your service needs redacted.',
        ),
    }

    defaults = {
        'client_routing': {},
        'logging': {
            'version': 1,
            'formatters': {
                'console': {
                    'format':
                    '%(asctime)s %(levelname)7s %(correlation_id)s %(request_id)s: %(message)s'
                },
                'syslog': {
                    'format':
                    ('%(service_name)s_service: %(name)s %(levelname)s %(module)s %(process)d '
                     'correlation_id %(correlation_id)s request_id %(request_id)s %(message)s'
                     ),
                },
            },
            'filters': {
                'pysoa_logging_context_filter': {
                    '()': 'pysoa.common.logging.PySOALogContextFilter',
                },
            },
            'handlers': {
                'console': {
                    'level': 'INFO',
                    'class': 'logging.StreamHandler',
                    'formatter': 'console',
                    'filters': ['pysoa_logging_context_filter'],
                },
                'syslog': {
                    'level': 'INFO',
                    'class': 'logging.handlers.SysLogHandler',
                    'facility': SysLogHandler.LOG_LOCAL7,
                    'address': ('localhost', 514),
                    'formatter': 'syslog',
                    'filters': ['pysoa_logging_context_filter'],
                },
            },
            'loggers': {},
            'root': {
                'handlers': ['console'],
                'level': 'INFO',
            },
            'disable_existing_loggers': False,
        },
        'harakiri': {
            'timeout': 300,
            'shutdown_grace': 30,
        },
        'request_log_success_level': 'INFO',
        'request_log_error_level': 'INFO',
        'heartbeat_file': None,
        'extra_fields_to_redact': set(),
    }
Exemplo n.º 6
0
class IntrospectionAction(Action):
    """
    This action returns detailed information about the service's defined actions and the request and response schemas
    for each action, along with any documentation defined for the action or for the service itself. It can be passed
    a single action name to return information limited to that single action. Otherwise, it will return information for
    all of the service's actions.

    This action will be added to your service on your behalf if you do not define an action with name `introspect`.

    Making your services and actions capable of being introspected is simple. If your server class has a `description`
    attribute, that will be the service's documentation that introspection returns. If your server class does not have
    this attribute but does have a docstring, introspection will use the docstring. The same rule applies to action
    classes: Introspection first looks for a `description` attribute and then uses the docstring, if any. If neither of
    these are found, the applicable service or action documentation will be done.

    Introspection then looks at the `request_schema` and `response_schema` attributes for each of your actions, and
    includes the details about these schemas in the returned information for each action. Be sure you include field
    descriptions in your schema for the most effective documentation possible.
    """
    description = (
        "This action returns detailed information about the service's defined actions and the request and response "
        "schemas for each action, along with any documentation defined for the action or for the service itself. It "
        "can be passed a single action name to return information limited to that single action. Otherwise, it will "
        "return information for all of the service's actions. If an action is a switched action (meaning the action "
        "extends `SwitchedAction`, and which action code runs is controlled with SOA switches), multiple action "
        "introspection results will be returned for that action, each with a name ending in either `[switch:N]` (where "
        "`N` is the switch value) or `[DEFAULT]` for the default action."
    )

    request_schema = fields.Dictionary(
        {
            'action_name': fields.UnicodeString(
                min_length=1,
                allow_blank=False,
                description='Specify this to limit your introspection to a single action. It will be the only action '
                            'present in the `actions` response attribute. If the requested action does not exist, an '
                            'error will be returned.',
            ),
        },
        optional_keys=('action_name', ),
    )

    response_schema = fields.Dictionary(
        {
            'documentation': fields.Nullable(fields.UnicodeString(
                description='The documentation for the server, unless `action_name` is specified in the request body, '
                            'in which case this is omitted.',
            )),
            'action_names': fields.List(
                fields.UnicodeString(description='The name of an action.'),
                description='An alphabetized list of every action name included in `actions`.',
            ),
            'actions': fields.SchemalessDictionary(
                key_type=fields.UnicodeString(description='The name of the action.'),
                value_type=fields.Dictionary(
                    {
                        'documentation': fields.Nullable(
                            fields.UnicodeString(description='The documentation for the action'),
                        ),
                        'request_schema': fields.Nullable(fields.Anything(
                            description='A description of the expected request schema, including any documentation '
                                        'specified in the schema definition.',
                        )),
                        'response_schema': fields.Nullable(fields.Anything(
                            description='A description of the guaranteed response schema, including any documentation '
                                        'specified in the schema definition.',
                        )),
                    },
                    description='A introspection of a single action',
                ),
                description='A dict mapping action names to action description dictionaries. This contains details '
                            'about every action in the service unless `action_name` is specified in the request body, '
                            'in which case it contains details only for that action.',
            ),
        },
        optional_keys=('documentation', ),
    )

    def __init__(self, server):
        """
        Construct a new introspection action. Unlike its base class, which accepts a server settings object, this
        must be passed a `Server` object, from which it will obtain a settings object. The `Server` code that calls
        this action has special handling to address this requirement.

        :param server: A PySOA server instance
        :type server: Server
        """
        if not isinstance(server, Server):
            raise TypeError('First argument (server) must be a Server instance')

        super(IntrospectionAction, self).__init__(server.settings)

        self.server = server

    def run(self, request):
        """
        Introspects all of the actions on the server and returns their documentation.

        :param request: The request object
        :type request: EnrichedActionRequest

        :return: The response
        """
        if request.body.get('action_name'):
            return self._get_response_for_single_action(request.body.get('action_name'))

        return self._get_response_for_all_actions()

    def _get_response_for_single_action(self, request_action_name):
        action_name = request_action_name
        switch = None

        if SWITCHED_ACTION_RE.match(action_name):
            match = SWITCHED_ACTION_RE.match(action_name)
            action_name = match.group(str('action'))
            if match.group(str('default')):
                switch = SwitchedAction.DEFAULT_ACTION
            else:
                switch = int(match.group(str('switch')))

        if action_name not in self.server.action_class_map and action_name not in ('status', 'introspect'):
            raise ActionError(errors=[
                Error(code=ERROR_CODE_INVALID, message='Action not defined in service', field='action_name'),
            ])

        if action_name in self.server.action_class_map:
            action_class = self.server.action_class_map[action_name]
            if issubclass(action_class, SwitchedAction):
                if switch:
                    if switch == SwitchedAction.DEFAULT_ACTION:
                        action_class = action_class.switch_to_action_map[-1][1]
                    else:
                        for matching_switch, action_class in action_class.switch_to_action_map:
                            if switch == matching_switch:
                                break
                else:
                    response = {
                        'action_names': [],
                        'actions': {}
                    }
                    for sub_name, sub_class in self._iterate_switched_actions(action_name, action_class):
                        response['action_names'].append(sub_name)
                        response['actions'][sub_name] = self._introspect_action(sub_class)
                    response['action_names'] = list(sorted(response['action_names']))
                    return response
        elif action_name == 'introspect':
            action_class = self.__class__
        else:
            action_class = BaseStatusAction

        return {
            'action_names': [request_action_name],
            'actions': {request_action_name: self._introspect_action(action_class)}
        }

    def _get_response_for_all_actions(self):
        response = {
            'actions': {},
            'action_names': [],
            'documentation': getattr(self.server.__class__, 'description', self.server.__class__.__doc__) or None,
        }

        if 'introspect' not in self.server.action_class_map:
            response['action_names'].append('introspect')
            response['actions']['introspect'] = self._introspect_action(self.__class__)

        if 'status' not in self.server.action_class_map:
            response['action_names'].append('status')
            response['actions']['status'] = self._introspect_action(BaseStatusAction)

        for action_name, action_class in six.iteritems(self.server.action_class_map):
            if issubclass(action_class, SwitchedAction):
                for sub_action_name, sub_action_class in self._iterate_switched_actions(action_name, action_class):
                    response['action_names'].append(sub_action_name)
                    response['actions'][sub_action_name] = self._introspect_action(sub_action_class)
            else:
                response['action_names'].append(action_name)
                response['actions'][action_name] = self._introspect_action(action_class)

        response['action_names'] = list(sorted(response['action_names']))

        return response

    @staticmethod
    def _iterate_switched_actions(action_name, action_class):
        found_default = False
        last_index = len(action_class.switch_to_action_map) - 1
        for i, (switch, sub_action_class) in enumerate(action_class.switch_to_action_map):
            if switch == SwitchedAction.DEFAULT_ACTION:
                sub_action_name = '{}[DEFAULT]'.format(action_name)
                found_default = True
            elif not found_default and i == last_index:
                sub_action_name = '{}[DEFAULT]'.format(action_name)
            else:
                sub_action_name = '{}[switch:{}]'.format(action_name, get_switch(switch))

            yield sub_action_name, sub_action_class

    @staticmethod
    def _introspect_action(action_class):
        action = {
            'documentation': getattr(action_class, 'description', action_class.__doc__) or None,
            'request_schema': None,
            'response_schema': None,
        }

        if getattr(action_class, 'request_schema', None):
            action['request_schema'] = action_class.request_schema.introspect()

        if getattr(action_class, 'response_schema', None):
            action['response_schema'] = action_class.response_schema.introspect()

        return action
Exemplo n.º 7
0
class ExpansionSettings(Settings):
    """
    Defines the schema for configuration settings used when expanding objects on responses with the Expansions tool.
    """

    schema = {
        'type_routes':
        fields.SchemalessDictionary(
            key_type=fields.UnicodeString(
                description=
                'The name of the expansion route, to be referenced from the `type_expansions` '
                'configuration', ),
            value_type=fields.Dictionary(
                {
                    'service':
                    fields.UnicodeString(
                        description=
                        'The name of the service to call to resolve this route',
                    ),
                    'action':
                    fields.UnicodeString(
                        description=
                        'The name of the action to call to resolve this route, which must accept a single '
                        'request field of type `List`, to which all the identifiers for matching candidate '
                        'expansions will be passed, and which must return a single response field of type '
                        '`Dictionary`, from which all expansion objects will be obtained',
                    ),
                    'request_field':
                    fields.UnicodeString(
                        description=
                        'The name of the `List` identifier field to place in the `ActionRequest` body when '
                        'making the request to the named service and action',
                    ),
                    'response_field':
                    fields.UnicodeString(
                        description=
                        'The name of the `Dictionary` field returned in the `ActionResponse`, from which '
                        'the expanded objects will be extracted', ),
                },
                description='The instructions for resolving this type route',
            ),
            description=
            'The definition of all recognized types that can be expanded into and information about how '
            'to resolve objects of those types through action calls',
        ),
        'type_expansions':
        fields.SchemalessDictionary(
            key_type=fields.UnicodeString(
                description=
                'The name of the type for which the herein defined expansions can be sought, which will be '
                "matched with a key from the `expansions` dict passed to one of `Client`'s `call_***` "
                'methods, and which must also match the value of a `_type` field found on response objects '
                'on which extra data will be expanded', ),
            value_type=fields.SchemalessDictionary(
                key_type=fields.UnicodeString(
                    description=
                    'The name of an expansion, which will be matched with a value from the `expansions` '
                    "dict passed to one of `Client`'s `call_***` methods corresponding to the type key in "
                    'that dict', ),
                value_type=fields.Dictionary(
                    {
                        'type':
                        fields.Nullable(
                            fields.UnicodeString(
                                description=
                                'The type of object this expansion yields, which must map back to a '
                                '`type_expansions` key in order to support nested/recursive expansions, and '
                                'may be `None` if you do not wish to support nested/recursive expansions for '
                                'this expansion', )),
                        'route':
                        fields.UnicodeString(
                            description=
                            'The route to use to resolve this expansion, which must match a key in the '
                            '`type_routes` configuration', ),
                        'source_field':
                        fields.UnicodeString(
                            description=
                            'The name of the field in the base object that contains the identifier used '
                            'for obtaining the expansion object (the identifier will be passed to the '
                            '`request_field` in the route when resolving the expansion)',
                        ),
                        'destination_field':
                        fields.UnicodeString(
                            description=
                            'The name of a not-already-existent field in the base object into which the '
                            'expansion object will be placed after it is obtained from the route',
                        ),
                        'raise_action_errors':
                        fields.Boolean(
                            description=
                            'Whether to raise action errors encountered when expanding objects these '
                            'objects (by default, action errors are suppressed, which differs from the '
                            'behavior of the `Client` to raise action errors during normal requests)',
                        ),
                    },
                    optional_keys=('raise_action_errors', ),
                    description=
                    'The definition of one specific possible expansion for this object type',
                ),
                description=
                'The definition of all possible expansions for this object type',
            ),
            description=
            'The definition of all types that may contain identifiers that can be expanded into objects '
            'using the `type_routes` configurations',
        ),
    }
Exemplo n.º 8
0
class ServerSettings(SOASettings):
    """
    Base settings class for all servers, whose `middleware` values are restricted to subclasses of `ServerMiddleware`
    and whose `transport` values are restricted to subclasses of `BaseServerTransport`. Middleware and transport
    configuration settings schemas will automatically switch based on the configuration settings schema for the `path`
    for each.
    """

    schema = dict(
        {
            'transport':
            fields.ClassConfigurationSchema(base_class=BaseServerTransport),
            'middleware':
            fields.List(
                fields.ClassConfigurationSchema(base_class=ServerMiddleware),
                description=
                'The list of all `ServerMiddleware` objects that should be applied to requests processed '
                'by this server',
            ),
            'client_routing':
            fields.SchemalessDictionary(
                key_type=fields.UnicodeString(),
                value_type=fields.SchemalessDictionary(),
                description=
                'Client settings for sending requests to other services; keys should be service names, and '
                'values should be the corresponding configuration dicts, which will be validated using the '
                'ClientSettings schema.',
            ),
            'logging':
            PYTHON_LOGGING_CONFIG_SCHEMA,
            'harakiri':
            fields.Dictionary(
                {
                    'timeout':
                    fields.Integer(
                        gte=0,
                        description=
                        'Seconds of inactivity before harakiri is triggered; 0 to disable, defaults to 300',
                    ),
                    'shutdown_grace':
                    fields.Integer(
                        gt=0,
                        description=
                        'Seconds to forcefully shutdown after harakiri is triggered if shutdown does not '
                        'occur',
                    ),
                },
                description=
                'Instructions for automatically terminating a server process when request processing takes '
                'longer than expected.',
            ),
            'request_log_success_level':
            PythonLogLevel(
                description=
                'The logging level at which full request and response contents will be logged for '
                'successful requests', ),
            'request_log_error_level':
            PythonLogLevel(
                description=
                'The logging level at which full request and response contents will be logged for requests '
                'whose responses contain errors (setting this to a more severe level than '
                '`request_log_success_level` will allow you to easily filter for unsuccessful requests)',
            ),
            'heartbeat_file':
            fields.Nullable(
                fields.UnicodeString(
                    description=
                    'If specified, the server will create a heartbeat file at the specified path on startup, '
                    'update the timestamp in that file after the processing of every request or every time '
                    'idle operations are processed, and delete the file when the server shuts down. The file '
                    'name can optionally contain the specifier {{pid}}, which will be replaced with the '
                    'server process PID. Finally, the file name can optionally contain the specifier {{fid}}, '
                    'which will be replaced with the unique-and-deterministic forked process ID whenever the '
                    'server is started with the --fork option (the minimum value is always 1 and the maximum '
                    'value is always equal to the value of the --fork option).',
                )),
            'extra_fields_to_redact':
            fields.Set(
                fields.UnicodeString(),
                description=
                'Use this field to supplement the set of fields that are automatically redacted/censored '
                'in request and response fields with additional fields that your service needs redacted.',
            ),
        }, **extra_schema)  # type: SettingsSchema

    defaults = dict(
        {
            'client_routing': {},
            'logging': {
                'version': 1,
                'formatters': {
                    'console': {
                        'format':
                        '%(asctime)s %(levelname)7s %(correlation_id)s %(request_id)s: %(message)s'
                    },
                    'syslog': {
                        'format':
                        ('%(service_name)s_service: %(name)s %(levelname)s %(module)s %(process)d '
                         'correlation_id %(correlation_id)s request_id %(request_id)s %(message)s'
                         ),
                    },
                },
                'filters': {
                    'pysoa_logging_context_filter': {
                        '()': 'pysoa.common.logging.PySOALogContextFilter',
                    },
                },
                'handlers': {
                    'console': {
                        'level': 'INFO',
                        'class': 'logging.StreamHandler',
                        'formatter': 'console',
                        'filters': ['pysoa_logging_context_filter'],
                    },
                    'syslog': {
                        'level': 'INFO',
                        'class': 'pysoa.common.logging.SyslogHandler',
                        'facility': SyslogHandler.LOG_LOCAL7,
                        'address': ('localhost', 514),
                        'formatter': 'syslog',
                        'filters': ['pysoa_logging_context_filter'],
                    },
                },
                'loggers': {},
                'root': {
                    'handlers': ['console'],
                    'level': 'INFO',
                },
                'disable_existing_loggers': False,
            },
            'harakiri': {
                'timeout': 300,
                'shutdown_grace': 30,
            },
            'request_log_success_level': 'INFO',
            'request_log_error_level': 'INFO',
            'heartbeat_file': None,
            'extra_fields_to_redact': set(),
            'transport': {
                'path':
                'pysoa.common.transport.redis_gateway.server:RedisServerTransport',
            }
        }, **extra_defaults)  # type: SettingsData
Exemplo n.º 9
0
from pymetrics.instruments import (
    Counter,
    Gauge,
    Histogram,
    Metric,
    Tag,
    Timer,
    TimerResolution,
)
from pymetrics.publishers.statsd import StatsdPublisher

__all__ = ('DogStatsdPublisher', )

_datadog_tags_value_type = fields.Nullable(
    fields.Any(fields.UnicodeString(), fields.ByteString(), fields.Integer(),
               fields.Float(), fields.Boolean()), )


@fields.ClassConfigurationSchema.provider(
    fields.Dictionary(
        {
            'host':
            fields.UnicodeString(
                description=
                'The host name or IP address on which the Dogstatsd server is listening',
            ),
            'port':
            fields.Integer(
                description=
                'The port number on which the Dogstatsd server is listening'),
Exemplo n.º 10
0
)
from pymetrics.publishers.utils import publish_metrics
from pymetrics.recorders.base import MetricsRecorder

__all__ = ('DefaultMetricsRecorder', )

M = TypeVar('M', bound=Metric)


@fields.ClassConfigurationSchema.provider(
    fields.Dictionary(
        {
            'prefix':
            fields.Nullable(
                fields.UnicodeString(
                    description=
                    'An optional prefix for all metrics names (the period delimiter will be added for you)',
                )),
            'config':
            fields.Nullable(CONFIGURATION_SCHEMA),
        },
        allow_extra_keys=False,
        optional_keys=('config', ),
        description=
        'The configuration schema for the default metrics recorder constructor arguments. Without the '
        '`config` key, it will not be able publish metrics.',
    ))
class DefaultMetricsRecorder(MetricsRecorder):
    def __init__(self, prefix, config=None):
        # type: (Optional[six.text_type], Optional[Dict[six.text_type, Any]]) -> None
        """