Snapshot Debugger

Gunnala SreekanthReddy3 min read
Snapshot Debugger

The Problem:

Earlier we used cloud debugger ,a GCP product to put a debugger to debug the state of the application, but down the line one problem occurred.

Cloud Debugger is Deprecated.It wont be there after May,2023.

So Google has moved their Cloud Debugger to Snapshot Debugger ,which is not a open source project, to be maintained by end users.

Developers can add the debugger sources and agents in their project and use the Debugger.

Prerequisites:

To use the debugger locally you need these 4 things:

  1. A Spring boot project using gradle/maven.
  2. Gcloud cli and account.
  3. Python3/3.4 with pip.
  4. Firebase project with Realtime Database Enabled
  5. Firebase Admin Service Account Json key
  6. Linux/Ubuntu Machine/Subsystem.(It wont run on mac /windows)

Once you have all these we can proceed with the setup process.

Setup:

Configuring Firebase

  1. Go to Console.firebase.com and login with Google Account.
  2. Select your project
  3. Enable Realtime Database
  4. Now goto project Settings, then Service Account
  5. Download the admin key json File.

Configuring Gcloud

  1. Open Terminal and run gcloud auth login
  2. Login using same credentials that you used for firebase.
  3. Type the following command gcloud projects list 
  4. Check if the same firebase project is listed or not.If listed ,then correct Gcloud account is used.
  5. Now Run the following command gcloud config set project $MY_PROJECT_ID ,replace $MY_PROJECT_ID with your project name.
  6. GCloud Config is done

Installing and Configuring Snapshot Debug Cli

 To install Snapshot debugger Cli ,run the following command:

python3 -m pip install snapshot-dbg-cli

Once Installation is complete and you have made sure that correct firebase account is selected using gcloud cli,Run the following command,

snapshot-dbg-cli init --use-default-rtdb

Or

                         python3 -m snapshot-dbg-cli init --use-default-rtdb

This will initialize the snapshot debugger for your firebase account and create logs in your Realtime Database.

Such output should be expected

Project 'test-proj' is successfully configured with the Firebase Realtime

Database for use by Snapshot Debugger.

The full database information is below. If you have specified a custom database

ID the url below is the one you'll need to specify when using the other cli

commands.

  name:         projects/23498723497/locations/us-central1/instances/test-proj-cdbg

  project:      projects/23498723497

  database url: https://test-proj-cdbg.firebaseio.com

  type:         USER_DATABASE

  state:        ACTIVE

Here take a note for the database url, because we will need it later.

Agent setup 

  1. Create a folder named debugger inside “src” folder
  2. Download the following tar file and unzip it inside the debugger folder.

https://github.com/GoogleCloudPlatform/cloud-debug-java/releases/latest/download/cdbg_java_agent_gce.tar.gz 

  1. Also add your admin json file inside this folder for testing(we can remove it later)
  2. In service build. gradle, go to JIB
  3. Now add/replace the following

extraDirectories.paths = ['src/debugger']

    container {

        creationTime = 'USE_CURRENT_TIMESTAMP'

        ports = ['50061', '50062']

       // environment = ["FIREBASE_ADMIN_SDK_KEY":"/opt/cdbg/admin.json"]

        entrypoint=["sh", "-c",

                    "java -Djava.security.egd=file:/dev/urandom " +

                            "-agentpath:./opt/cdbg/cdbg_java_agent.so " +

                            "-Dcom.google.cdbg.module=starter-server " +

                            "-Dcom.google.cdbg.version=${scmVersion.version.toString()} " +

                            "-Dcom.google.cdbg.agent.use_firebase=True " +

                            "-Dcom.google.cdbg.auth.serviceaccount.jsonfile=\$FIREBASE_ADMIN_SDK_KEY " +

                            "-Dcom.google.cdbg.agent.firebase_db_url=https://unifiedplatform-dev.firebaseio.com/ "+

                            "-cp /app/resources:/app/classes:/app/libs/* " +

                            "org.heartfulness.unifiedplatform.volunteer.application.VolunteerApplicationApp"]

        mainClass = 'org.heartfulness.unifiedplatform.volunteer.application.VolunteerApplicationApp'

    }

Now when you will build Docker image ,all the files from the debugger folder will be copied into /opt/cdbg folder inside the container.

For testing purpose ,you can change the following line

-Dcom.google.cdbg.auth.serviceaccount.jsonfile=\$FIREBASE_ADMIN_SDK_KEY

To

-Dcom.google.cdbg.auth.serviceaccount.jsonfile=/opt/cdbg/firebase_admin.json’ 

Now try running the docker container locally using docker hub desktop.If everything is configured properly,it will start .You will notice a new entry ”Debugger” in firebase Realtime database .

If everything is working fine,you can revert the changes and rebuild the docker container.

Platform GCP DevOps Settings

Add the following lines to your deployment.yml(Make Sure indentation is correct)

         Env:

            - name: FIREBASE_ADMIN_SDK_KEY

              value: "/var/run/secrets/firebase-admin-sdk/key.json"

          volumeMounts:   

            - name: firebase-admin-sdk-key

              mountPath: /var/run/secrets/firebase-admin-sdk/

          volumes:

 - name: firebase-admin-sdk-key

   secret:

      secretName: firebase-admin-sdk-key

Ready to build something that matters?

We solve problems that don't have Stack Overflow answers. Let's talk.

Book a Discovery Call