Jump to content

Google Cloud Logging with the Rust SDK and Gemini CLI

From JOHNWICK
Revision as of 04:34, 21 November 2025 by PC (talk | contribs) (Created page with "This article leverages the Gemini CLI and the underlying Gemini LLM to develop native compiled code built in the Rust Language for Application Logging to Google Cloud. A fully functional logging client is developed in Rust for testing remote logging to Google Cloud Logging using the official Google Cloud Rust SDK. What is Rust? Rust is a high performance, memory safe, compiled language: Rust A language empowering everyone to build reliable and efficient software. www.rus...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This article leverages the Gemini CLI and the underlying Gemini LLM to develop native compiled code built in the Rust Language for Application Logging to Google Cloud. A fully functional logging client is developed in Rust for testing remote logging to Google Cloud Logging using the official Google Cloud Rust SDK. What is Rust? Rust is a high performance, memory safe, compiled language: Rust A language empowering everyone to build reliable and efficient software. www.rust-lang.org

Rust provides memory safe operations beyond C/C++ and also can provide exceptional performance gains as it is compiled directly to native binaries. Initial Environment Setup The environment is meant to be run from a Bash like shell. You can run this from a Linux VM, ChromeOS Linux VM, Firebase Studio environment, or any environment that provides a basic shell. Rust Setup Instructions to install Rust are available here: Getting started A language empowering everyone to build reliable and efficient software. www.rust-lang.org

For a Linux like environment the command looks like this: curl — proto ‘=https’ — tlsv1.2 -sSf https://sh.rustup.rs | sh Gemini CLI If not pre-installed you can download the Gemini CLI to interact with the source files and provide real-time assistance: sudo npm install -g @google/gemini-cli Note- if you are an a non standard environment — you will need to make sure to have at least Node version 20 available in order to run Gemini CLI. Testing the CLI Environment from the VM Once you have all the tools and the correct Node.js version in place- you can test the startup of Gemini CLI. You will need to authenticate with a Key or your Google Account: gemini

Google Cloud Logging As part of the Google Cloud platform — extensive logging options are provided. Both system level and application level logs can be created. A summary of Google Cloud Logging is here: Cloud Logging documentation | Google Cloud A fully managed service that allows you to store, search, analyze, monitor, and alert on logging data. cloud.google.com

Official Google Cloud Rust SDK Google Cloud provides a supported version of the Rust SDK. This SDK allows making client calls in Rust to Google Cloud Endpoints natively from Rust code: GitHub — googleapis/google-cloud-rust: Google Cloud Platform Rust Client Libraries Google Cloud Platform Rust Client Libraries. Contribute to googleapis/google-cloud-rust development by creating an… github.com

A sample getting started guide is available here: Google Cloud Client Libraries for Rust The Google Cloud Client Libraries for Rust is a collection of Rust crates to interact with Google Cloud Services. This… googleapis.github.io Rust Deployment Strategy Rust has not been the typical deployment path for applications using Google Cloud— so a step by step approach is used with validation at each milestone.

First Steps — Basic Google API Client Call in Rust

The first step is to build a minimal Rust sample that makes a Google Cloud API Client call. The most common pattern for using the Google Cloud Rust SDK is to create a client- then build the arguments matching the correct types, and finally perform the client SDK call to the Google Cloud backend service.

Clone the Samples Repo and Setup the Basic Environment Now that the Gemini CLI and the Rust installation has been validated from the base shell — you can clone the GitHub Repo with support scripts:

cd ~ git clone https://github.com/xbill9/gemini-cli-codeassist

Then run init.sh from the cloned directory. The script will attempt to determine your shell environment and set the correct variables:

cd gemini-cli-codeassist source init.sh

Set application default credentials for authentication:

gcloud auth application-default login

Debugging API Permission Errors If your application default credentials expires or your Google Cloud Authentication expires you will get an error similar to this:

Finished `dev` profile [unoptimized + debuginfo] target(s) in 27.94s

    Running `target/debug/logging-client-rust`

Starting client API call Project comglitn Error: Error { kind: Authentication, source: Some(CredentialsError { is_transient: false, message: Some("Request to fetch the token failed. Subsequent calls with this credential will also fail."), source: Some(Error { kind: Authentication, source: Some(CredentialsError { is_transient: false, message: Some("failed to refresh user access token, body=<{\n \"error\": \"invalid_grant\",\n \"error_description\": \"reauth related error (invalid_rapt)\",\n \"error_uri\": \"https://support.google.com/a/answer/9368756\",\n \"error_subtype\": \"invalid_rapt\"\n}>"), source: Some(reqwest::Error { kind: Status(400, None), url: "https://oauth2.googleapis.com/token" }) }) }) }) }

The workaround is to re-authenticate:

gcloud auth login gcloud auth application-default login

Another common error is that the environment variables are not set correctly. Go the the root directory and re-run the set_env.sh to set the variables:

cd ~/gemini-cli-codeassist source set_env.sh

Rust Debugging and Logging The environment variable RUST_LOG controls how verbose the logging from the Rust code will produce messages. Initially — set the variable to info:

export RUST_LOG=info echo $RUST_LOG info

Rust Log Levels These levels filter messages based on their severity, from most critical to least:

  • ERROR: Only error messages are logged.
  • WARN: Warning messages and higher (error) are logged.
  • INFO: Informational messages and higher (warn, error) are logged.
  • DEBUG: Debug messages and higher (info, warn, error) are logged.
  • TRACE: Trace messages and higher (debug, info, warn, error) are logged. This is the most verbose level.
  • OFF: Disables all logging.

Next Steps- Build Rust Sample Client Code A basic native Rust client sample the makes a direct connection to Google Cloud Logging has been included in the GitHub repo. Change to the sample directory and validate the Cargo.toml file for the minimal sample:

cd ~/gemini-cli-codeassist/logging-client-rust pwd /home/xbill/gemini-cli-codeassist/logging-client-rust

The Rust standard format file Cargo.toml contains details about the code:

head Cargo.toml [package] name = "logging-client-rust" version = "0.2.0" edition = "2024" description = "Minimal Rust logging client call to GCP" license = "MIT" repository = "https://github.com/xbill9/gemini-cli-codeassist"

Build the GCP Logging client sample with the Makefile:

make release Building Release...

   Finished `release` profile [optimized] target(s) in 0.15s

xbill@penguin:~/gemini-cli-codeassist/logging-client-rust$

Then run the generated Rust binary:

export RUST_LOG=info export PROJECT_ID=glitnir-dev ./target/release/logging-client-rust

At the info logging level — basic information is returned about the cloud logging:

INFO logging_client_rust > (main) Starting up! WARN logging_client_rust > (main) This is a warning message. INFO logging_client_rust > Structured log entry written successfully to: projects/glitnir-dev/logs/my-rust-app

Validate the Log Write Go to the Google Cloud console and search on Logs:

500px

Then drill into the Logs Explorer:

Use Gemini CLI to Validate the Logs Gemini CLI can now use the compiled tool to check the log messages:

You can also use the result of the logging-client-rust Tool within Gemini CLI to verify the Google Cloud logs:

File:Google cloud logging 2.jpg

Deep Dive with Rust Logging Update the RUST_LOG environment variable to a more granular level to get deeper visibility into the Rusk SDK client connection :

export RUST_LOG=debug echo $RUST_LOG debug

Now re-run the logging SDK call with the new Rust log level:

./target/release/logging-client-rust

INFO  logging_client_rust > (main) Starting up!
DEBUG logging_client_rust > (main) Project ID: glitnir-dev
DEBUG logging_client_rust > (main) Log Name: my-rust-app
WARN  logging_client_rust > (main) This is a warning message.
DEBUG reqwest::connect    > starting new connection: https://oauth2.googleapis.com/
DEBUG reqwest::connect    > starting new connection: https://oauth2.googleapis.com/
DEBUG rustls::client::hs  > No cached session for DnsName("oauth2.googleapis.com")
DEBUG rustls::client::hs  > Not resuming any session
DEBUG rustls::client::hs  > No cached session for DnsName("oauth2.googleapis.com")
DEBUG rustls::client::hs  > Not resuming any session
DEBUG rustls::client::hs  > Using ciphersuite TLS13_AES_256_GCM_SHA384
DEBUG rustls::client::tls13 > Not resuming
DEBUG rustls::client::tls13 > TLS1.3 encrypted extensions: ServerExtensions { selected_protocol: SingleProtocolName(ProtocolName(687474702f312e31)), unknown_extensions: {}, .. }
DEBUG rustls::client::hs    > ALPN protocol is Some(b"http/1.1")
DEBUG rustls::client::hs    > Using ciphersuite TLS13_AES_256_GCM_SHA384
DEBUG rustls::client::tls13 > Not resuming
DEBUG rustls::client::tls13 > TLS1.3 encrypted extensions: ServerExtensions { selected_protocol: SingleProtocolName(ProtocolName(687474702f312e31)), unknown_extensions: {}, .. }
DEBUG rustls::client::hs    > ALPN protocol is Some(b"http/1.1")
DEBUG rustls::common_state  > Sending warning alert CloseNotify
DEBUG rustls::common_state  > Sending warning alert CloseNotify
DEBUG reqwest::connect      > starting new connection: https://logging.googleapis.com/
DEBUG rustls::client::hs    > No cached session for DnsName("logging.googleapis.com")
DEBUG rustls::client::hs    > Not resuming any session
DEBUG rustls::client::hs    > Using ciphersuite TLS13_AES_256_GCM_SHA384
DEBUG rustls::client::tls13 > Not resuming
DEBUG rustls::client::tls13 > TLS1.3 encrypted extensions: ServerExtensions { selected_protocol: SingleProtocolName(ProtocolName(687474702f312e31)), unknown_extensions: {}, .. }
DEBUG rustls::client::hs    > ALPN protocol is Some(b"http/1.1")
INFO  logging_client_rust   > Structured log entry written successfully to: projects/glitnir-dev/logs/my-rust-app
DEBUG rustls::common_state  > Sending warning alert CloseNotify

xbill@penguin:~/gemini-cli-codeassist/logging-client-rust$

Package Information on crates.io The complete logging-client-rust is available on the Rust crates.io site: crates.io: Rust Package Registry Edit description crates.io


Summary A complete Rust native logging client was built and tested using the Gemini CLI along with Rust libraries for a direct connection to Google Cloud Logging. This compiled Rust code was then called directly from the local shell and performed logging client operations over the Google Cloud SDK for Rust. Then, Gemini CLI was used to validate the cloud logs and several Rust logging/debugging techniques were presented.

The overall goal was to validate the Rust client SDK libraries and the environment needed to successfully connect to Google Cloud from Rust native compiled code.

Read the full article here: https://medium.com/google-cloud/google-cloud-logging-with-the-rust-sdk-and-gemini-cli-cc49cf28c526