GoogleCloudPlatform / python-docs-samples

Code samples used on cloud.google.com
Apache License 2.0
7.35k stars 6.4k forks source link

gcp cloud trace and OTEL ResourceAttributes #7300

Closed raphaelauv closed 2 years ago

raphaelauv commented 2 years ago

GCP Cloud run does not seem to respect the ResourceAttributes of OTEL

from opentelemetry import trace
from opentelemetry.semconv.resource import ResourceAttributes
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.propagate import set_global_textmap
from opentelemetry.propagators.cloud_trace_propagator import CloudTraceFormatPropagator

API_CODE_VERSION = os.getenv('API_CODE_VERSION', '0.1')
ENV = os.getenv("ENV", "preprod")

set_global_textmap(CloudTraceFormatPropagator())
tracer_provider = TracerProvider(resource=Resource.create(attributes={
    ResourceAttributes.SERVICE_VERSION: API_CODE_VERSION,
    ResourceAttributes.DEPLOYMENT_ENVIRONMENT: ENV,
    ResourceAttributes.SERVICE_NAME: "toto"
}))
tracer_provider.add_span_processor(BatchSpanProcessor(CloudTraceSpanExporter(project_id=TRACING_GCP_PROJECT_ID)))
trace.set_tracer_provider(tracer_provider)

FastAPIInstrumentor.instrument_app(app=app_fast_api, excluded_urls="/health,/openapi.json")

running this will give span in cloud trace , but the ResourceAttributes do not appear in the filter options of cloud run

Screenshot from 2021-12-27 18-32-22 Screenshot from 2021-12-27 18-32-32

raphaelauv commented 2 years ago

The doc precise that the cloud trace fields (service and version) for filter in the UI are only for App engine

Service | (App Engine only) The App Engine service that   handled the request. For more information, go to App Engine.
Version | (App Engine only) The version of the application that handled the request.
grayside commented 2 years ago

I've had success making this work in the past by taking advantage of the app engine tag naming convention:

{
  "g.co/gae/app/module": $K_SERVICE,
  "g.co/gae/app/version", $K_REVISION
}

I would not count on this being stable/preferred, but it may be a workable hack.

muncus commented 2 years ago

Cloud Trace does not have the notion of a Resource (or a ResourceAttribute), and because there is a strict limit of 32 labels per span, the library by default does not copy ResourceAttributes to Span Labels, but this is configurable using the resource_regex parameter like so:

configure_exporter(CloudTraceSpanExporter(resource_regex="service.*"))

More detail is available in the docs for the resource_regex argument.