我有几个 Cloud Functions 可以远程调用 3rd 方 API,我希望能够在 Cloud Trace 中为这些调用收集延迟指标。
我正在尝试找到可以构建的准系统示例代码。我发现只有一个在https://medium.com/faun/tracing-python-cloud-functions-a17545586359
本质上是这样。
import requests
from opencensus.trace import tracer as tracer_module
from opencensus.trace.exporters import stackdriver_exporter
from opencensus.trace.exporters.transports.background_thread \
import BackgroundThreadTransport
PROJECT_ID = 'test-trace'
# instantiate trace exporter
exporter = stackdriver_exporter.StackdriverExporter(project_id=PROJECT_ID, transport=BackgroundThreadTransport)
def main_fun(data, context):
tracer = tracer_module.Tracer(exporter=exporter)
with tracer.span(name='get_token'):
print('Getting Token')
authtoken = get_token(email,password)
print('Got Token')
def get_token(email,password):
# Make some request out to an API and get a Token
return accesstoken
没有错误,一切都按预期工作,但没有出现在 Cloud Trace 或 Stackdriver 中的跟踪。
我在这里做错了吗?有没有人有一些简单的代码可能适用于 Cloud Function 中的 Cloud Trace