In this post, I want to share how to export the traces generated by your FastAPI application to the GCP observability platform.
There are mainly two ways you can export your traces to the GCP trace platform.
Export via opentelemetry-exporter-gcp-trace#
To use gcp-trace exporter in your application, you need to install opentelemetry-exporter-gcp-trace.
resource = Resource.create(
attributes={
"service.name": "demo-service",
},
)
provider = TracerProvider(
resource=resource,
)
cloud_trace_exporter = CloudTraceSpanExporter(
project_id="my-demo-project",
resource_regex=r".*",
)
cloud_trace_processor = BatchSpanProcessor(
cloud_trace_exporter,
)
provider.add_span_processor(cloud_trace_processor)The parameter resource_regex is important:
When exporting the traces to GCP cloud trace, the service name is missing from the Trace Explorer and empty.
This is due to how CloudTraceSpanExporter works.
By default, the resource label is copied to the spans by this exporter.
You have to provide the resource labels that you want to copy to span via resource_regex parameter.
For example, to only copy labels starting with service, using this:
cloud_trace_exporter = CloudTraceSpanExporter(
project_id="my-demo-project",
resource_regex=r"service\..*",
)With this GCP trace exporter package, all the work is done behind the hood. However, it is using google’s proprietary trace API and not fully compatible with OpenTelemetry. That is why the ugly “resource_regex” exists.
Export via GCP telemetry API#
Starting Feb. 2026, GCP supports the OTLP natively via its telemetry API.
Using the generic OTLP export and export to google telemetry api. There is an official example using gRPC here.
When I run the official example locally, I have met this error:
opentelemetry.exporter.otlp.proto.grpc.exporter:466 - ERROR - Failed to export traces to telemetry.googleapis.com:443, error code: StatusCode.INVALID_ARGUMENTThis is because the GCP project_id is not set up in the official code.
We need to set up the project id via either:
- env variable,
export OTEL_RESOURCE_ATTRIBUTES="gcp.project_id=my-demo-project" - resource attributes in the code itself
This is a sample config for reference:
import google.auth
import google.auth.transport.grpc
import google.auth.transport.requests
import grpc
from fastapi import FastAPI
from google.auth.transport.grpc import AuthMetadataPlugin
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
## note the `gcp.project_id` must be valid
resource = Resource.create(
attributes={
"service.name": "demo-service",
"gcp.project_id": "my-demo-project",
},
)
provider = TracerProvider(
resource=resource,
)
## credential setup
credentials, _ = google.auth.default()
request = google.auth.transport.requests.Request()
auth_metadata_plugin = AuthMetadataPlugin(credentials=credentials, request=request)
channel_creds = grpc.composite_channel_credentials(
grpc.ssl_channel_credentials(),
grpc.metadata_call_credentials(auth_metadata_plugin),
)
## connect OTLPSpanExporter to the gcp telemetry api
processor = BatchSpanProcessor(
OTLPSpanExporter(
credentials=channel_creds,
endpoint="https://telemetry.googleapis.com:443",
),
)
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)See also the official migration guide.