Ejemplo n.º 1
0
def _log_entry_mapping_to_pb(mapping):
    """Helper for :meth:`write_entries`, et aliae

    Ideally, would use a function from :mod:`protobuf.json_format`, but
    the right one isn't public.  See:
    https://github.com/google/protobuf/issues/1351
    """
    # pylint: disable=too-many-branches
    entry_pb = LogEntry()

    optional_scalar_keys = {
        'logName': 'log_name',
        'insertId': 'insert_id',
        'textPayload': 'text_payload',
    }

    for key, pb_name in optional_scalar_keys.items():
        if key in mapping:
            setattr(entry_pb, pb_name, mapping[key])

    if 'resource' in mapping:
        entry_pb.resource.type = mapping['resource']['type']

    if 'severity' in mapping:
        severity = mapping['severity']
        if isinstance(severity, str):
            severity = LogSeverity.Value(severity)
        entry_pb.severity = severity

    if 'timestamp' in mapping:
        timestamp = _datetime_to_pb_timestamp(mapping['timestamp'])
        entry_pb.timestamp.CopyFrom(timestamp)

    if 'labels' in mapping:
        for key, value in mapping['labels'].items():
            entry_pb.labels[key] = value

    if 'jsonPayload' in mapping:
        for key, value in mapping['jsonPayload'].items():
            entry_pb.json_payload[key] = value

    if 'protoPayload' in mapping:
        Parse(json.dumps(mapping['protoPayload']), entry_pb.proto_payload)

    if 'httpRequest' in mapping:
        _http_request_mapping_to_pb(
            mapping['httpRequest'], entry_pb.http_request)

    if 'operation' in mapping:
        _log_operation_mapping_to_pb(
            mapping['operation'], entry_pb.operation)

    return entry_pb
Ejemplo n.º 2
0
def _log_entry_mapping_to_pb(mapping):
    """Helper for :meth:`write_entries`, et aliae

    Performs "impedance matching" between the protobuf attrs and the keys
    expected in the JSON API.
    """
    # pylint: disable=too-many-branches
    entry_pb = LogEntry()

    optional_scalar_keys = {
        'logName': 'log_name',
        'insertId': 'insert_id',
        'textPayload': 'text_payload',
    }

    for key, pb_name in optional_scalar_keys.items():
        if key in mapping:
            setattr(entry_pb, pb_name, mapping[key])

    if 'resource' in mapping:
        entry_pb.resource.type = mapping['resource']['type']

    if 'severity' in mapping:
        severity = mapping['severity']
        if isinstance(severity, str):
            severity = LogSeverity.Value(severity)
        entry_pb.severity = severity

    if 'timestamp' in mapping:
        timestamp = _datetime_to_pb_timestamp(mapping['timestamp'])
        entry_pb.timestamp.CopyFrom(timestamp)

    if 'labels' in mapping:
        for key, value in mapping['labels'].items():
            entry_pb.labels[key] = value

    if 'jsonPayload' in mapping:
        for key, value in mapping['jsonPayload'].items():
            entry_pb.json_payload[key] = value

    if 'protoPayload' in mapping:
        Parse(json.dumps(mapping['protoPayload']), entry_pb.proto_payload)

    if 'httpRequest' in mapping:
        _http_request_mapping_to_pb(
            mapping['httpRequest'], entry_pb.http_request)

    if 'operation' in mapping:
        _log_operation_mapping_to_pb(
            mapping['operation'], entry_pb.operation)

    return entry_pb
Ejemplo n.º 3
0
def _log_entry_pb_to_mapping(entry_pb):
    """Helper for :meth:`list_entries`, et aliae

    Ideally, would use a function from :mod:`protobuf.json_format`, but
    the right one isn't public.  See:
    https://github.com/google/protobuf/issues/1351
    """
    mapping = {
        'logName': entry_pb.log_name,
        'resource': _mon_resource_pb_to_mapping(entry_pb.resource),
        'severity': LogSeverity.Name(entry_pb.severity),
        'insertId': entry_pb.insert_id,
        'timestamp': _pb_timestamp_to_rfc3339(entry_pb.timestamp),
        'labels': entry_pb.labels,
    }
    if entry_pb.HasField('text_payload'):
        mapping['textPayload'] = entry_pb.text_payload

    if entry_pb.HasField('json_payload'):
        mapping['jsonPayload'] = _struct_pb_to_mapping(entry_pb.json_payload)

    if entry_pb.HasField('proto_payload'):
        mapping['protoPayload'] = entry_pb.proto_payload

    if entry_pb.http_request:
        request = entry_pb.http_request
        mapping['httpRequest'] = {
            'requestMethod': request.request_method,
            'requestUrl': request.request_url,
            'status': request.status,
            'referer': request.referer,
            'userAgent': request.user_agent,
            'cacheHit': request.cache_hit,
            'requestSize': request.request_size,
            'responseSize': request.response_size,
            'remoteIp': request.remote_ip,
        }

    if entry_pb.operation:
        operation = entry_pb.operation
        mapping['operation'] = {
            'producer': operation.producer,
            'id': operation.id,
            'first': operation.first,
            'last': operation.last,
        }

    return mapping
Ejemplo n.º 4
0
def _log_entry_pb_to_mapping(entry_pb):
    """Helper for :meth:`list_entries`, et aliae

    Performs "impedance matching" between the protobuf attrs and the keys
    expected in the JSON API.
    """
    mapping = {
        'logName': entry_pb.log_name,
        'resource': _mon_resource_pb_to_mapping(entry_pb.resource),
        'severity': LogSeverity.Name(entry_pb.severity),
        'insertId': entry_pb.insert_id,
        'timestamp': _pb_timestamp_to_rfc3339(entry_pb.timestamp),
        'labels': entry_pb.labels,
    }
    if entry_pb.HasField('text_payload'):
        mapping['textPayload'] = entry_pb.text_payload

    if entry_pb.HasField('json_payload'):
        mapping['jsonPayload'] = _struct_pb_to_mapping(entry_pb.json_payload)

    if entry_pb.HasField('proto_payload'):
        mapping['protoPayload'] = entry_pb.proto_payload

    if entry_pb.http_request:
        request = entry_pb.http_request
        mapping['httpRequest'] = {
            'requestMethod': request.request_method,
            'requestUrl': request.request_url,
            'status': request.status,
            'referer': request.referer,
            'userAgent': request.user_agent,
            'cacheHit': request.cache_hit,
            'requestSize': request.request_size,
            'responseSize': request.response_size,
            'remoteIp': request.remote_ip,
        }

    if entry_pb.operation:
        operation = entry_pb.operation
        mapping['operation'] = {
            'producer': operation.producer,
            'id': operation.id,
            'first': operation.first,
            'last': operation.last,
        }

    return mapping