예제 #1
0
    def test_log_proto_w_explicit(self):
        import json
        import datetime
        from google.protobuf.json_format import MessageToJson
        from google.protobuf.struct_pb2 import Struct
        from google.protobuf.struct_pb2 import Value
        from google.cloud.logging import Resource

        message = Struct(fields={"foo": Value(bool_value=True)})
        ALT_LOG_NAME = "projects/foo/logs/alt.log.name"
        DEFAULT_LABELS = {"foo": "spam"}
        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"}
        )
        ENTRIES = [
            {
                "logName": ALT_LOG_NAME,
                "protoPayload": json.loads(MessageToJson(message)),
                "labels": LABELS,
                "insertId": IID,
                "severity": SEVERITY,
                "httpRequest": REQUEST,
                "timestamp": "2016-12-31T00:01:02.999999Z",
                "resource": RESOURCE._to_dict(),
                "trace": TRACE,
                "spanId": SPANID,
                "traceSampled": True,
            }
        ]
        client1 = _Client(self.PROJECT)
        client2 = _Client(self.PROJECT)
        api = client2.logging_api = _DummyLoggingAPI()
        logger = self._make_one(self.LOGGER_NAME, client=client1, labels=DEFAULT_LABELS)

        logger.log_proto(
            message,
            log_name=ALT_LOG_NAME,
            client=client2,
            labels=LABELS,
            insert_id=IID,
            severity=SEVERITY,
            http_request=REQUEST,
            timestamp=TIMESTAMP,
            resource=RESOURCE,
            trace=TRACE,
            span_id=SPANID,
            trace_sampled=True,
        )

        self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None))
예제 #2
0
    def test_commit_w_resource_specified(self):
        from google.cloud.logging_v2.entries import _GLOBAL_RESOURCE
        from google.cloud.logging import Resource

        logger = _Logger()
        client = _Client(project=self.PROJECT, connection=_make_credentials())
        api = client.logging_api = _DummyLoggingAPI()
        RESOURCE = Resource(type="gae_app",
                            labels={
                                "module_id": "default",
                                "version_id": "test"
                            })

        batch = self._make_one(logger, client, resource=RESOURCE)
        MESSAGE = "This is the entry text"
        ENTRIES = [
            {
                "textPayload": MESSAGE
            },
            {
                "textPayload": MESSAGE,
                "resource": _GLOBAL_RESOURCE._to_dict()
            },
        ]
        batch.log_text(MESSAGE, resource=None)
        batch.log_text(MESSAGE)
        batch.commit()
        self.assertEqual(
            api._write_entries_called_with,
            (ENTRIES, logger.full_name, RESOURCE._to_dict(), None),
        )
예제 #3
0
    def test_to_api_repr_proto_explicit(self):
        import datetime
        from google.protobuf.json_format import MessageToDict
        from google.cloud.logging import Resource
        from google.cloud._helpers import _datetime_to_rfc3339
        from google.protobuf.struct_pb2 import Struct
        from google.protobuf.struct_pb2 import Value

        LOG_NAME = "test.log"
        message = Struct(fields={"foo": Value(bool_value=True)})
        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)
        RESOURCE = Resource(type="gae_app",
                            labels={
                                "module_id": "default",
                                "version_id": "test"
                            })
        TRACE = "12345678-1234-5678-1234-567812345678"
        SPANID = "000000000000004a"
        FILE = "my_file.py"
        LINE = 123
        FUNCTION = "my_function"
        SOURCE_LOCATION = {"file": FILE, "line": LINE, "function": FUNCTION}
        OP_ID = "OP_ID"
        PRODUCER = "PRODUCER"
        OPERATION = {
            "id": OP_ID,
            "producer": PRODUCER,
            "first": True,
            "last": False
        }
        expected = {
            "logName": LOG_NAME,
            "protoPayload": MessageToDict(message),
            "labels": LABELS,
            "insertId": IID,
            "severity": SEVERITY,
            "httpRequest": REQUEST,
            "timestamp": _datetime_to_rfc3339(TIMESTAMP),
            "resource": RESOURCE._to_dict(),
            "trace": TRACE,
            "spanId": SPANID,
            "traceSampled": True,
            "sourceLocation": {
                "file": FILE,
                "line": str(LINE),
                "function": FUNCTION
            },
            "operation": OPERATION,
        }

        entry = self._make_one(
            log_name=LOG_NAME,
            payload=message,
            labels=LABELS,
            insert_id=IID,
            severity=SEVERITY,
            http_request=REQUEST,
            timestamp=TIMESTAMP,
            resource=RESOURCE,
            trace=TRACE,
            span_id=SPANID,
            trace_sampled=True,
            source_location=SOURCE_LOCATION,
            operation=OPERATION,
        )

        self.assertEqual(entry.to_api_repr(), expected)
예제 #4
0
    def test_from_api_repr_w_loggers_no_logger_match(self):
        from datetime import datetime
        from google.cloud._helpers import UTC
        from google.cloud.logging import Resource

        klass = self._get_target_class()
        client = _Client(self.PROJECT)
        SEVERITY = "CRITICAL"
        IID = "IID"
        NOW = datetime.utcnow().replace(tzinfo=UTC)
        TIMESTAMP = _datetime_to_rfc3339_w_nanos(NOW)
        LOG_NAME = "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME)
        LABELS = {"foo": "bar", "baz": "qux"}
        METHOD = "POST"
        URI = "https://api.example.com/endpoint"
        RESOURCE = Resource(
            type="gae_app",
            labels={
                "type": "gae_app",
                "labels": {
                    "module_id": "default",
                    "version": "test"
                },
            },
        )
        STATUS = "500"
        TRACE = "12345678-1234-5678-1234-567812345678"
        SPANID = "000000000000004a"
        FILE = "my_file.py"
        LINE_NO = 123
        FUNCTION = "my_function"
        SOURCE_LOCATION = {
            "file": FILE,
            "line": str(LINE_NO),
            "function": FUNCTION
        }
        OP_ID = "OP_ID"
        PRODUCER = "PRODUCER"
        OPERATION = {
            "id": OP_ID,
            "producer": PRODUCER,
            "first": True,
            "last": False
        }
        API_REPR = {
            "logName": LOG_NAME,
            "insertId": IID,
            "timestamp": TIMESTAMP,
            "labels": LABELS,
            "severity": SEVERITY,
            "httpRequest": {
                "requestMethod": METHOD,
                "requestUrl": URI,
                "status": STATUS,
            },
            "resource": RESOURCE._to_dict(),
            "trace": TRACE,
            "spanId": SPANID,
            "traceSampled": True,
            "sourceLocation": SOURCE_LOCATION,
            "operation": OPERATION,
        }
        loggers = {}

        entry = klass.from_api_repr(API_REPR, client, loggers=loggers)

        self.assertEqual(entry.log_name, LOG_NAME)
        logger = entry.logger
        self.assertIsInstance(logger, _Logger)
        self.assertEqual(logger.name, self.LOGGER_NAME)
        self.assertEqual(entry.insert_id, IID)
        self.assertEqual(entry.timestamp, NOW)
        self.assertIsNone(entry.received_timestamp)
        self.assertEqual(entry.labels, LABELS)
        self.assertEqual(entry.severity, SEVERITY)
        self.assertEqual(entry.http_request["requestMethod"], METHOD)
        self.assertEqual(entry.http_request["requestUrl"], URI)
        self.assertEqual(entry.http_request["status"], STATUS)
        self.assertIs(logger.client, client)
        self.assertEqual(logger.name, self.LOGGER_NAME)
        self.assertEqual(loggers, {LOG_NAME: logger})
        self.assertEqual(entry.resource, RESOURCE)
        self.assertEqual(entry.trace, TRACE)
        self.assertEqual(entry.span_id, SPANID)
        self.assertTrue(entry.trace_sampled)

        source_location = entry.source_location
        self.assertEqual(source_location["file"], FILE)
        self.assertEqual(source_location["line"], LINE_NO)
        self.assertEqual(source_location["function"], FUNCTION)

        self.assertEqual(entry.operation, OPERATION)
        self.assertIsNone(entry.payload)