Google Cloud Platform Stackdriver Logging & Monitoring v2 with Docker

Jared Messenger
Ten Dozen
Published in
3 min readNov 24, 2016

If you’ve recently logged into your Google Cloud Platform, you were probably greeted with the notice,

“In the next few weeks the Logs Viewer will migrate to the Stackdriver Logging v2 API. Learn more”.

Google Cloud Platform Stackdriver Logging

This politely tells us that their documentation is going to be littered with artifacts of old and new libraries and fragmented snippets of code. It also means their documentation will only cover the most basic installation:

Vanilla Installation from copied from Google Docs:

curl -O https://repo.stackdriver.com/stack-install.sh
sudo bash stack-install.sh --write-gcm
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh

If you’re like me, lazy, hoping for a quick fix, you copied/pasted the 4 lines into a Dockerfile RUN command and were unfortunately greeted with errors while building. This is because the engineers at Google tried to make the installation as simple as possible (it would’ve worked if we were using a fresh new compute engine instance).

If you open those *.sh files up you can see they’re just simply adding source repositories and then running apt-get. So let’s add the source repositories to the Dockerfile and add the packages to the apt-get command.

If you’re running the Docker container on your localhost, you will need to create a service account that has permissions to write to the logs.

IAM & Admin > Service Accounts > Create Service Account

Google Cloud Platform IAM, Service accounts
Google Cloud Platform permission to write to logs

In the Dockerfile gist (above) you will see the last line is to copy and paste this json private key. Make sure you edit the Dockerfile to point to this private key. When running the Docker container, you will also need to set an environment variable to point to this private key.

docker run -d -p 80:80 -p 443:443 --name my-container --add-host=dockerhost:10.0.2.2 -e GOOGLE_APPLICATION_CREDENTIALS="some-dir/google-developer.key" -it yourname/buildname:buildtag

Finally the fun part, actually logging custom data in Python:

from google.cloud import logginglogging_client = logging.Client()# Create logger
logger = logging_client.logger("my-logger")
# Simple text
logger.log_text("Hello world")
# Log Error
logger.log_text("Oh no!", severity="ERROR")

For more in-depth examples on logging, please refer to the documentation.

Lastly, to view these new logs, select “Custom Logs” in the first drop down. In the “next few weeks” this will change to Stackdriver’s UI.

Hopefully you’re up and running. Play around with Stackdriver when it switches over and don’t forget to create an uptime check and an alerting policy.

--

--

Jared Messenger
Ten Dozen

Jared Messenger is an Angel Investor and iOS engineer. Currently he is focused on computer vision and machine learning models for mobile devices.