Cloud Run 中未跟踪数据存储 grpc

问题描述 投票:0回答:1

我有一个用于跟踪 Python 后端的工具,它使用 NDB 与数据存储区交互,我遵循将跟踪导出到 Cloud Trace 的经典方法:

from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.propagate import set_global_textmap
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.grpc import GrpcInstrumentorClient
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.propagators.cloud_trace_propagator import CloudTraceFormatPropagator
from fastapi import FastAPI
from google.cloud import ndb

app = FastAPI()

client = ndb.Client()

class MyEntity(ndb.Model):
    name = ndb.StringProperty()
    value = ndb.IntegerProperty()

@app.get("/entities")
def get_entities():
    with client.context():
        entities = MyEntity.query().fetch(20)
        results = [
            {
                'id': entity.key.id(),
                'name': entity.name,
                'value': entity.value
            } for entity in entities
        ]
    return results

tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BatchSpanProcessor(CloudTraceSpanExporter()))
trace.set_tracer_provider(tracer_provider)
set_global_textmap(CloudTraceFormatPropagator())

FastAPIInstrumentor().instrument_app(app, excluded_urls=EXCLUDED_PATHS_REGEX)

grpc_client_instrumentor = GrpcInstrumentorClient()
grpc_client_instrumentor.instrument(
        tracer_provider=tracer_provider
    )

当我在本地测试仪器时(使用数据存储模拟器),我看到了数据存储 grpc 调用的跨度:

datastore spans

具有适当的属性:

enter image description here

但是,部署到 Cloud Run 后,当我检查已发送请求的跟踪时,跟踪仅显示该请求的一个具有延迟的跨度,仅此而已。跟踪的检查还显示“缺少跨度 ID ..” ”。要在 Cloud Run 中跟踪来自 Datastore 的 grpc 调用,还需要什么?

google-cloud-platform google-cloud-datastore google-cloud-run open-telemetry google-cloud-trace
1个回答
0
投票

事实证明,该问题与代码无关,而是与 Cloud Run 配置相关。 Cloud Run 的服务帐户必须具有角色

Cloud Trace Agent

© www.soinside.com 2019 - 2024. All rights reserved.