Skip to content

Commit

Permalink
fix: logger uses default resource (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Mar 9, 2021
1 parent bfafb35 commit 0f90a79
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
4 changes: 3 additions & 1 deletion google/cloud/logging_v2/logger.py
Expand Up @@ -20,6 +20,7 @@
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={})
Expand Down Expand Up @@ -62,6 +63,7 @@ def __init__(self, name, client, *, labels=None):
self.name = name
self._client = client
self.labels = labels
self.default_resource = detect_resource(client.project)

@property
def client(self):
Expand Down Expand Up @@ -120,7 +122,7 @@ def _do_log(self, client, _entry_class, payload=None, **kw):
# Apply defaults
kw["log_name"] = kw.pop("log_name", self.full_name)
kw["labels"] = kw.pop("labels", self.labels)
kw["resource"] = kw.pop("resource", _GLOBAL_RESOURCE)
kw["resource"] = kw.pop("resource", self.default_resource)

if payload is not None:
entry = _entry_class(payload=payload, **kw)
Expand Down
44 changes: 37 additions & 7 deletions tests/unit/test_logger.py
Expand Up @@ -99,11 +99,15 @@ 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": {"type": "global", "labels": {}},
"resource": detect_resource(self.PROJECT)._to_dict(),
"labels": DEFAULT_LABELS,
}
]
Expand Down Expand Up @@ -170,12 +174,17 @@ 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()
TEXT = "TEXT"
ENTRIES = [
{
"logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME),
"textPayload": TEXT,
"resource": {"type": "global", "labels": {}},
"resource": RESOURCE,
}
]
client = _Client(self.PROJECT)
Expand All @@ -187,13 +196,18 @@ 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()
DEFAULT_LABELS = {"foo": "spam"}
ENTRIES = [
{
"logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME),
"textPayload": TEXT,
"resource": {"type": "global", "labels": {}},
"resource": RESOURCE,
"labels": DEFAULT_LABELS,
}
]
Expand Down Expand Up @@ -263,12 +277,17 @@ 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()
ENTRIES = [
{
"logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME),
"jsonPayload": STRUCT,
"resource": {"type": "global", "labels": {}},
"resource": RESOURCE,
}
]
client = _Client(self.PROJECT)
Expand All @@ -280,13 +299,18 @@ 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()
DEFAULT_LABELS = {"foo": "spam"}
ENTRIES = [
{
"logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME),
"jsonPayload": STRUCT,
"resource": {"type": "global", "labels": {}},
"resource": RESOURCE,
"labels": DEFAULT_LABELS,
}
]
Expand Down Expand Up @@ -359,13 +383,16 @@ 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": {"type": "global", "labels": {}},
"resource": detect_resource(self.PROJECT)._to_dict(),
}
]
client = _Client(self.PROJECT)
Expand All @@ -380,14 +407,17 @@ 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": {"type": "global", "labels": {}},
"resource": detect_resource(self.PROJECT)._to_dict(),
"labels": DEFAULT_LABELS,
}
]
Expand Down

0 comments on commit 0f90a79

Please sign in to comment.