def _create_diagnostic_entry(name=_PYTHON_LIBRARY_NAME, version=_LIBRARY_VERSION, **kw): """Create a diagnostic log entry describing this library The diagnostic log consists of a list of library name and version objects that have handled a given log entry. If this library is the originator of the log entry, it will look like: {logging.googleapis.com/diagnostic: {instrumentation_source: [{name: "python", version: "3.0.0"}]}} Args: name(str): The name of this library (e.g. 'python') version(str) The version of this library (e.g. '3.0.0') Returns: google.cloud.logging_v2.LogEntry: Log entry with library information """ payload = { _DIAGNOSTIC_INFO_KEY: { _INSTRUMENTATION_SOURCE_KEY: [_get_instrumentation_source(name, version)] } } kw["severity"] = "INFO" entry = StructEntry(payload=payload, **kw) return entry
def entry_from_resource(resource, client, loggers): """Detect correct entry type from resource and instantiate. Args: resource (dict): One entry resource from API response. client (~logging_v2.client.Client): Client that owns the log entry. loggers (dict): A mapping of logger fullnames -> loggers. If the logger that owns the entry is not in ``loggers``, the entry will have a newly-created logger. Returns: google.cloud.logging_v2.entries._BaseEntry: The entry instance, constructed via the resource """ if "textPayload" in resource: return TextEntry.from_api_repr(resource, client, loggers=loggers) if "jsonPayload" in resource: return StructEntry.from_api_repr(resource, client, loggers=loggers) if "protoPayload" in resource: return ProtobufEntry.from_api_repr(resource, client, loggers=loggers) return LogEntry.from_api_repr(resource, client, loggers=loggers)
def log_struct(self, info, **kw): """Add a struct entry to be logged during :meth:`commit`. Args: info (dict): The struct entry, kw (Optional[dict]): Additional keyword arguments for the entry. See :class:`~logging_v2.entries.LogEntry`. """ self.entries.append(StructEntry(payload=info, **kw))
def test_log_struct_explicit(self): import datetime from google.cloud.logging_v2.resource import Resource from google.cloud.logging_v2.entries import StructEntry STRUCT = {"message": "Message text", "weather": "partly cloudy"} LABELS = {"foo": "bar", "baz": "qux"} IID = "IID" SEVERITY = "CRITICAL" METHOD = "POST" URI = "https://api.example.com/endpoint" STATUS = "500" TRACE = "12345678-1234-5678-1234-567812345678" SPANID = "000000000000004a" REQUEST = { "requestMethod": METHOD, "requestUrl": URI, "status": STATUS } TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999) RESOURCE = Resource(type="gae_app", labels={ "module_id": "default", "version_id": "test" }) ENTRY = StructEntry( payload=STRUCT, labels=LABELS, insert_id=IID, severity=SEVERITY, http_request=REQUEST, timestamp=TIMESTAMP, resource=RESOURCE, trace=TRACE, span_id=SPANID, trace_sampled=True, ) client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_struct( STRUCT, labels=LABELS, insert_id=IID, severity=SEVERITY, http_request=REQUEST, timestamp=TIMESTAMP, resource=RESOURCE, trace=TRACE, span_id=SPANID, trace_sampled=True, ) self.assertEqual(batch.entries, [ENTRY])
def test_log_struct_defaults(self): from google.cloud.logging_v2.entries import _GLOBAL_RESOURCE from google.cloud.logging_v2.entries import StructEntry STRUCT = {"message": "Message text", "weather": "partly cloudy"} ENTRY = StructEntry(payload=STRUCT, resource=_GLOBAL_RESOURCE) client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_struct(STRUCT) self.assertEqual(batch.entries, [ENTRY])
def test_context_mgr_failure(self): import datetime from google.protobuf.struct_pb2 import Struct from google.protobuf.struct_pb2 import Value from google.cloud.logging_v2.entries import TextEntry from google.cloud.logging_v2.entries import StructEntry from google.cloud.logging_v2.entries import ProtobufEntry TEXT = "This is the entry text" STRUCT = {"message": TEXT, "weather": "partly cloudy"} LABELS = {"foo": "bar", "baz": "qux"} IID = "IID" SEVERITY = "CRITICAL" METHOD = "POST" URI = "https://api.example.com/endpoint" STATUS = "500" REQUEST = { "requestMethod": METHOD, "requestUrl": URI, "status": STATUS } TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999) message = Struct(fields={"foo": Value(bool_value=True)}) client = _Client(project=self.PROJECT) api = client.logging_api = _DummyLoggingAPI() logger = _Logger() UNSENT = [ TextEntry(payload=TEXT, insert_id=IID, timestamp=TIMESTAMP), StructEntry(payload=STRUCT, severity=SEVERITY), ProtobufEntry(payload=message, labels=LABELS, http_request=REQUEST), ] batch = self._make_one(logger, client=client) try: with batch as other: other.log_text(TEXT, insert_id=IID, timestamp=TIMESTAMP) other.log_struct(STRUCT, severity=SEVERITY) other.log_proto(message, labels=LABELS, http_request=REQUEST) raise _Bugout() except _Bugout: pass self.assertEqual(list(batch.entries), UNSENT) self.assertIsNone(api._write_entries_called_with)
'dest_instance': { 'project_id': 'yoyodyne-102010', 'region': 'us-west1', 'vm_name': 'vm-instance-02', 'zone': 'us-west1-a', }, 'dest_vpc': { 'project_id': 'yoyodyne-102010', 'subnetwork_name': 'yoyo-vpc-1', 'vpc_name': 'yoyo-vpc-1', }, 'start_time': '2018-04-03T13:47:31.805417512Z', }, ] SAMPLE_ENTRIES = [StructEntry(x, None) for x in SAMPLE_PAYLODS] class TestClient(Client): _credentials = '' class FlowRecordTests(TestCase): def test_init_outbound(self): flow_record = FlowRecord(SAMPLE_ENTRIES[0]) for attr, expected in [ ('src_ip', ip_address('198.51.100.75')), ('src_port', 49444), ('dest_ip', ip_address('192.0.2.2')), ('dest_port', 3389), ('protocol', 6),