Exemple #1
0
def fetch(start_time=None,
          end_time=None,
          offset=None,
          minimum_log_level=None,
          include_incomplete=False,
          include_app_logs=False,
          version_ids=None,
          **kwargs):
    """Returns an iterator yielding an application's request and application logs.

  Logs will be returned by the iterator in reverse chronological order by
  request end time, or by last flush time for requests still in progress (if
  requested).  The items yielded are
  google.appengine.api.logservice.log_service_pb.RequestLog protocol buffer
  objects, the contents of which are accessible via method calls.

  All parameters are optional.

  Args:
    start_time: The earliest request completion or last-update time that
      results should be fetched for, in seconds since the Unix epoch.
    end_time: The latest request completion or last-update time that
      results should be fetched for, in seconds since the Unix epoch.
    offset: A byte string representing an offset into the log stream, extracted
      from a previously emitted RequestLog.  This iterator will begin
      immediately after the record from which the offset came.
    minimum_log_level: An application log level which serves as a filter on the
      requests returned--requests with no application log at or above the
      specified level will be omitted.  Works even if include_app_logs is not
      True.  In ascending order, the available log levels are:
      logservice.LOG_LEVEL_DEBUG, logservice.LOG_LEVEL_INFO,
      logservice.LOG_LEVEL_WARNING, logservice.LOG_LEVEL_ERROR,
      and logservice.LOG_LEVEL_CRITICAL.
    include_incomplete: Whether or not to include requests that have started but
      not yet finished, as a boolean.  Defaults to False.
    include_app_logs: Whether or not to include application level logs in the
      results, as a boolean.  Defaults to False.
    version_ids: A list of version ids whose logs should be queried against.
      Defaults to the application's current version id only.

  Returns:
    An iterable object containing the logs that the user has queried for.

  Raises:
    InvalidArgumentError: Raised if any of the input parameters are not of the
      correct type.
  """

    args_diff = set(kwargs) - _FETCH_KWARGS
    if args_diff:
        raise InvalidArgumentError('Invalid arguments: %s' %
                                   ', '.join(args_diff))

    request = log_service_pb.LogReadRequest()

    request.set_app_id(os.environ['APPLICATION_ID'])

    if start_time is not None:
        if not isinstance(start_time, (float, int, long)):
            raise InvalidArgumentError('start_time must be a float or integer')
        request.set_start_time(long(start_time * 1000000))

    if end_time is not None:
        if not isinstance(end_time, (float, int, long)):
            raise InvalidArgumentError('end_time must be a float or integer')
        request.set_end_time(long(end_time * 1000000))

    if offset is not None:
        try:
            request.mutable_offset().ParseFromString(offset)
        except (TypeError, ProtocolBuffer.ProtocolBufferDecodeError):
            raise InvalidArgumentError(
                'offset must be a string or read-only buffer')

    if minimum_log_level is not None:
        if not isinstance(minimum_log_level, int):
            raise InvalidArgumentError('minimum_log_level must be an int')

        if not minimum_log_level in range(LOG_LEVEL_CRITICAL + 1):
            raise InvalidArgumentError(
                """minimum_log_level must be between 0 and 4
                                 inclusive""")
        request.set_minimum_log_level(minimum_log_level)

    if not isinstance(include_incomplete, bool):
        raise InvalidArgumentError('include_incomplete must be a boolean')
    request.set_include_incomplete(include_incomplete)

    if not isinstance(include_app_logs, bool):
        raise InvalidArgumentError('include_app_logs must be a boolean')
    request.set_include_app_logs(include_app_logs)

    if version_ids is None:
        version_id = os.environ['CURRENT_VERSION_ID']
        version_ids = [version_id.split('.')[0]]
    else:
        if not isinstance(version_ids, list):
            raise InvalidArgumentError('version_ids must be a list')
        for version_id in version_ids:
            if not _MAJOR_VERSION_ID_RE.match(version_id):
                raise InvalidArgumentError(
                    'version_ids must only contain valid major version identifiers'
                )

    request.version_id_list()[:] = version_ids

    prototype_request = kwargs.get('prototype_request')
    if prototype_request:
        if not isinstance(prototype_request, log_service_pb.LogReadRequest):
            raise InvalidArgumentError(
                'prototype_request must be a LogReadRequest')
        request.MergeFrom(prototype_request)

    timeout = kwargs.get('timeout')
    if timeout is not None:
        if not isinstance(timeout, (float, int, long)):
            raise InvalidArgumentError('timeout must be a float or integer')

    batch_size = kwargs.get('batch_size')
    if batch_size is not None:
        if not isinstance(batch_size, (int, long)):
            raise InvalidArgumentError('batch_size must be an integer')

        if batch_size < 1:
            raise InvalidArgumentError('batch_size must be greater than zero')

        if batch_size > MAX_ITEMS_PER_FETCH:
            raise InvalidArgumentError('batch_size specified is too large')
        request.set_count(batch_size)

    return _LogQueryResult(request, timeout=timeout)
def fetch(start_time=None,
          end_time=None,
          offset=None,
          minimum_log_level=None,
          include_incomplete=False,
          include_app_logs=False,
          module_versions=None,
          version_ids=None,
          request_ids=None,
          **kwargs):
    """Returns an iterator yielding an application's request and application logs.

  Logs will be returned by the iterator in reverse chronological order by
  request end time, or by last flush time for requests still in progress (if
  requested).  The items yielded are RequestLog objects, the contents of which
  are accessible via method calls.

  All parameters are optional.

  Args:
    start_time: The earliest request completion or last-update time that
      results should be fetched for, in seconds since the Unix epoch.
    end_time: The latest request completion or last-update time that
      results should be fetched for, in seconds since the Unix epoch.
    offset: A byte string representing an offset into the log stream, extracted
      from a previously emitted RequestLog.  This iterator will begin
      immediately after the record from which the offset came.
    minimum_log_level: An application log level which serves as a filter on the
      requests returned--requests with no application log at or above the
      specified level will be omitted.  Works even if include_app_logs is not
      True.  In ascending order, the available log levels are:
      logservice.LOG_LEVEL_DEBUG, logservice.LOG_LEVEL_INFO,
      logservice.LOG_LEVEL_WARNING, logservice.LOG_LEVEL_ERROR,
      and logservice.LOG_LEVEL_CRITICAL.
    include_incomplete: Whether or not to include requests that have started but
      not yet finished, as a boolean.  Defaults to False.
    include_app_logs: Whether or not to include application level logs in the
      results, as a boolean.  Defaults to False.
    module_versions: A list of tuples of the form (module, version), that
      indicate that the logs for the given module/version combination should be
      fetched.  Duplicate tuples will be ignored.  This kwarg may not be used
      in conjunction with the 'version_ids' kwarg.
    version_ids: A list of version ids whose logs should be queried against.
      Defaults to the application's current version id only.  This kwarg may not
      be used in conjunction with the 'module_versions' kwarg.
    request_ids: If not None, indicates that instead of a time-based scan, logs
      for the specified requests should be returned.  Malformed request IDs will
      cause the entire request to be rejected, while any requests that are
      unknown will be ignored. This option may not be combined with any
      filtering options such as start_time, end_time, offset, or
      minimum_log_level.  version_ids is ignored.  IDs that do not correspond to
      a request log will be ignored.  Logs will be returned in the order
      requested.

  Returns:
    An iterable object containing the logs that the user has queried for.

  Raises:
    InvalidArgumentError: Raised if any of the input parameters are not of the
      correct type.
  """

    args_diff = set(kwargs) - _FETCH_KWARGS
    if args_diff:
        raise InvalidArgumentError('Invalid arguments: %s' %
                                   ', '.join(args_diff))

    request = log_service_pb.LogReadRequest()

    request.set_app_id(os.environ['APPLICATION_ID'])

    if start_time is not None:
        if not isinstance(start_time, (float, int, long)):
            raise InvalidArgumentError('start_time must be a float or integer')
        request.set_start_time(long(start_time * 1000000))

    if end_time is not None:
        if not isinstance(end_time, (float, int, long)):
            raise InvalidArgumentError('end_time must be a float or integer')
        request.set_end_time(long(end_time * 1000000))

    if offset is not None:
        try:
            request.mutable_offset().ParseFromString(offset)
        except (TypeError, ProtocolBuffer.ProtocolBufferDecodeError):
            raise InvalidArgumentError(
                'offset must be a string or read-only buffer')

    if minimum_log_level is not None:
        if not isinstance(minimum_log_level, int):
            raise InvalidArgumentError('minimum_log_level must be an int')

        if minimum_log_level not in logsutil.LOG_LEVELS:
            raise InvalidArgumentError('minimum_log_level must be one of %s' %
                                       repr(logsutil.LOG_LEVELS))
        request.set_minimum_log_level(minimum_log_level)

    if not isinstance(include_incomplete, bool):
        raise InvalidArgumentError('include_incomplete must be a boolean')
    request.set_include_incomplete(include_incomplete)

    if not isinstance(include_app_logs, bool):
        raise InvalidArgumentError('include_app_logs must be a boolean')
    request.set_include_app_logs(include_app_logs)

    if 'server_versions' in kwargs:
        logging.warning('The server_versions kwarg to the fetch() method is '
                        'deprecated.  Please use the module_versions kwarg '
                        'instead.')
        module_versions = kwargs.pop('server_versions')
    if version_ids and module_versions:
        raise InvalidArgumentError(
            'version_ids and module_versions may not be '
            'used at the same time.')

    if version_ids is None and module_versions is None:
        module_version = request.add_module_version()
        if os.environ['CURRENT_MODULE_ID'] != 'default':

            module_version.set_module_id(os.environ['CURRENT_MODULE_ID'])
        module_version.set_version_id(
            os.environ['CURRENT_VERSION_ID'].split('.')[0])

    if module_versions:
        if not isinstance(module_versions, list):
            raise InvalidArgumentError('module_versions must be a list')

        req_module_versions = set()
        for entry in module_versions:
            if not isinstance(entry, (list, tuple)):
                raise InvalidArgumentError(
                    'module_versions list entries must all be '
                    'tuples or lists.')
            if len(entry) != 2:
                raise InvalidArgumentError(
                    'module_versions list entries must all be '
                    'of length 2.')
            req_module_versions.add((entry[0], entry[1]))

        for module, version in sorted(req_module_versions):
            req_module_version = request.add_module_version()

            if module != 'default':
                req_module_version.set_module_id(module)
            req_module_version.set_version_id(version)

    if version_ids:
        if not isinstance(version_ids, list):
            raise InvalidArgumentError('version_ids must be a list')
        for version_id in version_ids:
            if not _MAJOR_VERSION_ID_RE.match(version_id):
                raise InvalidArgumentError(
                    'version_ids must only contain valid major version identifiers'
                )
            request.add_module_version().set_version_id(version_id)

    if request_ids is not None:
        if not isinstance(request_ids, list):
            raise InvalidArgumentError('request_ids must be a list')
        if not request_ids:
            raise InvalidArgumentError('request_ids must not be empty')
        if len(request_ids) != len(set(request_ids)):
            raise InvalidArgumentError(
                'request_ids must not contain duplicates')
        for request_id in request_ids:
            if not _REQUEST_ID_RE.match(request_id):
                raise InvalidArgumentError('%s is not a valid request log id' %
                                           request_id)
        request.request_id_list()[:] = request_ids

    prototype_request = kwargs.get('prototype_request')
    if prototype_request:
        if not isinstance(prototype_request, log_service_pb.LogReadRequest):
            raise InvalidArgumentError(
                'prototype_request must be a LogReadRequest')
        request.MergeFrom(prototype_request)

    timeout = kwargs.get('timeout')
    if timeout is not None:
        if not isinstance(timeout, (float, int, long)):
            raise InvalidArgumentError('timeout must be a float or integer')

    batch_size = kwargs.get('batch_size')
    if batch_size is not None:
        if not isinstance(batch_size, (int, long)):
            raise InvalidArgumentError('batch_size must be an integer')

        if batch_size < 1:
            raise InvalidArgumentError('batch_size must be greater than zero')

        if batch_size > MAX_ITEMS_PER_FETCH:
            raise InvalidArgumentError('batch_size specified is too large')
        request.set_count(batch_size)

    return _LogQueryResult(request, timeout=timeout)
def fetch(start_time=None,
          end_time=None,
          minimum_log_level=None,
          include_incomplete=False,
          include_app_logs=False,
          version_ids=None,
          batch_size=None,
          **kwargs):
    """Fetches an application's request and application logs.

  Results will be yielded in reverse chronological order by request end time,
  or by last flush time for requests still in progress (if requested).

  All parameters are optional.

  Args:
    start_time: The earliest request completion or last-update time that
      results should be fetched for, in seconds since the Unix epoch.
    end_time: The latest request completion or last-update time that
      results should be fetched for, in seconds since the Unix epoch.
    minimum_log_level: An application log level which serves as a filter on the
      requests returned--requests with no application log at or above the
      specified level will be omitted.  Works even if include_app_logs is not
      True.  In ascending order, the available log levels are:
      logservice.LOG_LEVEL_DEBUG, logservice.LOG_LEVEL_INFO,
      logservice.LOG_LEVEL_WARNING, logservice.LOG_LEVEL_ERROR,
      and logservice.LOG_LEVEL_CRITICAL.
    include_incomplete: Whether or not to include requests that have started but
      not yet finished, as a boolean.  Defaults to False.
    include_app_logs: Whether or not to include application level logs in the
      results, as a boolean.  Defaults to False.
    version_ids: A list of version ids whose logs should be queried against.
      Defaults to the application's current version id only.
    batch_size: The number of log records that the iterator for this request
      should request from the storage infrastructure at a time.

  Returns:
    An iterable object containing the logs that the user has queried for.

  Raises:
    InvalidArgumentError: Raised if any of the input parameters are not of the
      correct type.
  """

    args_diff = set(kwargs.iterkeys()) - _FETCH_KWARGS
    if args_diff:
        raise InvalidArgumentError('Invalid arguments: %s' %
                                   ', '.join(args_diff))

    request = log_service_pb.LogReadRequest()

    request.set_app_id(os.environ['APPLICATION_ID'])

    start_time_usec = kwargs.get('start_time_usec')
    if start_time_usec is not None:
        if not isinstance(start_time_usec, (float, int, long)):
            raise InvalidArgumentError(
                'start_time_usec must be a float or integer')
        if start_time is not None:
            raise InvalidArgumentError(
                'start_time_usec and start_time may not be used together')
        request.set_start_time(long(start_time_usec))
    end_time_usec = kwargs.get('end_time_usec')
    if end_time_usec is not None:
        if not isinstance(end_time_usec, (float, int, long)):
            raise InvalidArgumentError(
                'end_time_usec must be a float or integer')
        if end_time is not None:
            raise InvalidArgumentError(
                'end_time_usec and end_time may not be used together')
        request.set_end_time(long(end_time_usec))

    if start_time is not None:
        if not isinstance(start_time, (float, int, long)):
            raise InvalidArgumentError('start_time must be a float or integer')
        request.set_start_time(long(start_time * 1000000))

    if end_time is not None:
        if not isinstance(end_time, (float, int, long)):
            raise InvalidArgumentError('end_time must be a float or integer')
        request.set_end_time(long(end_time * 1000000))

    if batch_size is not None:
        if not isinstance(batch_size, (int, long)):
            raise InvalidArgumentError('batch_size must be an integer')

        if batch_size < 1:
            raise InvalidArgumentError('batch_size must be greater than zero')

        if batch_size > MAX_ITEMS_PER_FETCH:
            raise InvalidArgumentError('batch_size specified is too large')
        request.set_count(batch_size)

    if minimum_log_level is None:
        minimum_log_level = kwargs.get('min_log_level')
    if minimum_log_level is not None:
        if not isinstance(minimum_log_level, int):
            raise InvalidArgumentError('minimum_log_level must be an int')

        if not minimum_log_level in range(LOG_LEVEL_CRITICAL + 1):
            raise InvalidArgumentError(
                """minimum_log_level must be between 0 and 4
                                 inclusive""")
        request.set_minimum_log_level(minimum_log_level)

    if not isinstance(include_incomplete, bool):
        raise InvalidArgumentError('include_incomplete must be a boolean')
    request.set_include_incomplete(include_incomplete)

    if not isinstance(include_app_logs, bool):
        raise InvalidArgumentError('include_app_logs must be a boolean')
    request.set_include_app_logs(include_app_logs)

    if version_ids is None:
        version_id = os.environ['CURRENT_VERSION_ID']
        version_ids = [version_id.split('.')[0]]
    else:
        if not isinstance(version_ids, list):
            raise InvalidArgumentError('version_ids must be a list')
        for version_id in version_ids:
            if not _MAJOR_VERSION_ID_RE.match(version_id):
                raise InvalidArgumentError(
                    'version_ids must only contain valid major version identifiers'
                )

    request.version_id_list()[:] = version_ids

    prototype_request = kwargs.get('prototype_request')
    if prototype_request:
        if not isinstance(prototype_request, log_service_pb.LogReadRequest):
            raise InvalidArgumentError(
                'prototype_request must be a LogReadRequest')
        request.MergeFrom(prototype_request)

    return _LogQueryResult(request)