Package: opencensus
OpenCensusfor Python. OpenCensus provides a framework to measure a
server’s resource usage and collect performance stats. This repository
contains Python related utilities and supporting software needed by
OpenCensus.API DocumentationTracingInstallation & basic usageInstall the opencensus package usingpiporpipenv:pip install opencensus
pipenv install opencensusInitialize a tracer for your application:fromopencensus.trace.tracerimportTracerfromopencensus.trace.samplersimportAlwaysOnSamplertracer=Tracer(sampler=AlwaysOnSampler())Initialize a view_manager and a stats_recorder for your application:fromopencensus.statsimportstatsasstats_modulestats=stats_module.statsview_manager=stats.view_managerstats_recorder=stats.stats_recorderUsageYou can collect traces using theTracercontext manager:fromopencensus.trace.tracerimportTracerfromopencensus.trace.samplersimportAlwaysOnSampler# Initialize a tracer, by default using the `PrintExporter`tracer=Tracer(sampler=AlwaysOnSampler())# Example for creating nested spanswithtracer.span(name='span1'):do_something_to_trace()withtracer.span(name='span1_child1'):do_something_to_trace()withtracer.span(name='span1_child2'):do_something_to_trace()withtracer.span(name='span2'):do_something_to_trace()OpenCensus will collect everything within thewithstatement as a single span.Alternatively, you can explicitly start and end a span:fromopencensus.trace.tracerimportTracerfromopencensus.trace.samplersimportAlwaysOnSampler# Initialize a tracer, by default using the `PrintExporter`tracer=Tracer(sampler=AlwaysOnSampler())tracer.start_span(name='span1')do_something_to_trace()tracer.end_span()CustomizationThere are several things you can customize in OpenCensus:Excludelist, which excludes certain hosts and paths from being tracked.
By default, the health check path for the App Engine flexible environment is
not tracked, you can turn it on by excluding it from the excludelist setting.Exporter, which sends the traces.
By default, the traces are printed to stdout in JSON format. You can choose
different exporters to send the traces to. There are three built-in exporters,
which arePrintExporter,FileExporterandLoggingExporter, the
other exporters are provided asextensions.Sampler, which determines how traces are sampled.
The default sampler is theProbabilitySampler, which samples (i.e.
enables tracing for) a percentage of all requests. Sampling is deterministic
according to the trace ID. To force sampling for all requests, or to prevent
any request from being sampled, seeAlwaysOnSamplerandAlwaysOffSampler.Propagator, which serializes and deserializes theSpanContextand its headers. The default propagator isTraceContextPropagator, other propagators includeBinaryFormatPropagator,GoogleCloudFormatPropagatorandTextFormatPropagator.You can customize while initializing a tracer.importrequestsfromopencensus.traceimportconfig_integrationfromopencensus.traceimportfile_exporterfromopencensus.traceimporttracerastracer_modulefromopencensus.trace.propagationimportgoogle_cloud_formatfromopencensus.trace.samplersimportProbabilitySamplerconfig_integration.trace_integrations(['httplib'])tracer=tracer_module.Tracer(exporter=file_exporter.FileExporter(file_name='traces'),propagator=google_cloud_format.GoogleCloudFormatPropagator(),sampler=ProbabilitySampler(rate=0.5),)withtracer.span(name='parent'):withtracer.span(name='child'):response=requests.get('http://localhost:5000')You can use a configuration file for Flask/Django/Pyramid. For more
information, please read theindividual integration documentation.'OPENCENSUS':{'TRACE':{'EXCLUDELIST_HOSTNAMES':['localhost','127.0.0.1'],'EXCLUDELIST_PATHS':['_ah/health'],'SAMPLER':'opencensus.trace.samplers.ProbabilitySampler(rate=1)','EXPORTER':'''opencensus.ext.ocagent.trace_exporter.TraceExporter(
            service_name='foobar',
        )''','PROPAGATOR':'opencensus.trace.propagation.google_cloud_format.GoogleCloudFormatPropagator()',}}ExtensionsIntegrationOpenCensus supports integration with popular web frameworks, client libraries and built-in libraries.DjangoFlaskgeventGoogle Cloud Client LibrariesgRPChttplibhttpxloggingMySQLPostgreSQLpymongoPyMySQLPyramidrequestsSQLAlchemythreadingLog ExporterAzureMetrics ExporterAzureStats ExporterOCAgentPrometheusStackdriverTrace ExporterAzureDatadogJaegerOCAgentStackdriverZipkinVersioningThis library followsSemantic Versioning.GA: Libraries defined at a GA quality level are stable, and will not introduce
backwards-incompatible changes in any minor or patch releases. We will address issues and requests
with the highest priority. If we were to make a backwards-incompatible changes on an API, we will
first mark the existing API as deprecated and keep it for 18 months before removing it.Beta: Libraries defined at a Beta quality level are expected to be mostly stable and we’re
working towards their release candidate. We will address issues and requests with a higher priority.
There may be backwards incompatible changes in a minor version release, though not in a patch
release. If an element is part of an API that is only meant to be used by exporters or other
opencensus libraries, then there is no deprecation period. Otherwise, we will deprecate it for 18
months before removing it, if possible.