Disable opencensus telemetry when unit tests are executed
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 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.
@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.
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,
)
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?
You have to pass in your instrumentation key in place of the 0s in order to get this to work.