1. Introduction
In this lab, you'll use Gemini, an AI-powered collaborator in Google Cloud, to troubleshoot a problem with Cloud Function deployment by analyzing error logs, identifying the root cause of the problem and finding how to fix it.
What you'll do...
- You'll use Cloud Shell Terminal and gcloud CLI to setup the environment including enabling relevant Google APIs and creating a Cloud Function from provided code
- You'll use Cloud Logging log summarization to generate log summaries in order to understand captured information.
- You'll chat with Gemini to get Gemini assistance in troubleshooting and resolving the problem.
What you'll learn...
- How to use Gemini with Google Cloud Observability and for problem troubleshooting.
- How to find and understand Cloud Function logs with help from Gemini.
What you'll need...
- Chrome web browser
- A Google Cloud project with billing enabled
- A Google account with access permissions to the project allowing you to enable APIs and manipulate resources
This lab is targeted to DevOps and platform engineers and software developers of all levels, including beginners. Our focus will be on getting hands-on experience with the capabilities of Gemini in troubleshooting.
2. Setup
This section covers everything you need to do to get started with this lab.
Configure environment
1. Log-in to Cloud Console by opening https://console.cloud.google.com.
2. Select a Google Cloud project that you plan to work with for this lab.
3. Open the Cloud Functions page in Console. You can do it by selecting Cloud Functions from the navigation menu (top left icon ☰ in Console window).
Alternatively, you can search for "cloud functions" in the search area of the console:
Click the Create Function button on top of the list of functions
If you did not use Cloud Functions in this project before, in order to work you will be asked to enable Google APIs.
Click ENABLE to continue.
4. Define basic properties of the new Cloud Function.
- Set the name to be "
codelab-cf
" - Select the Allow unauthenticated invocations option
To complete the step, click NEXT button at the bottom left area of the window:
After clicking NEXT you might be prompted to enable additional Google API(s). Like in step 4, click ENABLE.
Set up runtime and code of the Cloud Function
This CodeLab uses Python as a programming language. Do not worry if you are unfamiliar with Python. To complete this CodeLab you will not need knowledge of Python.
5. Select Python 3.11 as the function's Runtime
Note that the change of runtime changes the source code shown in Inline Editor.
6. Copy code below into Inline Editor to modify the auto-generate quickstart example.
from google.cloud import storage
import json
import re
client = storage.Client()
def get_object_list(request):
if request.args and 'path' in request.args:
path = request.args['path']
else:
return '{}'
parsed = re.search('gs:\/\/([a-zA-Z0-9_-]{3,63})\/([-a-zA-Z0-9_\+.\/]*)', path)
bucket, prefix = parsed.group(1), parsed.group(2)
blobs = client.list_blobs(bucket, prefix=prefix, delimiter='/')
objects = []
if (blobs):
for blob in blobs:
objects.append(blob.name)
return json.dumps(objects)
This code reads the path
attribute from the GET request, parses the path to pick up the bucket name and calls Google Cloud Storage API to get a list of objects stored at this path. The function (get_object_list
) accepts a request which is a Flask object as an input argument and returns the names of the objects as a JSON array.
7. Select the requirements.txt
file from the list of files left in Inline Editor. Replace the current list of dependencies with the new one by copying the code below into Inline Editor.
google-cloud-storage
8. To deploy Cloud Function, click DEPLOY at the bottom left.
You may need to wait a couple of minutes until the deployment process completes. If you followed setup instructions closely you should expect to see the deployment reported as failed.
3. Finding and resolving the problem
In this section you will use the Log explanations feature and other Gemini assistance to identify the problem and the way to resolve it.
1. The deployment fails. You should see an error message informing you about this with additional information and an option to see deployment logs. Click VIEW LOGS to view logs of the deployment.
NOTE that the deployment logs will be opened in a separate tab in the same browser. You will need to navigate between the tabs to complete this CodeLab.
NOTE that you may not see log lines when clicking VIEW LOGS or see only a couple of lines. In such an event, use the time-range selector in the query pane of the Log explorer and select Last 30 minutes.
2. Review the logs in the opened tab. Pay attention that log summaries look like multiple lines of the exception's callstack. It is because Cloud Function captures text printed to stdout
or stderr
and writes each line as a separate log entry. Other log lines summarize information about exit error code as well as additional information reported by Cloud Functions and Cloud Run APIs.
You can click on any of the lines to see more information about a particular log. The expanded logs have additional UI elements clicking on which allows you to copy all information about the log, expand all fields to display all data of the log entry and to explain the log entry using Gemini.
4. The exception callstack is not very informative. Look through the logs to find the end of the exception's stack trace log. It is the line saying "Container called exit(1)
". The following log summary says:
functions_framework.exceptions.MissingTargetException: File /workspace/main.py is expected to contain...
It looks like a good candidate for investigation. Click on this line to expand the log entry. Then click the "Explain this log entry" button to see additional information about the selected log.
If Gemini was not previously used in this project, you will be prompted to enable a required API. If you are prompted to enable the API, click ENABLE to move forward.
NOTE that occasionally you may get an error when prompting Gemini. On such an event, repeat your prompt operation one more time to get the response. For example, click the "Explain this log entry" button again.
5. Review explanation provided by Gemini. If the given explanation is not sufficient or unclear, prompt Gemini to provide more information by using one of the following prompts or composing your own question
Can you provide more information about this log?
Can you provide more information about the error?
6. Then prompt Gemini for suggestions to resolve the problem. For example ask Gemini
How to solve this problem?
Suggest a solution to resolve this error
Suggest how I can fix this error
Gemini response may vary depending on the current context and also the wording and format of your prompt. It is expected that Gemini will recommend to ensure that the source code of Cloud Function has a function with the name hello_http
.
7. You are currently seeing the tab that shows you Cloud Functions logs. Select the previous tab that shows the Cloud Function deployment page with the error and click EDIT.
8. Confirm that authentication is still set to Allow unauthenticated invocations and update the selection if necessary.
9. Click NEXT at the bottom to see Inline Editor. Follow the Gemini recommendation and change the function name from get_order_list
to hello_http
.
After finishing the edit click DEPLOY.
You may need to wait a couple of minutes until the deployment process completes. Confirm that the deployment completes successfully and no error messages appear.
NOTE that Console may continue showing you the last error message until the deployment process is complete.
10. Check that the Cloud Function is operational by sending the following HTTPS request using curl
. You can use Cloud Shell or do it from any terminal that has curl
and gcloud
CLI installed.executing the following command from the Cloud Shell Terminal.
curl -m 70 -X GET \
https://us-central1-${GOOGLE_CLOUD_PROJECT}.cloudfunctions.net/\
codelab-cf?path=gs://cloud-samples-data/generative-ai/image/ \
-H "Authorization: bearer $(gcloud auth print-identity-token)"
NOTE that to run this command from a terminal you will have to authenticate using gcloud CLI and to set the value of your project ID to the environment variable GOOGLE_CLOUD_PROJECT
.
Bonus
The name of the function hello_http
was defined as a default entry point for HTTP triggered Cloud Function when the Python runtime was selected. Alternative solution can be to keep get_order_list
as the name of the function in the source code and to update the entry point field of the Cloud Function that is located above Inline Editor to the right from the Runtime field:
Changing the entry point from the hello_http
to get_order_list
will resolve the problem with deployment.
4. Cleanup
To cleanup you can either shutdown the project or delete the Cloud Function instance. You can delete Cloud Functions using Console or with the CLI command like below:
gcloud functions delete codelab-cf --region=us-central1
5. Congratulations!
Congratulations - you've successfully used Gemini to troubleshoot and resolve the problem with your application and now understand how it can assist with understanding logs and getting answers to questions about Google Cloud.