Skip to main content

Context-managed metrics tracking and output, including but not limited to process/subprocess latencies.

Project description

The goblinfish.metrics.trackers Package

Provides context-manager classes to name, track and report elapsed-time and other metrics for top-level process entry-points (like AWS Lambda Function handlers) and sub-processes within them.

Quick Start

Install in your project:

# Install with pip
pip install goblinfish-metrics-trackers
# Install with pipenv
pipenv install goblinfish-metrics-trackers

Import in your code:

from goblinfish.metrics.trackers import ProcessTracker

Create the timing-tracker instance:

tracker = ProcessTracker()

Decorate your top-level/entry-point function:

@tracker.track
def some_function():
    ...

Add any sub-process timers:

@tracker.track
def some_function():
    ...

    with tracker.timer('some_process_name'):
        # Do stuff here
        ...

When this code is executed, after the context created by the @tracker.track decorator is complete, it will print something that looks like this:

{"some_process_name": 0.0, "some_function": 0.0}

More detailed examples can be found in the examples directory in the repository.

Behavior in an asyncio context

This version will work with processes running under asyncio, for example:

with tracker.timer('some_async_process'):
    async.run(some_function())

but it may only capture the time needed for the async tasks/coroutines to be created rather than how long it takes for any of them to execute, depending on the implementation pattern used. A more useful approach, shown in the li-article-async-example.py module in the examples directory is to encapsulate the async processes in an async function, then wrap all of that function's processes that need to be timed in the context manager. For example:

async def get_person_data():
    sleep_for = random.randrange(2_000, 3_000) / 1000
    print(
        f'get_person_data sleeping for {sleep_for} seconds'
    )
    with tracker.timer('get_person_data'):
        await asyncio.sleep(sleep_for)
    return {'person_data': ('Professor Plum', dict())}

…which will contribute to the logged/printed output in a more meaningful fashion:

// get_person_data sleeping for 2.054
{
    "get_person_data": 2.0551600456237793,
    "main": 2.8084700107574463
}

Contribution guidelines

At this point, contributions are not accepted — I need to finish configuring the repository, deciding on whether I want to set up automated builds for pull-requests, and probably several other items.

Who do I talk to?

The current maintainer(s) will always be listed in the [maintainers] section of the pyproject.toml file in the repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

goblinfish_metrics_trackers-0.0.5.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

goblinfish_metrics_trackers-0.0.5-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file goblinfish_metrics_trackers-0.0.5.tar.gz.

File metadata

File hashes

Hashes for goblinfish_metrics_trackers-0.0.5.tar.gz
Algorithm Hash digest
SHA256 5171130dfde093fb22f5fd51f6677cb2312727144ff98bb1a034ce0e84d44383
MD5 8b97c44179573189cf0b829a8814882f
BLAKE2b-256 92437316c8929389d8607a12a208bd3d14ee74a2a7803988b82f4d614d6be48a

See more details on using hashes here.

File details

Details for the file goblinfish_metrics_trackers-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for goblinfish_metrics_trackers-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 89e4fc226e76efb663cc6e254dbcda02f6e1efc27cb899af442235c40deb335f
MD5 d84e5a67b2f598543bce19a91270b425
BLAKE2b-256 70084ddfe5eab5d475a918f4d71fd98a6688e0c1424a6ae72d0ea7b11e6f8d74

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page