View latency of app requests
Learn how to collect and view latency data from your applications:
-
Create a Google Kubernetes Engine (GKE) cluster by using the Google Cloud CLI.
-
Download and deploy a sample application to your cluster.
Create a trace by sending an HTTP request to the sample application.
View the latency information of the trace you created.
Clean up.
To follow step-by-step guidance for this task directly in the Google Cloud console, click Guide me:
Before you begin
-
Security constraints defined by your organization might prevent you from completing the following steps. For troubleshooting information, see Develop applications in a constrained Google Cloud environment.
- 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.
-
Enable the Google Kubernetes Engine and Cloud Trace APIs.
-
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.
-
Enable the Google Kubernetes Engine and Cloud Trace APIs.
Create a GKE cluster
In the toolbar, click terminal
Activate Cloud Shell , and then perform the following steps in the Cloud Shell.Create a cluster:
gcloud container clusters create cloud-trace-demo --zone us-central1-c
The previous command, which takes several minutes to complete, creates a standard cluster with the name
cloud-trace-demo
in the zoneus-central1-c
.Configure
kubectl
to automatically refresh its credentials to use the same identity as the Google Cloud CLI:gcloud container clusters get-credentials cloud-trace-demo --zone us-central1-c
Verify access to your cluster:
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
Download and deploy and application
Download and deploy a Python application, which uses the Flask framework and the OpenTelemetry package. The application is described in the About the app section of this page.
In the Cloud Shell, do the following:
Clone a Python app from GitHub:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Run the following command to deploy the sample application:
cd python-docs-samples/trace/cloud-trace-demo-app-opentelemetry && ./setup.sh
The script
setup.sh
takes several minutes to complete.The script configures three services using a pre-built image and then waits for all resources to be provisioned. The workloads are named
cloud-trace-demo-a
,cloud-trace-demo-b
, andcloud-trace-demo-c
.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.
Create trace data
A trace describes the time it takes an application to complete a single operation.
To create a trace, in the Cloud Shell, run the following command:
curl $(kubectl get svc -o=jsonpath='{.items[?(@.metadata.name=="cloud-trace-demo-a")].status.loadBalancer.ingress[0].ip}')
The response of the previous command looks like the following:
Hello, I am service A
And I am service B
Hello, I am service C
You can execute the curl
command multiple times to generate multiple
traces.
View latency data
-
In the Google Cloud console, go to the Trace explorer page:
You can also find this page by using the search bar.
Each trace is represented by a dot on the graph and a row in the table.
In the following screenshot shows multiple traces:
To view a trace in detail, select a dot in the graph or a row in the table.
The scatter plot is refreshed and the dot you selected is highlighted with a circle drawn around the dot, and all other dots that represent all other traces are dimmed.
A Gantt chart displays information about the selected trace. The first row in the Gantt chart is for the trace, and there exists one row for each span in the trace. A span describes how long it takes to perform a complete sub-operation.
To view information detailed information about a span, in the Gantt chart, select the span.
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 GKE 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 project and you no longer need the project, then delete the project.
If you used an existing project, then do the following:
To delete your cluster, in the Cloud Shell, run the following command:
gcloud container clusters delete cloud-trace-demo --zone us-central1-c
What's next
- For information on languages and platforms supported, see Cloud Trace overview.
For details on how to instrument your applications, see:
For more information on the Trace Explorer window, see Find and view traces.
To learn more about managing GKE clusters, see kubectl.