Search code examples
pythonopencensus

Trouble shooting when tring to install and import `stats_exporter` from `opencensus.ext.stackdriver`


Im trying to install and use stats_exporter from opencensus.ext.stackdriver using the following guide: opencensus-ext-stackdriver

after installing it through pip:

pip install opencensus-ext-stackdriver

Im trying to import it and:

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

When comparing the Git repo, and my local venv/lib/python3.7/site-packages/... it seems like the pip version isn't compatible with Github , so i tried to install it though cloning, and using setup.py

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

which gives me the following error:

(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/

Similar errors of this type indicated that i need to upgrade setuptools, tried that also :(

This post suggests that it might related to the fact that i'm using python3, which isn't completable with version though i really need to install this package on my python3 venv.

Any Help on this issue would be great!


Solution

  • Try this:

    #!/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()
    

    and

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

    NB The OpenCensus libraries need gRPC too.

    You will need:

    • a GCP Project and it's Project ID (${PROJECT_ID})
    • a service account with roles/cloudtrace.agent and its key.

    Then:

    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