Skip to content
Permalink
Browse files
fix: logger uses default resource (#207)
  • Loading branch information
daniel-sanche committed Mar 9, 2021
1 parent bfafb35 commit 0f90a79d165314d261413cc369408e15f711129f
Showing with 41 additions and 9 deletions.
  1. +3 −1 google/cloud/logging_v2/logger.py
  2. +1 −1 tests/environment
  3. +37 −7 tests/unit/test_logger.py
@@ -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={})
@@ -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):
@@ -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)
@@ -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,
}
]
@@ -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)
@@ -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,
}
]
@@ -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)
@@ -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,
}
]
@@ -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)
@@ -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,
}
]

0 comments on commit 0f90a79

Please sign in to comment.