View app latency
Learn how to use Cloud Trace by doing the following:
Deploy a sample application to a Google Kubernetes Engine (GKE) cluster.
Create a trace by sending an HTTP request to the sample application.
Use the Cloud Trace interface to view the latency information of the trace you created.
Before you begin
Some of the steps in this document might not work correctly if your organization applies constraints to your Google Cloud environment. In that case, you might not be able to complete tasks like creating public IP addresses or service account keys. If you make a request that returns an error about constraints, see how to Develop applications in a constrained Google Cloud environment.
Create a project with billing enabled:
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
Download and deploy your application
To download and deploy the application, do the following:
To open the Cloud Shell, click Activate Cloud Shell in the Google Cloud console toolbar:
After a few moments, a Cloud Shell session opens inside the Google Cloud console.
To download the source code from GitHub, run the following command:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Enable the Google Kubernetes Engine API by using the following command:
gcloud services enable container.googleapis.com
To create the GKE cluster named
cloud-trace-demo
in theus-central1-c
zone, run the following commands in the Cloud Shell:ZONE=us-central1-c gcloud container clusters create cloud-trace-demo \ --zone $ZONE
This command takes a few minutes to complete. After it completes successfully, your Google Cloud project contains the GKE cluster named
cloud-trace-demo
. You must have permission to create clusters that have external access in your Google Cloud project.Update your GKE cluster credentials by running the following command:
gcloud container clusters get-credentials cloud-trace-demo --zone $ZONE
Verify access to the cluster by running the following command:
kubectl get nodes
A sample output of this command is:
NAME STATUS ROLES AGE VERSION gke-cloud-trace-demo-default-pool-063c0416-113s Ready <none> 78s v1.22.12-gke.2300 gke-cloud-trace-demo-default-pool-063c0416-1n27 Ready <none> 79s v1.22.12-gke.2300 gke-cloud-trace-demo-default-pool-063c0416-frkd Ready <none> 78s v1.22.12-gke.2300
Deploy the sample application by running the following command:
cd python-docs-samples/trace/cloud-trace-demo-app-opentelemetry && ./setup.sh
The script
setup.sh
configures three services of the application using a pre-built image. The workloads are namedcloud-trace-demo-a
,cloud-trace-demo-b
, andcloud-trace-demo-c
. The setup script waits for all resources to be provisioned, so the configuration might take several minutes to complete.A sample output of this command is:
deployment.apps/cloud-trace-demo-a is created service/cloud-trace-demo-a is created deployment.apps/cloud-trace-demo-b is created service/cloud-trace-demo-b is created deployment.apps/cloud-trace-demo-c is created service/cloud-trace-demo-c is created Wait for load balancer initialization complete...... Completed. You can access the demo at http://34.82.132.95/
Create a trace
A trace describes the time it takes an application to complete a single operation. Each trace consists of one or more spans. A span describes how long it takes to perform a complete sub-operation. For example, a trace might describe how long it takes to process an incoming request from a user and return a response. A span might describe how long a particular RPC call requires. For more information, see Cloud Trace's Data model.
To create a trace by sending a curl request to cloud-trace-demo-a
, use the
following command:
curl $(kubectl get svc -o=jsonpath='{.items[?(@.metadata.name=="cloud-trace-demo-a")].status.loadBalancer.ingress[0].ip}')
You can execute the curl
command multiple times to generate multiple
traces.
The output looks like the following:
Hello, I am service A And I am service B Hello, I am service C
View the trace data
In the Google Cloud console, select Cloud Trace or click the following button:
Overview window
The Overview window is the default view in Trace. This window displays latency data and summary information, including an analysis report. If you created a new project, the most interesting pane of the Overview window is the pane labeled Recent traces:
This pane lists the most recent traces and their latency. To view the details of a trace, click its link.
Trace list window
In the Trace navigation pane, click list Trace list:
This window displays a graph and a table. Each dot on the graph represents
a trace. Each dot also corresponds to a row in the table. In the previous
screenshot, multiple traces are listed, indicating that the curl
command
was executed multiple times.
To view a trace in detail, select a dot in the graph or a row in the table:
After you select a dot, you see a waterfall graph where each row corresponds to a span. The details of the span, such as its trace labels, method, and summary information about the command latency are displayed in the details table. To view details about a span, click the corresponding row in the waterfall graph:
Analysis Reports window
To view or create a report, in the Trace navigation pane, click
Analysis Reports. Trace
automatically creates daily reports. For this project, there isn't enough
data to create a new report.
About the application
The sample application used in this quickstart is available in a GitHub repository. This repository contains information on how to use the application in environments other than the Cloud Shell. The sample application is written in Python, uses the Flask framework and OpenTelemetry packages, and executes on a Google Kubernetes Engine cluster.
Instrumentation
The file app.py
in the GitHub repository,
contains the instrumentation necessary to capture and send trace
data to your Google Cloud project:
The application imports several OpenTelemetry packages:
The application instruments web requests with trace context and automatically traces Flask handlers and requests to other services:
The application configures the Cloud Trace exporter as a trace provider, which propagates trace context in the Cloud Trace format:
The following code snippet shows how to send requests in Python. OpenTelemetry implicitly propagates the trace context for you with your outgoing requests:
How the application works
For clarity, in this section, cloud-trace-demo
is omitted from the service
names. For example, the service cloud-trace-demo-c
is referenced as c
.
This application creates three services named a
, b
, and c
. Service a
is
configured to call service b
, service b
is configured to call service c
.
For details on the configuration of the services, see the YAML files in the
GitHub repository.
When you issued a HTTP request to service a
in this quickstart,
you used the following curl
command:
curl $(kubectl get svc -o=jsonpath='{.items[?(@.metadata.name=="cloud-trace-demo-a")].status.loadBalancer.ingress[0].ip}')
The curl
command works as follows:
kubectl
fetches the IP address of the service namedcloud-trace-demo-a
.- The
curl
command then sends the HTTP request to servicea
. - Service
a
receives the HTTP request and sends a request to serviceb
. - Service
b
receives the HTTP request and sends a request to servicec
. - Service
c
receives the HTTP request from serviceb
and returns the stringHello, I am service C
to serviceb
. - Service
b
receives the response from servicec
, appends it to the stringAnd I am service B
, and returns the result to servicea
. - Service
a
receives the response from serviceb
and appends it to the stringHello, I am service A
. - The response from service
a
is printed in the Cloud Shell.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
If you created a new Google Cloud project for this quickstart, then delete the project to stop accruing charges. To delete your project, do the following:
- In the Google Cloud console, click menu Navigation Menu, and select Home.
- In the Project info pane, click Go to project settings.
- In the Settings window, click Shut down, and complete the remaining steps.
If you didn't create a new Google Cloud project for this quickstart, then delete the Google Kubernetes Engine cluster named
cloud-trace-demo
by running the following command:gcloud container clusters delete cloud-trace-demo --zone $ZONE
What's next
- For information on languages and platforms supported, see Cloud Trace overview.
- For details on how to instrument your applications, see Instrument for Cloud Trace.
- For more information on the Overview window, see View traces overview.
- For more information on the Trace list window, see Find and view traces.
- For information on the waterfall graph and trace details, see View a trace.
- For information on Analysis Reports, see Create and view reports.
- To learn more about managing Google Kubernetes Engine clusters, see kubectl