Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

docs(samples): Adding code samples for log reading #56

Merged
merged 11 commits into from
Oct 27, 2022
2 changes: 1 addition & 1 deletion samples/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest==7.2.0
google-cloud-compute==1.6.1
google-cloud-resource-manager==1.6.3
google-cloud-storage==2.5.0
google-cloud-storage==2.5.0
1 change: 1 addition & 0 deletions samples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
isort==5.10.1
black==22.10.0
google-cloud-batch==0.4.0
google-cloud-logging==3.2.5
39 changes: 39 additions & 0 deletions samples/snippets/logs/read_job_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# [START batch_job_logs]
from typing import NoReturn

from google.cloud import batch_v1
from google.cloud import logging


def print_job_logs(project_id: str, job: batch_v1.Job) -> NoReturn:
"""
Prints the log messages created by given job.

Args:
project_id: name of the project hosting the job.
job: the job which logs you want to print.
"""
# Initialize client that will be used to send requests across threads. This
# client only needs to be created once, and can be reused for multiple requests.
log_client = logging.Client(project=project_id)
logger = log_client.logger("batch_task_logs")

for log_entry in logger.list_entries(filter_=f"labels.job_uid={job.uid}"):
print(log_entry.payload)

# [END batch_job_logs]
14 changes: 11 additions & 3 deletions samples/snippets/tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ..get.get_task import get_task
from ..list.list_jobs import list_jobs
from ..list.list_tasks import list_tasks
from ..logs.read_job_logs import print_job_logs

PROJECT = google.auth.default()[1]
REGION = 'europe-north1'
Expand Down Expand Up @@ -82,11 +83,18 @@ def _check_tasks(job_name):
print('Tasks tested')


def test_script_job(job_name):
def _check_logs(job, capsys):
print_job_logs(PROJECT, job)
output = [line for line in capsys.readouterr().out.splitlines(keepends=False) if line != ""]
assert len(output) == 4
assert all(log_msg.startswith("STDOUT") for log_msg in output)


def test_script_job(job_name, capsys):
job = create_script_job(PROJECT, REGION, job_name)
_test_body(job, additional_test=lambda: _check_tasks(job_name))
_test_body(job, additional_test=lambda: _check_logs(job, capsys))


def test_container_job(job_name):
job = create_container_job(PROJECT, REGION, job_name)
_test_body(job)
_test_body(job, additional_test=lambda: _check_tasks(job_name))