# opencensus

> A stats collection and distributed tracing framework

- **URL**: https://www.freshcrate.ai/projects/opencensus
- **Author**: OpenCensus Authors
- **Category**: Frameworks
- **Latest version**: `0.11.4` (2026-04-21)
- **License**: Apache-2.0
- **Source**: https://github.com/census-instrumentation/opencensus-python
- **Language**: Python
- **GitHub**: 674 stars, 247 forks
- **Registry**: pypi (`opencensus`)
- **Tags**: `pypi`

## Description

OpenCensus - A stats collection and distributed tracing framework
=================================================================

|gitter|
|travisci|
|circleci|
|pypi|
|compat_check_pypi|
|compat_check_github|


.. |travisci| image:: https://travis-ci.org/census-instrumentation/opencensus-python.svg?branch=master
    :target: https://travis-ci.org/census-instrumentation/opencensus-python
.. |circleci| image:: https://circleci.com/gh/census-instrumentation/opencensus-python.svg?style=shield
   :target: https://circleci.com/gh/census-instrumentation/opencensus-python
.. |gitter| image:: https://badges.gitter.im/census-instrumentation/lobby.svg
   :target: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
.. |pypi| image:: https://badge.fury.io/py/opencensus.svg
   :target: https://pypi.org/project/opencensus/
.. |compat_check_pypi| image:: https://python-compatibility-tools.appspot.com/one_badge_image?package=opencensus
   :target: https://python-compatibility-tools.appspot.com/one_badge_target?package=opencensus
.. |compat_check_github| image:: https://python-compatibility-tools.appspot.com/one_badge_image?package=git%2Bgit%3A//github.com/census-instrumentation/opencensus-python.git
   :target: https://python-compatibility-tools.appspot.com/one_badge_target?package=git%2Bgit%3A//github.com/census-instrumentation/opencensus-python.git

`OpenCensus`_ for 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.

.. _OpenCensus: https://github.com/census-instrumentation

-  `API Documentation`_

.. _API Documentation: https://opencensus.io/api/python/trace/usage.html

--------
 Tracing
--------

Installation & basic usage
--------------------------

1. Install the opencensus package using `pip`_ or `pipenv`_:

    ::

        pip install opencensus
        pipenv install opencensus

2. Initialize a tracer for your application:

    .. code:: python

        from opencensus.trace.tracer import Tracer
        from opencensus.trace.samplers import AlwaysOnSampler

        tracer = Tracer(sampler=AlwaysOnSampler())

    .. _pip: https://pip.pypa.io
    .. _pipenv: https://docs.pipenv.org/

3. Initialize a view_manager and a stats_recorder for your application:

    .. code:: python

        from opencensus.stats import stats as stats_module

        stats = stats_module.stats
        view_manager = stats.view_manager
        stats_recorder = stats.stats_recorder


Usage
-----

You can collect traces using the ``Tracer`` `context manager`_:

.. code:: python

    from opencensus.trace.tracer import Tracer
    from opencensus.trace.samplers import AlwaysOnSampler

    # Initialize a tracer, by default using the `PrintExporter`
    tracer = Tracer(sampler=AlwaysOnSampler())

    # Example for creating nested spans
    with tracer.span(name='span1'):
        do_something_to_trace()
        with tracer.span(name='span1_child1'):
            do_something_to_trace()
        with tracer.span(name='span1_child2'):
            do_something_to_trace()
    with tracer.span(name='span2'):
        do_something_to_trace()

OpenCensus will collect everything within the ``with`` statement as a single span.

Alternatively, you can explicitly start and end a span:

.. code:: python

    from opencensus.trace.tracer import Tracer
    from opencensus.trace.samplers import AlwaysOnSampler

    # Initialize a tracer, by default using the `PrintExporter`
    tracer = Tracer(sampler=AlwaysOnSampler())

    tracer.start_span(name='span1')
    do_something_to_trace()
    tracer.end_span()


.. _context manager: https://docs.python.org/3/reference/datamodel.html#context-managers


Customization
-------------

There 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 are ``PrintExporter``, ``FileExporter`` and ``LoggingExporter``, the
  other exporters are provided as `extensions <#trace-exporter>`__.

* **Sampler**, which determines how traces are sampled.
  The default sampler is the ``ProbabilitySampler``, 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, see ``AlwaysOnSampler`` and
  ``AlwaysOffSampler``.

* **Propagator**, which serializes and deserializes the
  ``SpanContext`` and its headers. The default propagator is
  ``Tr

## Recent releases

| Version | Date | Urgency | Changes |
| --- | --- | --- | --- |
| `0.11.4` | 2026-04-21 | Low | Imported from PyPI (0.11.4) |
| `opencensus-ext-azure@1.1.15` | 2025-06-03 | Low | Switch ordering for Statsbeat Attach detection to prioritize Azure Functions   ([#1251](https://github.com/census-instrumentation/opencensus-python/pull/1251)) |
| `opencensus-ext-azure@1.1.15` | 2025-06-03 | Low | Switch ordering for Statsbeat Attach detection to prioritize Azure Functions   ([#1251](https://github.com/census-instrumentation/opencensus-python/pull/1251)) |
| `opencensus-ext-azure@1.1.15` | 2025-06-03 | Low | Switch ordering for Statsbeat Attach detection to prioritize Azure Functions   ([#1251](https://github.com/census-instrumentation/opencensus-python/pull/1251)) |
| `opencensus-ext-azure@1.1.15` | 2025-06-03 | Low | Switch ordering for Statsbeat Attach detection to prioritize Azure Functions   ([#1251](https://github.com/census-instrumentation/opencensus-python/pull/1251)) |
| `opencensus-ext-azure@1.1.15` | 2025-06-03 | Low | Switch ordering for Statsbeat Attach detection to prioritize Azure Functions   ([#1251](https://github.com/census-instrumentation/opencensus-python/pull/1251)) |
| `opencensus-ext-azure@1.1.15` | 2025-06-03 | Low | Switch ordering for Statsbeat Attach detection to prioritize Azure Functions   ([#1251](https://github.com/census-instrumentation/opencensus-python/pull/1251)) |
| `opencensus-ext-azure@1.1.15` | 2025-06-03 | Low | Switch ordering for Statsbeat Attach detection to prioritize Azure Functions   ([#1251](https://github.com/census-instrumentation/opencensus-python/pull/1251)) |
| `opencensus-ext-azure@1.1.15` | 2025-06-03 | Low | Switch ordering for Statsbeat Attach detection to prioritize Azure Functions   ([#1251](https://github.com/census-instrumentation/opencensus-python/pull/1251)) |
| `opencensus-ext-azure@1.1.15` | 2025-06-03 | Low | Switch ordering for Statsbeat Attach detection to prioritize Azure Functions   ([#1251](https://github.com/census-instrumentation/opencensus-python/pull/1251)) |

## Citation

- HTML: https://www.freshcrate.ai/projects/opencensus
- Markdown: https://www.freshcrate.ai/projects/opencensus.md
- Dependencies JSON: https://www.freshcrate.ai/api/projects/opencensus/deps

_Generated by freshcrate.ai. Indexes pypi releases for AI-agent ecosystem packages._
