Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Setting up Cloud Debugger for Java
2.1.
Setting up Cloud Debugger
2.2.
Canary snapshots and logpoints
2.3.
Enabling canary snapshots and logpoints
2.3.1.
When to enable canary snapshots and logpoints
2.3.2.
When to disable canary snapshots and logpoints
2.4.
App Engine Flexible Environment
2.5.
Google Kubernetes Engine
2.6.
Compute Engine
2.6.1.
Tomcat
2.7.
Cloud Run and Cloud Run for Anthos
3.
Debug Snapshots
3.1.
Snapshots
3.2.
Debugger agent with canarying
3.3.
Debugger agent without canarying
3.4.
Take a debug snapshot
3.5.
Expressions
3.6.
To view a snapshot
3.7.
Troubleshooting
3.7.1.
Has the debugger agent crashed any of the instances?
4.
Debug Logpoints
4.1.
Logpoints
4.2.
Debugger agent with canarying
4.3.
Debugger agent without canarying
4.4.
Adding a debug logpoint
4.5.
Logpoint message format
4.6.
Logpoint Condition
5.
Logs Panel
5.1.
Search by text
5.2.
Additional filter/view options
6.
Frequently Asked Questions
6.1.
Mention the Java versions for which the cloud debugger is available.
6.2.
What are the requirements for compute engine VM instances to be used with a cloud debugger?
6.3.
Is StringBuilder.append() supported by snapshot conditions?
7.
Conclusion
Last Updated: Mar 27, 2024

Basic Concepts of Cloud Debugger

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Cloud Debugger is also known as Stackdriver Debugger. It lets the user inspect the state of a running cloud application at any code location without stopping or slowing it down. It's safe for use with the production apps. When a debug snapshot is captured by the Java debugger agent, it adds less than 10ms to the request latency. This blog will discuss the basic concepts that are used in Cloud Debugger.

Setting up Cloud Debugger for Java

For some environments, the user must explicitly specify the access scope in order to let the cloud debugger agent send data. It is recommended to set the broadest possible access scope and then use Identity and Access Management to restrict access.

Setting up Cloud Debugger

Follow the below-mentioned steps in order to set up a cloud debugger.

  • Make sure that Cloud Debugger API is enabled on the project.
  • On the compute environment that is currently being used, install as well as configure the Debugger.
  • Select the source code.

Canary snapshots and logpoints

Enable the canary mode for the debugger agent to prevent snapshots and logpoints from being loaded on all the running instances simultaneously and causing the job to be taken down because of a potential bug in the debugger agent. A snapshot or logpoint is applied to only a subset of the running instances . The snapshot or logpoint doesn't affect the instances when the carry mode is enabled is verified by the Debugger. The snapshot or the logpoint is applied to all the instances once the verification is complete.

Enabling canary snapshots and logpoints

The user gets the option of enabling or disabling the canary when the latest version of the debugger agent is installed. By default, canarying is disabled.

When to enable canary snapshots and logpoints

If the user wishes to protect deployment and production-critical workloads, then the user should enable canarying when debugging these workloads. If a user has a single instance, the user can still debug with canarying enabled, but then the single instance runs without canarying the snapshot or logpoint.

When to disable canary snapshots and logpoints

The user shouldn't enable canarying on workloads that have an execution time of less than 40 seconds. If the user wants a faster snapshot-triggering cycle, then also the user shouldn't enable canarying. 

App Engine Flexible Environment

No configuration is required as the Debugger is enabled by default for the Java runtime. The Java source files that have been used to build the app are displayed by the Debug page in the console.

A debugger for custom runtimes that uses the Google-provided base images for Java is included by default. If the default entry point is used, then no configuration is required. The Java source files that have been used to build the app are displayed by the Debug page in the console. If the user wants to use Cloud debugger with custom runtimes built using other base images, the user needs to follow the setup instructions for Compute Engine.

Google Kubernetes Engine

Follow the below-mentioned steps to enable Debugger using gcloud:

  • The user can create their cluster using one of the below-mentioned access scopes:
    • https://www.googleapis.com/auth/cloud-platform grants the user's cluster access to all of the Google Cloud APIs.
    • https://www.googleapis.com/auth/cloud_debugger grants the user's cluster access to only the debugger API.
To add the debugger agent to the containerized app and initialize it when the app is deployed, add the below-mentioned lines to the Dockerfile.
# Create a directory for the Debugger. Add and unzip the agent in the directory.
RUN mkdir /opt/cdbg && \
    wget -qO- https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezy/cdbg_java_agent_gce.tar.gz | \
    tar xvz -C /opt/cdbg
  • To run the Debugger agent, add the below-mentioned lines to the Dockerfile
If the user wants to debug with canarying enabled
# Start the agent when the app is deployed.
RUN java -agentpath:/opt/cdbg/cdbg_java_agent.so \
-Dcom.google.cdbg.module=MODULE \
-Dcom.google.cdbg.version=VERSION \
  -Dcom.google.cdbg.breakpoints.enable_canary=true \
  -jar PATH_TO_JAR_FILE
  • If the user wants to debug with canarying NOT enabled, set the enable_canary flag to false:
-Dcom.google.cdbg.breakpoints.enable_canary=false

Assign the placeholder with the below-mentioned:

PATH_TO_JAR_FILE: it refers to the relative path to the app's JAR file.

Module: it is the name of the app.

VERSION: it is the app version or the build ID.

Compute Engine

The user can use the cloud debugger with any Java app that is running on a Google Compute Engine instance. It is recommended to enable the Debugger on all of the running instances of the app.

  • The user must ensure that the Compute Engine VM instances are running on:
    • 64-bit Debian Linux image
    • Java JDK version 7, 8 or 9
  • The user needs to ensure that the compute engine VM instances that are created must have the access scope option to Allow full access to all Cloud APIs.
To get the pre-built agent package, use the below-mentioned command:
# Create a directory for the Debugger. Add and unzip the agent in the directory.
sudo mkdir /opt/cdbg
wget -qO- https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezy/cdbg_java_agent_gce.tar.gz | \
tar xvz -C /opt/cdbg
  • Add the agent to the Java invocation:
  • To debug with canarying enabled:
# Start the agent when the app is deployed.
java -agentpath:/opt/cdbg/cdbg_java_agent.so \
-Dcom.google.cdbg.module=MODULE \
-Dcom.google.cdbg.version=VERSION \
-Dcom.google.cdbg.breakpoints.enable_canary=true \
-jar PATH_TO_JAR_FILE
  • To debug with canarying disabled:
-Dcom.google.cdbg.breakpoints.enable_canary=false


Tomcat

Add this line to either of the below mentioned:

/etc/default/tomcat7 or /etc/default/tomcat8


To debug with canarying enabled

# Start the agent when the app is deployed.
JAVA_OPTS="${JAVA_OPTS} -agentpath:/opt/cdbg/cdbg_java_agent.so \
    -Dcom.google.cdbg.module=MODULE \
    -Dcom.google.cdbg.version=VERSION \
    -Dcom.google.cdbg.breakpoints.enable_canary=true"


To debug with canarying disabled:

-Dcom.google.cdbg.breakpoints.enable_canary=false


Jetty

Add the lines to below mentioned

/var/lib/jetty/start.d


To debug with canarying enabled

--exec
-agentpath:/opt/cdbg/cdbg_java_agent.so \
-Dcom.google.cdbg.module=MODULE \
-Dcom.google.cdbg.version=VERSION \
-Dcom.google.cdbg.breakpoints.enable_canary=true


To debug with canarying disabled

-Dcom.google.cdbg.breakpoints.enable_canary=false

Cloud Run and Cloud Run for Anthos

  • To create a directory in for installing the debugger agent, download the debugger agent archive, and extract it into the installation directory: add the following mentioned commands to the Dockerfile:
# Create a directory for the Debugger. Add and then unzip the agent in the directory.
RUN mkdir /opt/cdbg && \
    wget -qO- https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezycdbg_java_agent_gce.tar.gz | \
    tar xvz -C /opt/cdbg

 

If the user wishes to install a previous version of the agent, then change the URL to the below-mentioned value and replace the VERSION_NUMBER with the version of the agent that you want to use:

https://storage.googleapis.com/cloud-debugger/archive/java/VERSION_NUMBER/cdbg_java_agent_gce.tar.gz


After locating the Java invocation, add the following flag to initialize the debugger agent. To debug with canarying enabled:

# Start the agent when the app is deployed.
RUN java -agentpath:/opt/cdbg/cdbg_java_agent.so \
    -Dcom.google.cdbg.breakpoints.enable_canary=true \
    -jar PATH_TO_JAR_FILE


If you want to debug with canarying disabled:

-Dcom.google.cdbg.breakpoints.enable_canary=false
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Debug Snapshots

Once the app is deployed or is running, the user can open the Cloud Debugger in the google cloud console. The Debugger allows the user to capture and inspect the call stack as well as the local variables in the app without having to stop or slow it down.

Snapshots

They capture local variables as well as the call stack at a specific line location in the app's source code. The user has the ability to specify certain conditions and locations to return the snapshot of the app's data and view it in detail.

Debugger agent with canarying

Once the user has set a snapshot, the debugger agent then tests the snapshot on a subset of the instances. Once the debugger agent verifies, then the snapshot can execute successfully. The snapshot gets applied to all the instances. This process takes around 40 seconds. The results can be viewed in the Variables and Call Stack panes after the process when the snapshot is triggered. If the snapshot is triggered within then 40 seconds after it is set up, then the user sees the results from those instances that had the canary snapshot applied.

Several strategies are implemented by the debugger agent to minimize the amount of latency caused when capturing the data.

Debugger agent without canarying

If the user uses the debugger agent without the canarying and sets a snapshot, then it gets applied to all of the running instances of the app. A snapshot is taken by the Debugger, which is made available for viewing when any instance executes the code for the first time at the snapshot location. Several strategies are implemented by the debugger agent to minimize the amount of latency caused when capturing the data.

Take a debug snapshot

To take a snapshot, click on that particular snapshot source code line number.

  • The user must ensure that the Snapshot panel is selected.
  • Choose the file that has the source code. The center panel will display the contents of the selected file.
  • Click on the line number of the source code location.


The user can select multiple lines to set more than one snapshot location on a file. If in case no source code is available, then the user can enter the filename:line manually to take a snapshot in the Snapshot panel.

Expressions

The Debugger's expressions feature allows the user to evaluate complex expressions or traverse object hierarchies when a snapshot is taken. 

The use of expressions is optional, though some of the uses for expressions are mentioned below:

  • To view those static and global variables which are not part of the local variable set.
  • To view the deeply nested member variables without expanding the local variable in the debugger panel every time.
  • To help avoid repetitive mathematical calculations.
     

To add an expression, follow the below-mentioned steps:

  • Use the expression field to enter an expression. Multiple expressions can be added.
  • Click on Retake

To view a snapshot

When the source code is executed by the app at the location specified by the user, then the snapshot data appears in the Debugger. The variables section of the panel displays the instance variables and the local variables. The Call Stack section of the panel displays the stack trace.

The user can examine the value of the local variables at the moment when that snapshot was taken. The user can even examine the local variables at any level of the stack by clicking on any call stack frame.

Click and expand the Snapshot History panel if multiple snapshot locations on a file are set up or to view snapshots that have already been taken.

Troubleshooting

Has the debugger agent crashed any of the instances?

There are chances that the user might get false positives that make it appear as if the Debugger agent has triggered its safeguards because of the following reasons:

  • Instance shutdown while the debugger agent was canarying.
  • If an instance with a canary snapshot applied to it gets shut down, it will appear as if the debugger agent terminated the instance. Verify that no instances were shut down within 40 seconds after the snapshot was set.


The user should remove the debugger agent from the instances and reinstall the previous version. 

Debug Logpoints

Once the application is deployed or is running, the user can open a cloud debugger in the google cloud console. The cloud debugger allows the user to inject logging into the running services without having to restart or interfering with the normal function of the service.

Logpoints

It allows the user to inject logging into the running services without having to restart or interfere with the normal functions of the service. The cloud debugger logs a message every time any instance executes code at the logpoint location. The output is sent to the appropriate log for the target's environment.

They remain active for 24 hours after the creation or unless they are deleted, or the service is redeployed. If the user places a logpoint on a line that receives a lot of traffic, then the Debugger throttles the logpoint in order to reduce its impact on the application.

Debugger agent with canarying

Once the logpoint is set up, then the debugger agent tests the logpoint on a subset of the instances. After the agent verifies, the logpoint can execute successfully, and the logpoint is applied to all the instances. This process takes about 40 seconds. The debugger agent logs the message when the logpoint is triggered after the process. The debugger agent logs the message from those instances that had the canary logpoint applied to it if the logpoint is triggered within the 40 seconds after setting it up.

Debugger agent without canarying

When the user uses the debugger agent without canarying and sets a logpoint, it then applies to all of the running instances of the app. The debugger agent logs a message when for the first time, any instance executes the code at the logpoint location. 

Adding a debug logpoint

To add a logpoint from the console, use the below-mentioned steps:

  • The user must ensure that the Logpoint tab is selected in the right panel.
  • From the left panel, choose those files that contain the source code where a logpoint is to be added. The center panel displays the contents of the file.
  • Select the line number at which the logpoint is to be added and then select Create Logpoiny.
  • Enter a message in between the logpoint("”) field and click on the add button.

Logpoint message format

What gets logged in the output depends on the message of a logpoint. Expressions allow the user to evaluate and log values of interest. Whatever is in the message between the braces, such as {a+b}, is replaced with the value of the expression in the output.

Most of the Java expressions are supported, including the below-mentioned:

  • Local variables: x==2
  • Numerical & Boolean operation: a+b<10
  • String comparisons: tempString == “xyz”
  • Function calls. It allows only read-only functions. For example, StringBuilder.indexOf() is supported, whereas StringBuilder.append() isn’t. 

Logpoint Condition

It is a simple expression that must evaluate to be true for the logpoint to be logged. It is in the application language. They are evaluated each time the line is executed until the logpoint expires or gets deleted.

The condition needs to be a full boolean expression. It can include logical operators:

testString == "dog"
answer <= 10
testString == "dog" && answer <= 10

Logs Panel

Quick access to log messages through an in-page logs panel is available on the Debug page, it allows the user to see log messages next to the source code.

To expand the panel and view the file contents click on the Logs button.

Search by text

It allows the user to search through all of the available logs with a text search. The logs get filtered as the user types, allowing the user to determine if any logs match the query quickly. The text entered in the search field is treated as an exact string. If the user wants to make an AND search for multiple terms, then simply press the Enter between each string.

Additional filter/view options

Click on the more actions menu next to any log entry if the user wants to access additional filter and view options.

Filter by request: it returns all the log entries that are generated by the same request.

Filter by file: it returns all the log entries that belong to the same file, irrespective of request or line number.

Show in context: it highlights the selected log entry and returns an unfiltered view, allowing the user to see the entry in context with preceding and following requests.

Show in Logs Explorer: it opens the log entry on the Logs page and displays detailed information about the request.

Frequently Asked Questions

Mention the Java versions for which the cloud debugger is available.

The cloud debugger is available for Java versions 7,8,9 and 11.

What are the requirements for compute engine VM instances to be used with a cloud debugger?

The required conditions are:

  • A 64-bit Debian Linux image
  • Java JDK version 7, 8, or, 9

Is StringBuilder.append() supported by snapshot conditions?

No, it supports only read-only function calls.

Conclusion

In this article, we have extensively discussed the basic level concepts of Cloud Debugger.

After reading about the basic level concepts of Cloud Debugger,  are you not feeling excited to read/explore more articles on Google Cloud? Don't worry; Coding Ninjas has you covered. To learn about GCP certification: Google Cloud Platform, the difference between AWS, Azure & Google Cloud, and which platform is best: AWS vs. Google Cloud.

If you wish to enhance your skills in Data Structures and AlgorithmsCompetitive ProgrammingJavaScript, etc., you should check out our Guided path column at Coding Ninjas Studio. We at Coding Ninjas Studio organize many contests in which you can participate. You can also prepare for the contests and test your coding skills by giving the mock test series available. In case you have just started the learning process, and your dream is to crack major tech giants like Amazon, Microsoft, etc., then you should check out the most frequently asked problems and the interview experiences of your seniors that will surely help you in landing a job in your dream company. 

Do upvote if you find the blogs helpful.

Happy Learning!

Live masterclass