Google Cloud Trace Tutorial: Architecture, Pricing, Use Cases, and Hands-On Guide for Observability and monitoring

Category

Observability and monitoring

1. Introduction

Cloud Trace is Google Cloud’s distributed tracing service. It helps you understand where time is spent in a request as it travels through your application and across microservices, serverless components, and external dependencies.

In simple terms: Cloud Trace shows you a timeline of a request (a “trace”) broken into spans (individual operations), so you can quickly pinpoint slow endpoints, bottlenecks, or problematic downstream calls.

Technically, Cloud Trace ingests trace spans from instrumented workloads (for example via OpenTelemetry), stores and indexes them by Google Cloud project, and provides analysis and visualization through the Google Cloud console (Trace UI) and APIs. It supports common tracing concepts like trace IDs, span IDs, latency breakdowns, sampling, and correlation with logs and metrics.

The main problem it solves is debugging and optimizing latency in distributed systems. Without tracing, you often only know that a request is slow; with Cloud Trace, you can see which service, which operation, and which dependency caused the delay.

Naming note: Cloud Trace was historically known as Stackdriver Trace (Stackdriver became part of Google Cloud Operations). Today the product name is Cloud Trace and it is part of the Google Cloud Observability suite.


2. What is Cloud Trace?

Cloud Trace is a managed distributed tracing service in Google Cloud Observability and monitoring that helps you collect, analyze, and visualize timing data (traces/spans) for requests in your applications.

Official purpose (what it’s for)

  • Collect and store distributed traces from applications running on Google Cloud (and, with proper configuration, from other environments).
  • Provide tools to analyze request latency, identify outliers, and troubleshoot performance regressions.
  • Support correlation of traces with other observability signals (logs, metrics, errors) in the Google Cloud ecosystem.

Core capabilities (what you can do)

  • Ingest spans via APIs/SDKs and OpenTelemetry exporters.
  • View traces and their span timelines in the Google Cloud console.
  • Use latency analysis to find slow endpoints and high-latency traces.
  • Programmatically write and read trace data via the Cloud Trace API.

Major components

  • Instrumentation: Libraries/agents that create spans (commonly OpenTelemetry SDKs with a Cloud Trace exporter).
  • Cloud Trace API: Receives spans and allows querying trace data.
  • Trace UI in Google Cloud console: Trace list/explorer and trace detail views; latency reporting features (UI capabilities can evolve—verify in official docs for the latest UI terms).
  • IAM roles and permissions: Control who can write traces and who can view them.

Service type

  • Fully managed Google Cloud service (control plane and storage managed by Google).
  • API-driven ingestion with console-based analysis.

Scope (how it’s scoped in Google Cloud)

  • Project-scoped: Trace data is associated with a Google Cloud project.
  • Access controlled via IAM at the project level (and potentially via organizational controls like VPC Service Controls, depending on your environment—verify support in official docs if you require it).

Regional/global considerations

Cloud Trace is consumed as a Google Cloud API service. You typically don’t pick a “zonal” or “regional” instance the way you would for a database. Data residency and location controls for trace storage can be nuanced and may change—verify in official docs if you have strict residency requirements.

How it fits into the Google Cloud ecosystem

Cloud Trace is usually used alongside: – Cloud Monitoring (metrics, SLOs/alerting) – Cloud Logging (logs, log-based metrics, trace-log correlation) – Error Reporting (grouping/triage of exceptions) – Cloud Profiler (CPU/heap profiling) – Managed runtimes like Cloud Run, GKE, Compute Engine, App Engine


3. Why use Cloud Trace?

Business reasons

  • Reduce customer-facing latency and improve user experience.
  • Shorten mean time to resolution (MTTR) for performance incidents.
  • Provide evidence-driven optimization: measure improvements after releases.

Technical reasons

  • Find slow requests and identify which service or dependency is responsible.
  • Understand distributed request flow across microservices.
  • Validate caching strategies, database query behavior, and retry storms.

Operational reasons

  • Improve on-call effectiveness with trace timelines instead of guessing from logs alone.
  • Support post-incident analysis by examining traces around incident windows.
  • Complement metrics: metrics tell you “what,” traces help explain “why.”

Security/compliance reasons

  • Enforce least-privilege access to trace data via IAM.
  • Support auditing of API usage (via Cloud Audit Logs for supported services—verify in your environment).
  • Help detect unusual call paths (for example, unexpected downstream calls).

Scalability/performance reasons

  • Scales with distributed environments where single-node profiling or logs aren’t sufficient.
  • Supports sampling strategies to control overhead and cost.

When teams should choose Cloud Trace

  • You’re running services on Google Cloud and want tight integration with Google Cloud Observability.
  • You use (or plan to use) OpenTelemetry as a standard instrumentation layer.
  • You need a managed tracing backend without operating Jaeger/Zipkin/Tempo.

When teams should not choose Cloud Trace

  • You need strict, configurable on-prem storage control with custom retention/backends (self-managed tracing might fit better).
  • You already standardized on another tracing backend across multiple clouds and want a single vendor-neutral store.
  • You require features not available in Cloud Trace UI/API (for example, specific advanced query features). In that case, evaluate alternatives carefully.

4. Where is Cloud Trace used?

Industries

  • SaaS and B2B platforms
  • E-commerce and retail
  • Financial services (latency-sensitive APIs)
  • Gaming (backend request performance)
  • Media and streaming platforms
  • Healthcare (performance and audit-driven diagnostics)

Team types

  • SRE and platform engineering teams
  • DevOps and operations teams
  • Backend/microservices development teams
  • API engineering teams
  • Security and reliability engineers (for correlation and incident response)

Workloads

  • Microservices on GKE
  • Serverless apps on Cloud Run
  • VM-based services on Compute Engine
  • Hybrid apps (Google Cloud + on-prem) using OpenTelemetry exporters
  • Event-driven systems where traces connect HTTP requests to async processing (requires propagation and instrumentation discipline)

Architectures

  • Service-to-service synchronous calls (HTTP/gRPC)
  • Polyglot stacks (Go/Java/Python/Node.js) with OpenTelemetry
  • API gateway + backend services + database + third-party APIs

Real-world deployment contexts

  • Production performance optimization and incident response
  • Staging/QA regression detection (release comparisons)
  • Load testing validation (spot slow code paths under stress)

Production vs dev/test usage

  • Dev/test: typically higher sampling (more visibility) and shorter retention needs.
  • Production: careful sampling to balance visibility, overhead, and cost; access control and governance become more important.

5. Top Use Cases and Scenarios

Below are realistic scenarios where Cloud Trace is a strong fit.

1) Microservice latency root-cause

  • Problem: A user request takes 4 seconds, but service metrics look “fine.”
  • Why Cloud Trace fits: Breaks down latency by span across services and dependencies.
  • Scenario: A checkout API calls inventory, pricing, fraud detection, and payment—Cloud Trace reveals payment tokenization is the bottleneck.

2) Cold start vs application latency (serverless)

  • Problem: P95 latency spikes occur after deployments.
  • Why it fits: Traces show time spent in initialization vs request handling (depending on instrumentation).
  • Scenario: Cloud Run service shows long spans at startup; you optimize container image and dependency loading.

3) Database query hotspot detection

  • Problem: Intermittent slow queries cause timeouts.
  • Why it fits: DB spans highlight slow operations and correlate with specific endpoints.
  • Scenario: A product listing endpoint occasionally triggers an unindexed query; traces expose the slow query path.

4) Third-party API performance monitoring

  • Problem: External API calls are slow or failing, impacting user requests.
  • Why it fits: Captures outbound client spans and timing.
  • Scenario: Shipping rate API adds 1.2 seconds; traces show it dominates the timeline.

5) Regression after a release

  • Problem: After a new version, latency increases but metrics don’t clearly show why.
  • Why it fits: Compare traces between versions; identify new spans or added work.
  • Scenario: A new feature adds an extra authorization call; trace reveals added hop.

6) Debugging retry storms and cascading latency

  • Problem: Latency increases due to retries; services amplify load.
  • Why it fits: Trace timelines can show repeated client spans and longer downstream latency.
  • Scenario: A downstream service returns 503; upstream retries create a fan-out storm.

7) Identifying “long tail” outliers

  • Problem: Average latency looks good, but P99 is terrible.
  • Why it fits: Trace list highlights slow outliers for deep inspection.
  • Scenario: Only certain customer requests are slow due to specific data shapes; traces identify the code path.

8) Validating caching effectiveness

  • Problem: Cache hit rate claims don’t match user experience.
  • Why it fits: Spans show cache lookups vs DB calls.
  • Scenario: Cache misses occur on specific keys; traces show DB spans appear unexpectedly.

9) Multi-region traffic debugging

  • Problem: Requests routed to a region have worse latency.
  • Why it fits: Traces show increased network or service latency (with appropriate instrumentation).
  • Scenario: A region uses a different dependency endpoint; traces reveal longer external call spans.

10) Tracing async work initiated by HTTP requests

  • Problem: A user request triggers background processing; you need end-to-end visibility.
  • Why it fits: With trace context propagation through messaging, you can connect spans across async boundaries.
  • Scenario: HTTP request publishes to Pub/Sub; subscriber continues the trace to show full processing time (requires careful propagation and instrumentation).

11) GKE service mesh troubleshooting (where applicable)

  • Problem: Service-to-service latency is inconsistent in a Kubernetes cluster.
  • Why it fits: Tracing helps identify which hop introduces latency.
  • Scenario: A sidecar proxy or upstream service introduces delays; traces show slow segments.

12) SLA/SLO support investigations

  • Problem: An SLO breach occurred; you need evidence for which endpoints/users were impacted.
  • Why it fits: Trace sampling + filters can support targeted investigations.
  • Scenario: For a specific endpoint, you retrieve representative slow traces and identify root causes.

6. Core Features

Cloud Trace capabilities evolve; the list below focuses on durable, widely used features. Verify the latest UI terminology and feature set in the official docs.

1) Distributed trace ingestion (spans)

  • What it does: Accepts spans (operations) that form complete traces for requests.
  • Why it matters: Without spans, you can’t see cross-service request breakdown.
  • Practical benefit: Understand where time is spent and which dependencies dominate latency.
  • Caveats: You must instrument apps correctly and propagate trace context across service boundaries.

2) Trace visualization in Google Cloud console

  • What it does: Provides trace lists and trace detail views with span timelines.
  • Why it matters: Visual timelines speed debugging versus reading logs.
  • Practical benefit: Quickly spot the slow span, repeated retries, or unexpected call paths.
  • Caveats: UI filters and views can change; learn both UI and API-based workflows.

3) Latency analysis / reporting

  • What it does: Aggregates traces to show latency distributions and slow endpoints.
  • Why it matters: Helps prioritize optimization work by impact.
  • Practical benefit: Find top slow methods/routes and understand percentile behavior.
  • Caveats: The value depends on sampling strategy and consistent span naming.

4) Cloud Trace API (read/write)

  • What it does: Programmatic access to write spans and query traces.
  • Why it matters: Supports automation, custom tooling, and integration with CI/CD or analysis pipelines.
  • Practical benefit: You can validate ingestion, build dashboards, or export data flows (where supported).
  • Caveats: API quotas apply; plan for rate limits and pagination.

5) OpenTelemetry compatibility (common approach)

  • What it does: Lets you instrument apps using OpenTelemetry SDKs and export traces to Cloud Trace.
  • Why it matters: OpenTelemetry is a common standard across languages and platforms.
  • Practical benefit: Avoid vendor lock-in at instrumentation layer; consistent data model across services.
  • Caveats: Exporter configuration and supported semantic conventions vary; verify your language exporter and versions.

6) Trace context propagation

  • What it does: Preserves a request’s trace ID across services via headers (for example W3C Trace Context).
  • Why it matters: Without propagation, your traces fragment into isolated segments.
  • Practical benefit: True end-to-end visibility.
  • Caveats: You must ensure gateways, proxies, and clients forward headers; async propagation requires extra care.

7) Integration with Cloud Logging (correlation)

  • What it does: Enables linking logs to traces using trace IDs (when logs include trace correlation fields).
  • Why it matters: Traces show timing; logs show details/errors.
  • Practical benefit: Jump from a slow trace to relevant logs for that request.
  • Caveats: Correlation requires structured logging fields or platform support; not every log line automatically links.

8) IAM-controlled access

  • What it does: Restricts who can view traces and who can write spans.
  • Why it matters: Traces can contain sensitive metadata (URLs, IDs) if you’re not careful.
  • Practical benefit: Least privilege and separation of duties.
  • Caveats: Over-permissioned roles are common; define clear roles for writers vs readers.

7. Architecture and How It Works

High-level architecture

At a high level, Cloud Trace is an ingestion + storage + analysis system:

  1. Your application creates spans (via OpenTelemetry or another supported library).
  2. A tracer/exporter sends spans to the Cloud Trace API using authenticated requests.
  3. Cloud Trace stores and indexes the trace data under your Google Cloud project.
  4. You view traces and run latency analysis in the console, or query via APIs.
  5. You correlate traces with logs/metrics for end-to-end observability.

Request/data/control flow

  • Data plane (tracing data):
  • App → (OpenTelemetry SDK) → Exporter → Cloud Trace API → Trace storage/index → Trace UI/API queries
  • Control plane:
  • IAM policies determine who can write/read.
  • Quotas limit ingestion/query throughput.
  • Audit logs (where applicable) record administrative and data access operations (verify in official docs for Cloud Trace audit coverage).

Integrations with related services

  • Cloud Run / GKE / Compute Engine: Common compute targets that emit traces.
  • Cloud Logging: Use trace correlation fields to link logs to trace IDs.
  • Cloud Monitoring: Use metrics for alerts; traces for deep dive (Cloud Trace is not a full alerting system by itself).
  • Error Reporting: Combine stack traces/errors with trace timelines to debug failures.

Dependency services

You typically depend on: – Cloud Trace API (enabled in the project) – IAM + Service Accounts (for authentication) – Your chosen instrumentation libraries (OpenTelemetry recommended)

Security/authentication model

  • Applications authenticate to Cloud Trace using Application Default Credentials (ADC):
  • On Google Cloud runtimes, this usually means the workload’s service account.
  • You grant the service account permission to write trace spans (for example, a role like “Cloud Trace Agent”).
  • Humans and tooling that view traces need read permissions (for example, “Cloud Trace User”).

Networking model

  • Traces are sent over HTTPS to Google APIs endpoints.
  • If your environment uses restricted egress or private connectivity:
  • Ensure access to Google APIs (for example via Private Google Access, or other organization-approved paths).
  • For strict environments, confirm whether VPC Service Controls and restricted VIPs are supported for Cloud Trace in your setup (verify in official docs).

Monitoring/logging/governance considerations

  • Monitor exporter errors in application logs (failed exports mean missing traces).
  • Track ingestion volume to manage cost.
  • Implement data hygiene: avoid putting secrets or sensitive payloads into span attributes.

Simple architecture diagram (Mermaid)

flowchart LR
  U[User / Client] --> S[Service (Cloud Run / GKE / VM)]
  S -->|OpenTelemetry spans| E[OTel Exporter]
  E -->|HTTPS| CTA[Cloud Trace API]
  CTA --> UI[Trace UI in Google Cloud Console]

Production-style architecture diagram (Mermaid)

flowchart TB
  subgraph Internet
    C[Web / Mobile Client]
  end

  subgraph GoogleCloud[Google Cloud Project]
    LB[Cloud Load Balancer / API Gateway\n(optional)]
    CR1[Cloud Run: api-service]
    GKE1[GKE: orders-service]
    GKE2[GKE: payments-service]
    DB[Cloud SQL / Spanner / Firestore\n(example dependency)]
    EXT[Third-party API\n(example dependency)]

    OTel1[OpenTelemetry SDKs\n+ Context Propagation]
    TraceAPI[Cloud Trace API]
    TraceUI[Trace UI]
    Logging[Cloud Logging]
    Monitoring[Cloud Monitoring]
  end

  C --> LB --> CR1
  CR1 --> GKE1 --> DB
  GKE1 --> GKE2 --> EXT

  CR1 --- OTel1
  GKE1 --- OTel1
  GKE2 --- OTel1

  OTel1 -->|spans| TraceAPI --> TraceUI

  CR1 -->|logs (with trace IDs)| Logging
  GKE1 -->|logs (with trace IDs)| Logging
  GKE2 -->|logs (with trace IDs)| Logging

  Monitoring <--> TraceUI

8. Prerequisites

Before you start, ensure the following are in place.

Account/project requirements

  • A Google Cloud account with a Google Cloud project.
  • Billing enabled on the project (Cloud Trace usage beyond free allotments may incur cost; Cloud Run may also incur cost outside free tier).

Permissions / IAM roles

You need permissions for: – Enabling APIs – Deploying a service (Cloud Run in the lab) – Writing traces (service account) – Viewing traces (your user)

Common roles (use least privilege; exact needs vary): – For you (human) in the lab: – roles/run.admin (or narrower) – roles/iam.serviceAccountUserroles/serviceusage.serviceUsageAdmin (to enable APIs) or equivalent – roles/cloudtrace.user (to view traces) – For the runtime service account: – roles/cloudtrace.agent (to write trace spans)

Verify current IAM roles and permissions in official docs: – https://cloud.google.com/trace/docs/iam

Billing requirements

  • Cloud Trace is usage-based. Billing must be enabled to avoid unexpected service disruption when you exceed no-cost usage.

CLI/SDK/tools needed

  • Google Cloud CLI (gcloud): https://cloud.google.com/sdk/docs/install
  • A local terminal with:
  • Python 3.10+ (for the sample)
  • pip
  • Optional: Docker (not required for gcloud run deploy --source)

Region availability

  • Cloud Run is regional (you choose a region).
  • Cloud Trace is accessed as an API; regionality for trace storage/processing is not configured the same way as compute. If you need a specific data location, verify in official docs.

Quotas/limits

  • Cloud Trace API has quotas (write requests, read requests, etc.). For production, review quotas and request increases as needed.
  • Verify quotas in:
  • Google Cloud console → IAM & Admin → Quotas (filter for Cloud Trace API)
  • Or official docs (quota pages can change—verify current location).

Prerequisite services (APIs)

For the hands-on lab, enable: – Cloud Trace API – Cloud Run Admin API – Cloud Build API (if deploying from source) – Artifact Registry API (often required by Cloud Run builds)


9. Pricing / Cost

Cloud Trace pricing can change and may differ by SKU/region or other dimensions. Use official sources for current numbers and free-tier thresholds.

Current pricing model (high-level)

Cloud Trace is typically priced based on trace data ingestion volume (for example, number of spans ingested) and possibly additional dimensions for reading/querying (depending on current SKU structure). Exact SKUs and free allotments must be confirmed on the official pricing page.

  • Official pricing page (verify current details):
    https://cloud.google.com/trace/pricing
  • Google Cloud Pricing Calculator:
    https://cloud.google.com/products/calculator

Pricing dimensions to understand

Common cost drivers in distributed tracing systems (and what to confirm for Cloud Trace): 1. Spans ingested (primary driver in most tracing backends) 2. Sampling rate (higher sampling → more spans → higher cost) 3. Span attribute cardinality (high-cardinality attributes can increase indexing/analysis overhead; the direct billing impact depends on product pricing details) 4. Retention (if configurable; verify whether Cloud Trace retention is fixed or configurable for your plan) 5. API read usage (if priced; verify on pricing page)

Free tier / no-cost usage

Cloud Trace historically offers some level of no-cost usage. The exact allowance is subject to change: – Verify the current free tier and thresholds on: https://cloud.google.com/trace/pricing

Hidden or indirect costs

  • Cloud Run / GKE / Compute costs for generating traces (CPU time and memory overhead from instrumentation/export).
  • Network egress: Sending spans to Google APIs is typically within Google’s network when running on Google Cloud, but cross-cloud or on-prem exporters may incur outbound internet/VPN/Interconnect costs.
  • Logging volume: If you add verbose logs for troubleshooting and keep them, Cloud Logging ingestion/storage can become a larger cost than tracing.

Cost optimization strategies

  • Use probabilistic sampling in high-throughput services (for example 1–10%) while keeping higher sampling for critical endpoints.
  • Prefer tail-based sampling only if you have a supported collector strategy (tail-based sampling is commonly done in OpenTelemetry Collectors; Cloud Trace pricing is still based on what you export).
  • Avoid adding large payloads or sensitive data as span attributes.
  • Instrument consistently, but don’t over-instrument extremely hot internal loops.
  • Set clear retention expectations and export only what you need (verify retention controls in Cloud Trace docs).

Example low-cost starter estimate (how to think about it)

A small Cloud Run service for learning might generate: – A few spans per request (for example: HTTP server span + a child span for an outbound call). – A few hundred requests during a lab session.

If you stay within the free tier thresholds for both Cloud Run and Cloud Trace, cost can be close to zero. If you exceed free tiers, cost depends on: – Total spans exported – Any applicable read/query charges – Cloud Run request/CPU time

Because exact unit prices and free tiers can change, use the pricing calculator and your expected spans per request × requests per day to estimate.

Example production cost considerations

In production, the main risk is high request volume combined with high sampling: – Example approach: – Estimate spans/request (often 5–50+ depending on downstream calls) – Multiply by requests/second and sampling rate – Compare to pricing SKUs and free tier – Consider: – Separate sampling policies for high-traffic endpoints vs critical transactions – Centralized OpenTelemetry Collector (optional) to control export volume and enrich data consistently


10. Step-by-Step Hands-On Tutorial

Objective

Deploy a small Python service to Cloud Run instrumented with OpenTelemetry, export spans to Cloud Trace, generate traffic, and verify traces in the Google Cloud console.

Lab Overview

You will: 1. Configure your Google Cloud project and enable required APIs. 2. Create a Cloud Run service account with trace-write permissions. 3. Build and deploy an instrumented Flask app to Cloud Run. 4. Send test requests and view traces in Cloud Trace. 5. Clean up resources to avoid ongoing cost.

This lab is designed to be low-cost and beginner-friendly.


Step 1: Set up your environment and select a project

1) Install and initialize the Google Cloud CLI: – https://cloud.google.com/sdk/docs/install

2) Authenticate and select your project:

gcloud auth login
gcloud config set project YOUR_PROJECT_ID

3) (Optional but recommended) Set a default region for Cloud Run:

gcloud config set run/region us-central1

Expected outcome: Your CLI is authenticated and pointing to the correct Google Cloud project.


Step 2: Enable required APIs

Enable Cloud Trace and Cloud Run dependencies:

gcloud services enable \
  cloudtrace.googleapis.com \
  run.googleapis.com \
  cloudbuild.googleapis.com \
  artifactregistry.googleapis.com

Expected outcome: APIs enable successfully.

Verification:

gcloud services list --enabled --filter="name:cloudtrace.googleapis.com"

Step 3: Create a service account for Cloud Run and grant trace permissions

1) Create a service account:

gcloud iam service-accounts create trace-demo-sa \
  --display-name="Cloud Run Trace Demo Service Account"

2) Grant the service account permission to write spans to Cloud Trace:

gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
  --member="serviceAccount:trace-demo-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
  --role="roles/cloudtrace.agent"

3) (Optional) Allow your user to view traces if you don’t already have access:

gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
  --member="user:YOUR_EMAIL" \
  --role="roles/cloudtrace.user"

Expected outcome: The service account exists and has Cloud Trace write permission.


Step 4: Create the instrumented Python service

Create a new local folder:

mkdir cloud-trace-cloudrun-demo
cd cloud-trace-cloudrun-demo

Create main.py:

import os
import time
import random
import requests
from flask import Flask

from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased
from opentelemetry.sdk.trace.export import BatchSpanProcessor

from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor

# Cloud Trace exporter for OpenTelemetry
# Verify exporter package support/version in official docs if you standardize this in production.
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter

app = Flask(__name__)

def configure_tracing():
    # Service name is important for filtering/grouping in tracing backends.
    service_name = os.getenv("OTEL_SERVICE_NAME", "trace-demo-service")

    resource = Resource.create({
        "service.name": service_name
    })

    # Demo-friendly sampling (100%) so you reliably see traces.
    # For production, use a lower ratio and a policy aligned with cost/performance.
    sampler = ParentBased(root=TraceIdRatioBased(1.0))

    provider = TracerProvider(resource=resource, sampler=sampler)
    exporter = CloudTraceSpanExporter()
    processor = BatchSpanProcessor(exporter)
    provider.add_span_processor(processor)

    trace.set_tracer_provider(provider)

    FlaskInstrumentor().instrument_app(app)
    RequestsInstrumentor().instrument()

configure_tracing()
tracer = trace.get_tracer(__name__)

@app.get("/")
def hello():
    # Add a custom span to show additional timing segments.
    with tracer.start_as_current_span("custom-work"):
        # Simulate variable work
        delay_ms = random.choice([10, 25, 50, 100, 250])
        time.sleep(delay_ms / 1000.0)

    # Create an outbound call span via requests instrumentation
    # Use a fast endpoint; external calls can be flaky in demos.
    r = requests.get("https://example.com", timeout=3)
    return {
        "message": "Hello from Cloud Run",
        "status_code": r.status_code,
        "simulated_delay_ms": delay_ms
    }, 200

@app.get("/slow")
def slow():
    with tracer.start_as_current_span("intentional-slow-span"):
        time.sleep(1.2)
    return {"message": "This endpoint is intentionally slower"}, 200

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", "8080")))

Create requirements.txt:

Flask==3.0.3
requests==2.32.3

opentelemetry-api==1.26.0
opentelemetry-sdk==1.26.0
opentelemetry-instrumentation==0.47b0
opentelemetry-instrumentation-flask==0.47b0
opentelemetry-instrumentation-requests==0.47b0

opentelemetry-exporter-gcp-trace==1.6.0

Create a Procfile-style entry point using Cloud Run’s default PORT by adding Dockerfile is optional if you use gcloud run deploy --source. For Python source deploy, also add app.yaml? Not required for Cloud Run.

Add README not necessary.

Expected outcome: You have a minimal Flask app that emits OpenTelemetry spans and exports them to Cloud Trace.

Important caveat: Package names and versions can change. If installation fails, verify current OpenTelemetry + Google Cloud Trace exporter guidance in official docs: – https://cloud.google.com/trace/docs/setup/python-ot

(If that specific URL changes, navigate from: https://cloud.google.com/trace/docs )


Step 5: Deploy to Cloud Run

Deploy directly from source (uses Cloud Build):

gcloud run deploy trace-demo \
  --source . \
  --service-account trace-demo-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com \
  --allow-unauthenticated \
  --set-env-vars OTEL_SERVICE_NAME=trace-demo-service

When deployment completes, gcloud prints a Service URL.

Expected outcome: A public Cloud Run service is deployed and reachable.

Verification: Open the service URL in your browser. You should see JSON output from /.


Step 6: Generate traffic (create traces)

Call the endpoints multiple times.

Replace SERVICE_URL with your Cloud Run URL:

SERVICE_URL="https://YOUR_CLOUD_RUN_URL"

# Generate a burst of requests
for i in $(seq 1 20); do
  curl -s "${SERVICE_URL}/" > /dev/null
done

# Generate some slower traces
for i in $(seq 1 5); do
  curl -s "${SERVICE_URL}/slow" > /dev/null
done

Expected outcome: Requests return HTTP 200 and your service produces spans that get exported.


Step 7: View traces in Cloud Trace (console)

1) In Google Cloud console, go to ObservabilityTrace.
Direct link (entry point; UI paths can change): https://console.cloud.google.com/traces/list

2) Select your project and look for recent traces. 3) Filter by your service name if the UI supports it (for example trace-demo-service) or by time range (last 1 hour).

4) Open a trace and confirm you see spans such as: – HTTP server span (Flask) – custom-work – HTTP client span for https://example.com – For /slow, the intentional-slow-span

Expected outcome: You can open a trace and see the span timeline with durations.


Validation

Use this checklist:

  • [ ] Cloud Run service responds to / and /slow
  • [ ] Trace UI shows recent traces within the expected time window
  • [ ] Trace details show multiple spans per request (not just one)
  • [ ] /slow traces show a noticeably longer span duration

If you don’t see traces after a few minutes, go to Troubleshooting below.


Troubleshooting

Common issues and fixes:

Issue: No traces appear in Cloud Trace

  • Cause: Missing permissions for the Cloud Run service account.
  • Fix: Ensure the Cloud Run runtime service account has roles/cloudtrace.agent.
gcloud projects get-iam-policy YOUR_PROJECT_ID \
  --flatten="bindings[].members" \
  --filter="bindings.members:trace-demo-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
  --format="table(bindings.role)"

Issue: Exporter errors in logs (PermissionDenied)

  • Cause: Wrong service account, or service deployed without the intended service account.
  • Fix: Confirm Cloud Run service is using the correct service account:
gcloud run services describe trace-demo --format="value(spec.template.spec.serviceAccountName)"

Redeploy with --service-account ... if needed.

Issue: Dependency install fails during build

  • Cause: Version mismatch or package name changes.
  • Fix: Verify exporter package and recommended versions in official docs. Consider pinning to known-compatible versions or using the Google-provided OpenTelemetry distributions (if recommended for your language/runtime).

Issue: Traces are sampled out

  • Cause: Sampling set too low (or inherited from upstream).
  • Fix: For the lab, we set 100% sampling. For production, use controlled sampling but ensure critical endpoints are included.

Issue: Outbound request spans missing

  • Cause: Requests instrumentation not applied or not imported early enough.
  • Fix: Ensure RequestsInstrumentor().instrument() is called during startup.

Cleanup

To avoid ongoing cost, delete resources.

1) Delete the Cloud Run service:

gcloud run services delete trace-demo

2) (Optional) Delete the service account:

gcloud iam service-accounts delete trace-demo-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com

3) (Optional) If Artifact Registry repositories were created by your workflow, review and delete unused images/repos in Artifact Registry.

Expected outcome: Cloud Run service is removed and no longer incurs runtime cost.


11. Best Practices

Architecture best practices

  • Standardize on OpenTelemetry across services so traces are consistent.
  • Adopt consistent service naming (service.name) and span naming conventions.
  • Instrument at boundaries:
  • inbound request handler
  • outbound HTTP/gRPC clients
  • database calls
  • queue publish/consume
  • Propagate context across:
  • HTTP/gRPC headers (W3C Trace Context recommended)
  • asynchronous messaging (requires explicit propagation patterns)

IAM/security best practices

  • Use a dedicated runtime service account per service or per environment (dev/stage/prod).
  • Grant minimal roles:
  • writers: roles/cloudtrace.agent
  • readers: roles/cloudtrace.user
  • Avoid giving broad Editor/Owner to developers for observability workflows.

Cost best practices

  • Implement sampling intentionally:
  • Start with higher sampling in staging.
  • Use lower sampling in production, increase sampling only during investigations.
  • Reduce span volume:
  • don’t create spans for extremely frequent internal loops
  • avoid excessive span attributes
  • Monitor ingestion trends and set internal budgets/alerts around usage (often via billing reports rather than trace itself).

Performance best practices

  • Use batch span processors (as in the lab) to reduce overhead.
  • Keep span attributes small; avoid attaching entire payloads.
  • Ensure exporter timeouts/retries don’t block request paths.

Reliability best practices

  • Ensure tracing failures do not break the application:
  • exporters should fail open (drop spans) rather than crash
  • Use structured logs with trace IDs to maintain visibility even when traces are sampled.

Operations best practices

  • Define runbooks:
  • “How to find the slow endpoint”
  • “How to locate traces for a specific request ID”
  • “How to correlate with logs”
  • Use consistent environment labels (for example deployment.environment=prod) if supported by your instrumentation.

Governance/tagging/naming best practices

  • Use consistent labels/attributes:
  • service.name, service.version
  • environment (prod/stage/dev)
  • region (if helpful)
  • Avoid high-cardinality user identifiers in span attributes (privacy + cost + usability concerns).

12. Security Considerations

Identity and access model

  • Cloud Trace access is controlled by Google Cloud IAM.
  • Separate permissions for:
  • writing spans (agent role)
  • reading traces (user role)
  • administering trace settings (admin role, if applicable in your org)

Reference: https://cloud.google.com/trace/docs/iam

Encryption

  • Data in transit: HTTPS/TLS to Google APIs.
  • Data at rest: encrypted by Google Cloud by default (standard Google Cloud storage encryption). For CMEK-style requirements, verify in official docs whether Cloud Trace supports customer-managed encryption keys for trace data in your region/organization.

Network exposure

  • Workloads must reach Google APIs endpoints.
  • For restricted networks:
  • evaluate Private Google Access / restricted VIP routes
  • confirm VPC Service Controls support for Cloud Trace if required (verify in official docs)

Secrets handling

  • Do not put secrets (API keys, tokens, credentials) into:
  • span names
  • span attributes
  • events
  • Treat tracing metadata as potentially accessible to broader engineering audiences.

Audit/logging

  • Use Cloud Audit Logs to track administrative actions in the project.
  • For data access auditability (who queried traces), verify Cloud Trace’s audit logging capabilities in your environment and organization policy (audit coverage varies by service and log type—verify in official docs).

Compliance considerations

  • Traces can contain personal data if you add user IDs, emails, or full URLs with query parameters.
  • Apply privacy controls:
  • avoid collecting PII
  • sanitize attributes
  • adopt a data classification standard for observability telemetry

Common security mistakes

  • Allowing broad read access to traces in production projects.
  • Capturing request bodies/headers as span attributes.
  • Mixing dev/test telemetry with prod in the same project (increases blast radius and confusion).

Secure deployment recommendations

  • Use separate projects per environment (or at least separate telemetry scopes).
  • Enforce least privilege and use groups for access control.
  • Review telemetry data policy and implement attribute allowlists/denylists.

13. Limitations and Gotchas

Because Cloud Trace is a managed service with evolving UI and APIs, always confirm current behavior in official docs. Common practical limitations include:

  • Sampling is required at scale: exporting 100% of spans in high-traffic production can be expensive and adds overhead.
  • Context propagation is easy to get wrong: missing headers or broken propagation leads to fragmented traces.
  • High-cardinality attributes reduce usability: millions of unique user IDs in attributes make filtering noisy and can increase backend overhead.
  • Async tracing requires design: for Pub/Sub or background jobs, you must explicitly propagate trace context to connect segments.
  • Quotas can block ingestion: high-volume bursts may hit API quotas; plan quota monitoring and request increases.
  • Exporter compatibility varies: OpenTelemetry exporters and semantic conventions can evolve quickly; pin versions and test upgrades.
  • Not an alerting system by itself: Cloud Trace is primarily for investigation; use Cloud Monitoring for alerts/SLOs.
  • Data residency constraints may apply: if your compliance program requires explicit data location controls, verify Cloud Trace support before adopting.

14. Comparison with Alternatives

Cloud Trace is one piece of Google Cloud Observability and monitoring. Alternatives depend on whether you want managed vs self-managed and whether you need cross-cloud standardization.

Comparison table

Option Best For Strengths Weaknesses When to Choose
Cloud Trace (Google Cloud) Teams on Google Cloud needing managed tracing Native Google Cloud integration, managed backend, IAM integration, console UI Less portable backend than OSS; feature set depends on Cloud Trace UI/API You run on Google Cloud and want low-ops distributed tracing
Cloud Monitoring (Google Cloud) Metrics, alerting, SLOs Strong alerting/dashboards; integrates with many services Not a tracing system; limited request-level breakdown Use for alerting/SLOs; pair with Cloud Trace for investigations
Cloud Logging (Google Cloud) Central logs, investigations, audits Powerful querying and retention controls Logs alone can’t show end-to-end latency breakdown Use for details/errors; correlate with traces
AWS X-Ray Tracing on AWS Deep AWS integration AWS-specific backend You’re primarily on AWS
Azure Application Insights Tracing/APM on Azure Azure-native APM Azure-specific backend You’re primarily on Azure
Jaeger (self-managed) Custom control, Kubernetes-native Open source, flexible deployment You operate storage/scaling/upgrades You need full control and can run it reliably
Zipkin (self-managed) Simple tracing Lightweight Less feature-rich at scale Small deployments, learning environments
Grafana Tempo (self-managed/managed) Large-scale tracing with Grafana ecosystem Works well with Grafana; scalable design Still requires ops unless managed; integration work You standardize on Grafana + OSS stack
OpenTelemetry Collector + vendor backend Standardized pipelines Vendor-neutral instrumentation pipeline Still must pick/manage backend You want portability and centralized control of telemetry pipelines

15. Real-World Example

Enterprise example: Banking API platform (multi-service latency control)

  • Problem: A banking platform has multiple internal microservices (auth, accounts, payments). Customers report intermittent slow transfers. Metrics show elevated latency but can’t isolate the cause.
  • Proposed architecture:
  • Standardize on OpenTelemetry SDKs in all services
  • Export traces to Cloud Trace in the production Google Cloud project
  • Correlate traces with Cloud Logging for error context
  • Use Cloud Monitoring for SLOs and alerting; use traces for incident investigations
  • Why Cloud Trace was chosen:
  • Managed tracing backend integrated with Google Cloud IAM and console workflows
  • Reduced operational burden versus self-hosting
  • Expected outcomes:
  • Faster isolation of slow dependencies (for example, a specific database query path)
  • Improved MTTR and fewer “blind” performance incidents
  • Evidence-driven performance improvements and release validation

Startup/small-team example: SaaS on Cloud Run (fast debugging without running infra)

  • Problem: A small SaaS team runs a Cloud Run API that calls a managed database and a third-party billing API. They see periodic timeouts and need a simple way to identify the slow step.
  • Proposed architecture:
  • Instrument the Cloud Run service with OpenTelemetry
  • Export to Cloud Trace
  • Add trace correlation to application logs
  • Why Cloud Trace was chosen:
  • Minimal ops: no tracing cluster, no storage management
  • Quick visibility into slow endpoints and external calls
  • Expected outcomes:
  • Identify whether slowdowns are cold starts, DB latency, or third-party API latency
  • Faster fixes (timeouts, retries, caching) and better customer experience

16. FAQ

1) What is a trace vs a span?

A trace represents a single request/transaction end-to-end. A span is one timed operation within that trace (like an HTTP call or DB query).

2) Do I need OpenTelemetry to use Cloud Trace?

No, but OpenTelemetry is a common and recommended approach for modern instrumentation. Cloud Trace can ingest spans written via supported client libraries/exporters.

3) Is Cloud Trace only for Google Cloud workloads?

It’s primarily used for Google Cloud, but you can export traces from other environments if they can authenticate and reach the Cloud Trace API. Confirm networking and auth requirements for your environment.

4) How do I control tracing overhead?

Use sampling (probabilistic/head sampling) and avoid creating excessive spans or large attributes. Use batch exporting.

5) Why are my traces fragmented across services?

Most often it’s broken trace context propagation. Ensure inbound/outbound headers are forwarded and your libraries are configured consistently.

6) Can I correlate Cloud Logging logs with Cloud Trace traces?

Yes, if logs include the trace ID correlation fields. Many Google Cloud runtimes support correlation patterns, and you can also implement structured logging with trace fields.

7) Does Cloud Trace support gRPC?

Tracing gRPC depends on your instrumentation library (for example OpenTelemetry gRPC instrumentation). The tracing backend stores spans regardless of protocol.

8) Can I use Cloud Trace for alerting?

Cloud Trace is mainly for analysis and debugging. Use Cloud Monitoring for alert policies and SLO-based alerting.

9) How do I name services so they’re easy to filter?

Set OpenTelemetry service.name consistently (and optionally service.version, environment attributes). Avoid random or per-instance names.

10) What permissions does a workload need to write spans?

Typically a role like roles/cloudtrace.agent on the project. Confirm in: https://cloud.google.com/trace/docs/iam

11) Are traces retained forever?

Retention is not “forever.” Retention and query windows may be limited and can change by product policy. Verify in official docs for current retention behavior.

12) Can I export traces out of Cloud Trace to another system?

This depends on what export mechanisms are supported at the moment. Many teams instead export from OpenTelemetry collectors to multiple backends. Verify current export options in official docs.

13) Why do I see traces for some endpoints but not others?

Possible causes: – sampling decisions exclude certain requests – instrumentation missing on some routes – errors in exporter – time range filter in UI

14) How do I trace asynchronous flows (Pub/Sub, background jobs)?

You need to propagate trace context through message attributes/metadata and continue the trace in the consumer. This is an application design and instrumentation task.

15) What’s the difference between Cloud Trace and Cloud Profiler?

Cloud Trace shows request-level latency timelines across services. Cloud Profiler shows CPU/heap profiles sampled over time for code-level optimization. They complement each other.

16) Can traces leak sensitive data?

Yes—if you add it to span attributes or names. Treat telemetry as production data and implement attribute hygiene, allowlists, and access controls.

17) Should I run an OpenTelemetry Collector?

For small deployments, direct export from services can be fine. For larger/regulated environments, a collector can centralize sampling, enrichment, and routing. It adds operational complexity.


17. Top Online Resources to Learn Cloud Trace

Resource Type Name Why It Is Useful
Official documentation Cloud Trace docs: https://cloud.google.com/trace/docs Primary source for concepts, API usage, setup guides, and best practices
Official pricing Cloud Trace pricing: https://cloud.google.com/trace/pricing Current pricing dimensions, SKUs, and free-tier thresholds (if any)
Pricing calculator Google Cloud Pricing Calculator: https://cloud.google.com/products/calculator Model expected trace ingestion and overall solution cost
IAM reference Cloud Trace IAM: https://cloud.google.com/trace/docs/iam Role mapping for writers/readers and least-privilege guidance
API reference Cloud Trace API overview: https://cloud.google.com/trace/docs/reference API methods, authentication expectations, quotas and usage patterns
OpenTelemetry on Google Cloud OpenTelemetry guidance (entry point): https://cloud.google.com/stackdriver/docs/instrumentation/setup/otel Google Cloud guidance for using OpenTelemetry with Observability tools (verify current page structure)
Google Cloud Observability Observability overview: https://cloud.google.com/products/operations How Trace fits with Monitoring, Logging, Error Reporting, Profiler
Cloud Run observability Cloud Run monitoring/troubleshooting docs: https://cloud.google.com/run/docs Practical runtime context for tracing and troubleshooting Cloud Run apps
Official samples (GitHub) GoogleCloudPlatform GitHub: https://github.com/GoogleCloudPlatform Search for official samples and instrumentation examples (verify repo relevance and maintenance)
Community learning OpenTelemetry documentation: https://opentelemetry.io/docs/ Vendor-neutral concepts, SDK guides, sampling, context propagation

18. Training and Certification Providers

Institute Suitable Audience Likely Learning Focus Mode Website URL
DevOpsSchool.com DevOps engineers, SREs, platform teams Google Cloud operations, observability, DevOps practices Check website https://www.devopsschool.com/
ScmGalaxy.com Beginners to intermediate engineers DevOps fundamentals, tooling, process Check website https://www.scmgalaxy.com/
CLoudOpsNow.in Cloud operations teams CloudOps practices, operations, monitoring Check website https://www.cloudopsnow.in/
SreSchool.com SREs and reliability-focused engineers SRE practices, reliability engineering, monitoring/tracing concepts Check website https://www.sreschool.com/
AiOpsSchool.com Ops teams adopting automation AIOps concepts, operations analytics Check website https://www.aiopsschool.com/

19. Top Trainers

Platform/Site Likely Specialization Suitable Audience Website URL
RajeshKumar.xyz Cloud/DevOps training content (verify offerings) Engineers seeking guided training https://www.rajeshkumar.xyz/
devopstrainer.in DevOps training platform (verify curriculum) Beginners to DevOps practitioners https://www.devopstrainer.in/
devopsfreelancer.com DevOps freelance/training resource (verify services) Teams seeking short-term help or coaching https://www.devopsfreelancer.com/
devopssupport.in DevOps support/training resource (verify services) Ops teams needing assistance https://www.devopssupport.in/

20. Top Consulting Companies

Company Likely Service Area Where They May Help Consulting Use Case Examples Website URL
cotocus.com Cloud/DevOps consulting (verify offerings) Architecture, DevOps pipelines, operations improvements Set up observability baseline, implement tracing standards, cost optimization review https://cotocus.com/
DevOpsSchool.com DevOps consulting and enablement (verify offerings) Training + consulting engagement for DevOps/SRE Implement OpenTelemetry instrumentation strategy, define SRE runbooks https://www.devopsschool.com/
DEVOPSCONSULTING.IN DevOps consulting (verify offerings) Tooling, automation, operations Deploy observability stack, refine IAM and governance for telemetry https://www.devopsconsulting.in/

21. Career and Learning Roadmap

What to learn before Cloud Trace

  • HTTP fundamentals, REST/gRPC basics
  • Microservices basics (service boundaries, dependencies)
  • Google Cloud basics:
  • projects, IAM, service accounts
  • Cloud Run or GKE fundamentals
  • Observability basics:
  • logs vs metrics vs traces
  • latency percentiles (P50/P95/P99)

What to learn after Cloud Trace

  • Cloud Monitoring:
  • SLI/SLO design
  • alerting strategies
  • Advanced OpenTelemetry:
  • collectors, processors, sampling policies
  • semantic conventions
  • baggage and context propagation patterns
  • Incident management:
  • runbooks, postmortems, error budgets
  • Performance engineering:
  • profiling, load testing, capacity planning

Job roles that use it

  • Site Reliability Engineer (SRE)
  • DevOps Engineer / Platform Engineer
  • Cloud Architect
  • Backend Engineer (microservices)
  • Operations Engineer / Production Engineer

Certification path (if available)

Google Cloud certifications don’t certify “Cloud Trace” specifically, but distributed tracing concepts appear in: – Professional Cloud DevOps Engineer – Professional Cloud Architect (observability architecture is often relevant)

Verify current certification outlines: – https://cloud.google.com/learn/certification

Project ideas for practice

  1. Instrument two Cloud Run services calling each other; ensure trace context propagates end-to-end.
  2. Add a database dependency and capture query spans.
  3. Implement sampling changes between dev and prod and measure cost/visibility tradeoffs.
  4. Correlate logs with trace IDs and build an incident runbook for “slow endpoint” investigation.
  5. Use an OpenTelemetry Collector to enrich spans (service version, environment) before exporting.

22. Glossary

  • Observability: Ability to understand a system’s internal state from external signals (logs, metrics, traces).
  • Trace: End-to-end record of a request as it flows through services.
  • Span: A timed operation within a trace (has start/end time and metadata).
  • Trace ID: Identifier shared across spans belonging to the same trace.
  • Span ID: Identifier for an individual span.
  • Parent/Child span: Relationship that represents nested operations.
  • Context propagation: Passing trace context across process/service boundaries (often via headers).
  • Sampling: Recording only a subset of traces/spans to reduce overhead and cost.
  • Head sampling: Sampling decision made at the start of a request.
  • Tail sampling: Sampling decision made after observing the trace (often via a collector).
  • OpenTelemetry (OTel): Open standard and set of libraries for generating and exporting telemetry.
  • Exporter: Component that sends telemetry data to a backend (Cloud Trace, Jaeger, etc.).
  • ADC (Application Default Credentials): Google Cloud’s standard mechanism for workloads to authenticate to APIs using service accounts.

23. Summary

Cloud Trace is Google Cloud’s managed distributed tracing service in the Observability and monitoring category. It helps you see end-to-end request latency across services by collecting and analyzing traces composed of spans.

It matters because modern systems fail and slow down in distributed ways: metrics tell you symptoms, but Cloud Trace shows the path and timing that leads you to the root cause. It fits best alongside Cloud Monitoring and Cloud Logging as part of a practical Google Cloud observability stack.

From a cost perspective, the biggest drivers are trace/span ingestion volume and sampling choices—plan sampling intentionally and avoid over-instrumentation. From a security perspective, apply least-privilege IAM, avoid sensitive span attributes, and treat trace metadata as production data.

Use Cloud Trace when you need managed tracing tightly integrated with Google Cloud. As a next step, extend the lab by adding multi-service propagation, structured log correlation, and production-grade sampling policies using OpenTelemetry.