opencensus-python icon indicating copy to clipboard operation
opencensus-python copied to clipboard

Disable opencensus telemetry when unit tests are executed

Open anandchugh opened this issue 6 years ago • 5 comments

Describe your environment. Describe any aspect of your environment relevant to the problem, including your Python version, platform, version numbers of installed dependencies, information about your cloud hosting provider, etc. If you're reporting a problem with a specific version of a library in this repo, please check whether the problem has been fixed on master.

Steps to reproduce. We have added opencensus in our python flask application. While running unit tests we are creating opencensus AzureLogHandler and Tracer using random uuid. Because of this we are lots of errors when we run unit tests

ERROR:opencensus.ext.azure.common.transport:Non-retryable server side error 400: {"itemsReceived":4,"itemsAccepted":0,"errors":[{"index":0,"statusCode":400,"message":"Invalid instrumentation key"},{"index":1,"statusCode":400,"message":"Invalid instrumentation key"},{"index":2,"statusCode":400,"message":"Invalid instrumentation key"},{"index":3,"statusCode":400,"message":"Invalid instrumentation key"}]}.

Is there a way to disable opencensus telemetry when unittests are executed?

What is the expected behavior? We were not expecting any errors.

What is the actual behavior? What did you see instead? We are seeing errors mentioned above Additional context. Add any other context about the problem here.

anandchugh avatar Apr 22 '20 20:04 anandchugh

@anandchugh Is it possible to just not instantiate these classes when running your unit tests? The outcome of your tests should not depend on the behaviour of opencensus classes.

lzchen avatar Apr 23 '20 00:04 lzchen

@anandchugh Is it possible to just not instantiate these classes when running your unit tests? The outcome of your tests should not depend on the behaviour of opencensus classes.

Since we have added logging to all the classes, hence during our unittests logging will get called which will then try to send opencensus telemetry.

anandchugh avatar Apr 23 '20 03:04 anandchugh

If absolutely MUST have AzureLogHandler and AzureExporter within your unittests, you can turn the sample rates to 0 to disable tracking of telemetry.

tracer = Tracer(
    exporter=AzureExporter(
        instrumentation_key='00000000-0000-0000-0000-000000000000',
    ),
    sampler=AlwaysOffSampler(),
)
handler = AzureLogHandler(
    instrumentation_key='00000000-0000-0000-0000-000000000000',
    logging_sampling_rate=0.0,
)

lzchen avatar Apr 23 '20 03:04 lzchen

instrumentation_key

I tried this and getting exception HANDLER = AzureLogHandler(instrumentation_key='00000000-0000-0000-0000-000000000000', logging_sampling_rate=0.0)

Exception has occurred: ValueError Invalid instrumentation key.

Is there a working sample for this?

anandchugh avatar May 18 '20 10:05 anandchugh

You have to pass in your instrumentation key in place of the 0s in order to get this to work.

lzchen avatar May 18 '20 17:05 lzchen