Skip to content
Permalink
Browse files
fix: revert default resource behavior to avoid breaking changes (#237)
  • Loading branch information
daniel-sanche committed Mar 24, 2021
1 parent 9069738 commit 24a0a5e674430e97a3a2e3b54477d8f95fa08ec6
Showing with 18 additions and 47 deletions.
  1. +4 −5 google/cloud/logging_v2/handlers/handlers.py
  2. +4 −3 google/cloud/logging_v2/logger.py
  3. +1 −1 tests/environment
  4. +2 −5 tests/unit/handlers/test_handlers.py
  5. +7 −33 tests/unit/test_logger.py
@@ -16,6 +16,8 @@

import logging


from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
from google.cloud.logging_v2.handlers.transports import BackgroundThreadTransport
from google.cloud.logging_v2.handlers._monitored_resources import detect_resource

@@ -61,7 +63,7 @@ def __init__(
*,
name=DEFAULT_LOGGER_NAME,
transport=BackgroundThreadTransport,
resource=None,
resource=_GLOBAL_RESOURCE,
labels=None,
stream=None,
):
@@ -80,15 +82,12 @@ def __init__(
:class:`.BackgroundThreadTransport`. The other
option is :class:`.SyncTransport`.
resource (~logging_v2.resource.Resource):
Resource for this Handler. If not given, will be inferred from the environment.
Resource for this Handler. Defaults to ``global``.
labels (Optional[dict]): Monitored resource of the entry, defaults
to the global resource type.
stream (Optional[IO]): Stream to be used by the handler.
"""
super(CloudLoggingHandler, self).__init__(stream)
if not resource:
# infer the correct monitored resource from the local environment
resource = detect_resource(client.project)
self.name = name
self.client = client
self.transport = transport(client, name)
@@ -20,7 +20,6 @@
from google.cloud.logging_v2.entries import StructEntry
from google.cloud.logging_v2.entries import TextEntry
from google.cloud.logging_v2.resource import Resource
from google.cloud.logging_v2.handlers._monitored_resources import detect_resource


_GLOBAL_RESOURCE = Resource(type="global", labels={})
@@ -49,21 +48,23 @@ class Logger(object):
See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.logs
"""

def __init__(self, name, client, *, labels=None):
def __init__(self, name, client, *, labels=None, resource=_GLOBAL_RESOURCE):
"""
Args:
name (str): The name of the logger.
client (~logging_v2.client.Client):
A client which holds credentials and project configuration
for the logger (which requires a project).
resource (~logging_v2.Resource): a monitored resource object
representing the resource the code was run on.
labels (Optional[dict]): Mapping of default labels for entries written
via this logger.
"""
self.name = name
self._client = client
self.labels = labels
self.default_resource = detect_resource(client.project)
self.default_resource = resource

@property
def client(self):
@@ -38,9 +38,7 @@ def _make_one(self, *args, **kw):

def test_ctor_defaults(self):
import sys
from google.cloud.logging_v2.handlers._monitored_resources import (
_create_global_resource,
)
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
from google.cloud.logging_v2.handlers.handlers import DEFAULT_LOGGER_NAME

patch = mock.patch(
@@ -55,8 +53,7 @@ def test_ctor_defaults(self):
self.assertIsInstance(handler.transport, _Transport)
self.assertIs(handler.transport.client, client)
self.assertEqual(handler.transport.name, DEFAULT_LOGGER_NAME)
global_resource = _create_global_resource(self.PROJECT)
self.assertEqual(handler.resource, global_resource)
self.assertEqual(handler.resource, _GLOBAL_RESOURCE)
self.assertIsNone(handler.labels)
self.assertIs(handler.stream, sys.stderr)

@@ -99,15 +99,11 @@ def test_batch_w_alternate_client(self):
self.assertIs(batch.client, client2)

def test_log_empty_defaults_w_default_labels(self):
from google.cloud.logging_v2.handlers._monitored_resources import (
detect_resource,
)

DEFAULT_LABELS = {"foo": "spam"}
ENTRIES = [
{
"logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME),
"resource": detect_resource(self.PROJECT)._to_dict(),
"resource": {"type": "global", "labels": {}},
"labels": DEFAULT_LABELS,
}
]
@@ -174,11 +170,7 @@ def test_log_empty_w_explicit(self):
self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None))

def test_log_text_defaults(self):
from google.cloud.logging_v2.handlers._monitored_resources import (
detect_resource,
)

RESOURCE = detect_resource(self.PROJECT)._to_dict()
RESOURCE = {"type": "global", "labels": {}}
TEXT = "TEXT"
ENTRIES = [
{
@@ -196,12 +188,8 @@ def test_log_text_defaults(self):
self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None))

def test_log_text_w_unicode_and_default_labels(self):
from google.cloud.logging_v2.handlers._monitored_resources import (
detect_resource,
)

TEXT = "TEXT"
RESOURCE = detect_resource(self.PROJECT)._to_dict()
RESOURCE = {"type": "global", "labels": {}}
DEFAULT_LABELS = {"foo": "spam"}
ENTRIES = [
{
@@ -277,12 +265,8 @@ def test_log_text_explicit(self):
self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None))

def test_log_struct_defaults(self):
from google.cloud.logging_v2.handlers._monitored_resources import (
detect_resource,
)

STRUCT = {"message": "MESSAGE", "weather": "cloudy"}
RESOURCE = detect_resource(self.PROJECT)._to_dict()
RESOURCE = {"type": "global", "labels": {}}
ENTRIES = [
{
"logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME),
@@ -299,12 +283,8 @@ def test_log_struct_defaults(self):
self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None))

def test_log_struct_w_default_labels(self):
from google.cloud.logging_v2.handlers._monitored_resources import (
detect_resource,
)

STRUCT = {"message": "MESSAGE", "weather": "cloudy"}
RESOURCE = detect_resource(self.PROJECT)._to_dict()
RESOURCE = {"type": "global", "labels": {}}
DEFAULT_LABELS = {"foo": "spam"}
ENTRIES = [
{
@@ -383,16 +363,13 @@ def test_log_proto_defaults(self):
import json
from google.protobuf.json_format import MessageToJson
from google.protobuf.struct_pb2 import Struct, Value
from google.cloud.logging_v2.handlers._monitored_resources import (
detect_resource,
)

message = Struct(fields={"foo": Value(bool_value=True)})
ENTRIES = [
{
"logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME),
"protoPayload": json.loads(MessageToJson(message)),
"resource": detect_resource(self.PROJECT)._to_dict(),
"resource": {"type": "global", "labels": {}},
}
]
client = _Client(self.PROJECT)
@@ -407,17 +384,14 @@ def test_log_proto_w_default_labels(self):
import json
from google.protobuf.json_format import MessageToJson
from google.protobuf.struct_pb2 import Struct, Value
from google.cloud.logging_v2.handlers._monitored_resources import (
detect_resource,
)

message = Struct(fields={"foo": Value(bool_value=True)})
DEFAULT_LABELS = {"foo": "spam"}
ENTRIES = [
{
"logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME),
"protoPayload": json.loads(MessageToJson(message)),
"resource": detect_resource(self.PROJECT)._to_dict(),
"resource": {"type": "global", "labels": {}},
"labels": DEFAULT_LABELS,
}
]

0 comments on commit 24a0a5e

Please sign in to comment.