I have a Flask app talking to a Python gRPC service, both deployed on Google Cloud Run. I can see traces on Google Trace after instrumenting the apps, but they all appear to have different Trace IDs which means the traces are not being linked together between the two services. This is my setup code for tracing on both services with grpc/Flask instrumentors setup on each side:
import logging
from opentelemetry import trace
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
from opentelemetry.propagators import set_global_textmap
from opentelemetry.tools.cloud_trace_propagator import CloudTraceFormatPropagator
from google.auth.exceptions import DefaultCredentialsError
logger = logging.getLogger(__name__)
def setup_tracing():
"""
Setup Tracing on Google Cloud. The Service Account Roles must have `Cloud Trace Agent`
Role added for traces to be ingested.
"""
trace.set_tracer_provider(TracerProvider())
try:
# If running on Google Cloud, will use instance metadata service account credentials to initialize
trace.get_tracer_provider().add_span_processor(
SimpleExportSpanProcessor(CloudTraceSpanExporter())
)
# Using the X-Cloud-Trace-Context header
set_global_textmap(CloudTraceFormatPropagator())
logger.info("Tracing Setup. Exporting Traces to Google Cloud.")
except DefaultCredentialsError:
# Not running on Google Cloud so will use console exporter
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
trace.get_tracer_provider().add_span_processor(
SimpleExportSpanProcessor(ConsoleSpanExporter())
)
logger.info("Tracing Setup. Exporting Traces to Console.")
Locally I can see with the ConsoleSpanExporter that the Trace IDs on both services match, however on Google Cloud Run they clearly don't resulting in separate traces on Google Trace, so I'm wondering if the Networking removes the headers between services or something else is happening which means the Trace ID isn't being propagated?
As an extra note I've also noticed that the load balancer in front of Cloud Run's Trace/Span IDs aren't being propagated using CloudTraceSpanFormatPropagator() which makes my logs messy too as the logs aren't nested together for requests.