Introduction: The Importance of Application Monitoring

Welcome to the next step in your GCP developer journey! So far, you have learned how to keep your applications secure by managing credentials and protecting your APIs. Now, we will focus on another critical aspect: monitoring your applications.

Monitoring is essential for both security and reliability. It helps you answer questions like:

  • Is my application working as expected?
  • Are there any unusual patterns or errors?
  • How is my application performing over time?

Google Cloud Monitoring (formerly Stackdriver) is a service that helps you collect, view, and analyze data from your applications. In this lesson, you will learn how to send your own custom data (called metrics) to Cloud Monitoring using OpenTelemetry, an industry-standard observability framework. You'll also learn how to create dashboards to visualize this information. By the end, you will be able to track important numbers from your application and see them in real time.

Quick Recall: Using Google Cloud Client Libraries with GCP Services

Before we dive in, let's quickly remind ourselves about Google Cloud client libraries. In previous lessons, you used GCP client libraries to interact with services like Secret Manager and Cloud Functions. These are the official Google Cloud SDKs for Python, and they allow you to connect to GCP services directly from your code.

For monitoring specifically, you can use the official google-cloud-monitoring library:

However, in this lesson, we'll use OpenTelemetry to send metrics. OpenTelemetry is a vendor-neutral standard for observability that works with many cloud platforms, including GCP. It provides a cleaner API and makes your code more portable.

Understanding OpenTelemetry and Cloud Monitoring

OpenTelemetry is an open-source framework that provides a standardized way to collect metrics, logs, and traces from your applications. Instead of using vendor-specific APIs, you write code once using OpenTelemetry, and it can send data to various backends, including Google Cloud Monitoring.

Benefits of using OpenTelemetry:

  • Vendor-neutral: Your code isn't locked to a specific cloud provider
  • Cleaner API: Simpler, more intuitive code
  • Industry standard: Widely adopted across the industry

Let's visualize how the pieces fit together:

This diagram shows the complete flow:

  1. Your Python application uses the OpenTelemetry API to create and record metrics
  2. The OpenTelemetry SDK processes these metrics and exports them
  3. Cloud Monitoring receives and stores the metrics
  4. A dashboard displays the metrics visually for monitoring

Note: In this practice environment, we use the OTLP (OpenTelemetry Protocol) exporter which sends metrics to a local collector. This collector then forwards metrics to Cloud Monitoring. In production GCP environments, you can use either OTLP or the direct Cloud Monitoring exporter. Both approaches are industry-standard and widely used.

Now let's implement this flow step by step.

Setting Up OpenTelemetry for Cloud Monitoring

Before we can emit metrics, we need to set up OpenTelemetry to work with Google Cloud Monitoring.

Step 1: Import Required Libraries

First, import the libraries you will use:

  • opentelemetry.metrics provides the core metrics API
  • MeterProvider manages the creation of meters (which create metrics)
  • PeriodicExportingMetricReader sends metrics to Cloud Monitoring at regular intervals
  • OTLPMetricExporter exports metrics using the OpenTelemetry Protocol
  • Resource helps identify what is emitting the metrics
Step 2: Configure OpenTelemetry

Next, configure OpenTelemetry to send metrics to Cloud Monitoring:

Let's break down what's happening:

  • Resource identifies your service with the label "service.name": "OrdersAPI"
  • OTLPMetricExporter configures the OTLP endpoint to send metrics to (in this environment, a local collector that forwards to GCP)
  • PeriodicExportingMetricReader automatically exports metrics every 10 seconds
  • MeterProvider combines everything and manages the metrics lifecycle
  • meter is used to create specific metric instruments
Emitting Custom Metrics with OpenTelemetry

A metric is just a number that tells you something about your application. OpenTelemetry provides different types of metric instruments. For tracking order values, we'll use a Histogram, which is perfect for recording measurements like amounts, durations, or sizes.

Step 3: Create a Histogram Instrument

Let's create a histogram to track order values:

This creates a histogram instrument named "order_value" that will track the distribution of order values in USD.

Step 4: Record Metric Values

Now let's record some order values. With OpenTelemetry, recording a value is very simple:

Example output:

Notice how much simpler this is compared to manually creating time series! OpenTelemetry handles:

  • Timestamps automatically
  • Batching multiple measurements
  • Exporting at regular intervals
  • Resource attribution

The final time.sleep(10) ensures the last batch of metrics is exported before the program ends (since we export every 10 seconds).

Creating And Configuring A Cloud Monitoring Dashboard

A dashboard in Cloud Monitoring is a visual display of your metrics. It helps you see trends and spot problems quickly. For this task, you would use the official Google Cloud Monitoring library in a production environment.

Understanding Dashboard Creation

In production GCP, you would create a dashboard to visualize the metrics you're sending through OpenTelemetry. Here's how that would look:

Here's what's happening:

  • We define a Widget that tells Cloud Monitoring what to display
  • The widget shows the average value of the order_value metric
  • When using the OTLP exporter (as in this lesson), metrics use the prefix workload.googleapis.com/
  • When using the direct Cloud Monitoring exporter, metrics use the prefix custom.googleapis.com/
  • The filter selects metrics from the service using the resource label we set earlier
Complete Working Example

Here's the complete code that brings everything together:

This code will emit 5 order value metrics and export them to Cloud Monitoring through the OTLP collector.

Summary And Practice Preview

In this lesson, you learned how to:

  • Use OpenTelemetry, an industry-standard observability framework, to emit custom metrics from your application
  • Configure OpenTelemetry to export metrics using OTLP (OpenTelemetry Protocol)
  • Create metric instruments (histograms) to track application data
  • Understand how dashboards can be created in Cloud Monitoring to visualize your metrics in real time

These skills help you keep an eye on your application's health and performance, which is important for both security and reliability. Using OpenTelemetry makes your monitoring code cleaner and more portable across different cloud platforms.

Next, you will get hands-on practice by emitting your own metrics with OpenTelemetry in the CodeSignal environment. Remember, the required libraries are already installed, so you can focus on writing and running your code.

Great job making it this far! You are now ready to monitor your own GCP applications using modern, industry-standard tools.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal