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.
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.
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:
- Your Python application uses the OpenTelemetry API to create and record metrics
- The OpenTelemetry SDK processes these metrics and exports them
- Cloud Monitoring receives and stores the metrics
- 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.
Before we can emit metrics, we need to set up OpenTelemetry to work with Google Cloud Monitoring.
First, import the libraries you will use:
opentelemetry.metricsprovides the core metrics APIMeterProvidermanages the creation of meters (which create metrics)PeriodicExportingMetricReadersends metrics to Cloud Monitoring at regular intervalsOTLPMetricExporterexports metrics using the OpenTelemetry ProtocolResourcehelps identify what is emitting the metrics
Next, configure OpenTelemetry to send metrics to Cloud Monitoring:
Let's break down what's happening:
Resourceidentifies your service with the label"service.name": "OrdersAPI"OTLPMetricExporterconfigures the OTLP endpoint to send metrics to (in this environment, a local collector that forwards to GCP)PeriodicExportingMetricReaderautomatically exports metrics every 10 secondsMeterProvidercombines everything and manages the metrics lifecyclemeteris used to create specific metric instruments
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.
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.
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).
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.
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
Widgetthat tells Cloud Monitoring what to display - The widget shows the average value of the
order_valuemetric - 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
filterselects metrics from the service using the resource label we set earlier
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.
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.
