Skip to content

Commit

Permalink
Merge branch 'main' into owl-bot-update-lock-04c35dc5f49f0f503a306397…
Browse files Browse the repository at this point in the history
…d6d043685f8d2bb822ab515818c4208d7fb2db3a
  • Loading branch information
gkevinzheng authored Jan 29, 2025
2 parents 98d5594 + c47946f commit b4b777b
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "3.11.3"
".": "3.11.4"
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

[1]: https://pypi.org/project/google-cloud-logging/#history

## [3.11.4](https://github.com/googleapis/python-logging/compare/v3.11.3...v3.11.4) (2025-01-22)


### Bug Fixes

* Made `write_entries` raise `ValueError` on `ParseError`s ([#958](https://github.com/googleapis/python-logging/issues/958)) ([5309478](https://github.com/googleapis/python-logging/commit/5309478c054d0f2b9301817fd835f2098f51dc3a))
* Require proto-plus >= 1.25 for Python 3.13 ([#955](https://github.com/googleapis/python-logging/issues/955)) ([7baed8e](https://github.com/googleapis/python-logging/commit/7baed8e968f0bfa6abdbf0715dc43822f2fba8ba))
* Require proto-plus >= 1.25 for Python 3.13 ([#955](https://github.com/googleapis/python-logging/issues/955)) ([002b1fc](https://github.com/googleapis/python-logging/commit/002b1fcb395d77d94d7216560c30015b9aefca81))

## [3.11.3](https://github.com/googleapis/python-logging/compare/v3.11.2...v3.11.3) (2024-10-15)


Expand Down
2 changes: 1 addition & 1 deletion google/cloud/logging/gapic_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "3.11.3" # {x-release-please-version}
__version__ = "3.11.4" # {x-release-please-version}
6 changes: 5 additions & 1 deletion google/cloud/logging_v2/_gapic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

from google.protobuf.json_format import MessageToDict
from google.protobuf.json_format import ParseDict
from google.protobuf.json_format import ParseError

from google.cloud.logging_v2._helpers import entry_from_resource
from google.cloud.logging_v2.sink import Sink
Expand Down Expand Up @@ -151,7 +152,10 @@ def write_entries(
Useful for checking whether the logging API endpoints are working
properly before sending valuable data.
"""
log_entry_pbs = [_log_entry_mapping_to_pb(entry) for entry in entries]
try:
log_entry_pbs = [_log_entry_mapping_to_pb(entry) for entry in entries]
except ParseError as e:
raise ValueError(f"Invalid log entry: {str(e)}") from e

request = WriteLogEntriesRequest(
log_name=logger_name,
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/logging_v2/gapic_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "3.11.3" # {x-release-please-version}
__version__ = "3.11.4" # {x-release-please-version}
45 changes: 42 additions & 3 deletions google/cloud/logging_v2/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def _do_log(self, client, _entry_class, payload=None, **kw):

api_repr = entry.to_api_repr()
entries = [api_repr]

if google.cloud.logging_v2._instrumentation_emitted is False:
entries = _add_instrumentation(entries, **kw)
google.cloud.logging_v2._instrumentation_emitted = True
Expand Down Expand Up @@ -200,18 +201,38 @@ def log_text(self, text, *, client=None, **kw):
self._do_log(client, TextEntry, text, **kw)

def log_struct(self, info, *, client=None, **kw):
"""Log a dictionary message
"""Logs a dictionary message.
See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries/write
The message must be able to be serializable to a Protobuf Struct.
It must be a dictionary of strings to one of the following:
- :class:`str`
- :class:`int`
- :class:`float`
- :class:`bool`
- :class:`list[str|float|int|bool|list|dict|None]`
- :class:`dict[str, str|float|int|bool|list|dict|None]`
For more details on Protobuf structs, see https://protobuf.dev/reference/protobuf/google.protobuf/#value.
If the provided dictionary cannot be serialized into a Protobuf struct,
it will not be logged, and a :class:`ValueError` will be raised.
Args:
info (dict): the log entry information
info (dict[str, str|float|int|bool|list|dict|None]):
the log entry information.
client (Optional[~logging_v2.client.Client]):
The client to use. If not passed, falls back to the
``client`` stored on the current sink.
kw (Optional[dict]): additional keyword arguments for the entry.
See :class:`~logging_v2.entries.LogEntry`.
Raises:
ValueError:
if the dictionary message provided cannot be serialized into a Protobuf
struct.
"""
for field in _STRUCT_EXTRACTABLE_FIELDS:
# attempt to copy relevant fields from the payload into the LogEntry body
Expand Down Expand Up @@ -405,8 +426,22 @@ def log_text(self, text, **kw):
def log_struct(self, info, **kw):
"""Add a struct entry to be logged during :meth:`commit`.
The message must be able to be serializable to a Protobuf Struct.
It must be a dictionary of strings to one of the following:
- :class:`str`
- :class:`int`
- :class:`float`
- :class:`bool`
- :class:`list[str|float|int|bool|list|dict|None]`
- :class:`dict[str, str|float|int|bool|list|dict|None]`
For more details on Protobuf structs, see https://protobuf.dev/reference/protobuf/google.protobuf/#value.
If the provided dictionary cannot be serialized into a Protobuf struct,
it will not be logged, and a :class:`ValueError` will be raised during :meth:`commit`.
Args:
info (dict): The struct entry,
info (dict[str, str|float|int|bool|list|dict|None]): The struct entry,
kw (Optional[dict]): Additional keyword arguments for the entry.
See :class:`~logging_v2.entries.LogEntry`.
"""
Expand Down Expand Up @@ -451,6 +486,10 @@ def commit(self, *, client=None, partial_success=True):
Whether a batch's valid entries should be written even
if some other entry failed due to a permanent error such
as INVALID_ARGUMENT or PERMISSION_DENIED.
Raises:
ValueError:
if one of the messages in the batch cannot be successfully parsed.
"""
if client is None:
client = self.client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"language": "PYTHON",
"name": "google-cloud-logging",
"version": "3.11.3"
"version": "3.11.4"
},
"snippets": [
{
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test__gapic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import google.auth.credentials
import mock

from datetime import datetime

import google.cloud.logging
from google.cloud import logging_v2
from google.cloud.logging_v2 import _gapic
Expand Down Expand Up @@ -173,6 +175,21 @@ def test_write_entries_single(self):
assert request.entries[0].resource.type == entry["resource"]["type"]
assert request.entries[0].text_payload == "text"

def test_write_entries_parse_error(self):
client = self.make_logging_api()
with self.assertRaises(ValueError):
with mock.patch.object(
type(client._gapic_api.transport.write_log_entries), "__call__"
) as call:
entry = {
"logName": self.LOG_PATH,
"resource": {"type": "global"},
"jsonPayload": {"time": datetime.now()},
}
client.write_entries([entry])

call.assert_not_called()

def test_logger_delete(self):
client = self.make_logging_api()

Expand Down

0 comments on commit b4b777b

Please sign in to comment.