0

我尝试使用以下指南安装和使用stats_exporteropencensus.ext.stackdriveropencensus -ext-stackdriver

通过 pip 安装后:

pip install opencensus-ext-stackdriver

我正在尝试导入它并且:

from opencensus.ext.stackdriver import stats_exporter as stackdriver
ImportError: cannot import name 'stats_exporter' from 'opencensus.ext.stackdriver'

在比较 Git repo 和我的本地venv/lib/python3.7/site-packages/... 时,似乎 pip 版本与 Github 不兼容,所以我尝试通过克隆安装它,并使用setup.py

pip install ../opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz

这给了我以下错误:

(venv) Yehoshaphats-MacBook-Pro:present-value yehoshaphatschellekens$ pip install ../opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz 
Processing /Users/yehoshaphatschellekens/opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/s2/y6vcdc1105s8xlpb12slr9z00000gn/T/pip-req-build-7m1ibdpd/setup.py", line 17, in <module>
        from version import __version__
    ModuleNotFoundError: No module named 'version'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/s2/y6vcdc1105s8xlpb12slr9z00000gn/T/pip-req-build-7m1ibdpd/

这种类型的类似错误表明我需要升级setuptools,也尝试过:(

这篇文章表明它可能与我正在使用 python3 的事实有关,version尽管我确实需要在我的 python3 venv 上安装这个包,但它是不可完成的。

关于这个问题的任何帮助都会很棒!

4

1 回答 1

1

试试这个:

#!/usr/bin/env python

import os

from opencensus.common.transports.async_ import AsyncTransport

from opencensus.ext.stackdriver import trace_exporter as stackdriver_exporter

from opencensus.trace import tracer as tracer_module
from opencensus.stats import stats as stats_module


def main():
    sde = stackdriver_exporter.StackdriverExporter(
                project_id=os.environ.get("PROJECT_ID"),
                transport=AsyncTransport)

    tracer = tracer_module.Tracer(exporter=sde)
    with tracer.span(name='doingWork') as span:
        for i in range(10):
            continue


if __name__ == "__main__":
    main()

grpcio==1.19.0
opencensus==0.3.1
opencensus-ext-stackdriver==0.1.1

注意OpenCensus 库也需要 gRPC。

你会需要:

  • 一个 GCP 项目及其项目 ID ( ${PROJECT_ID})
  • 服务帐户roles/cloudtrace.agent及其密钥。

然后:

virtualenv venv
source venv/bin/activate

export PROJECT_ID=[[YOUR-PROJECT-ID]]
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/key.json

pip3 install --requirement requirements.txt
python3 stackdriver.py
于 2019-04-05T01:24:06.533 回答