goblinfish-metrics-trackers


Namegoblinfish-metrics-trackers JSON
Version 1.0.1 PyPI version JSON
download
home_pageNone
SummaryContext-managed metrics tracking and output, including but not limited to process/subprocess latencies.
upload_time2025-02-03 22:19:54
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseMIT License Copyright (c) 2025 Brian D. Allbee Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords aws cloudwatch log metrics latency process timing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # The goblinfish.metrics.trackers Package

> Provides context-manager classes to name, track and report elapsed-time and other, user-defined metrics for top-level process entry-points (like AWS Lambda Function handlers, which is what it was originally conceived for) and sub-processes within them.

## Quick Start

Install in your project:

```shell
# Install with pip
pip install goblinfish-metrics-trackers
```

```shell
# Install with pipenv
pipenv install goblinfish-metrics-trackers
```

Import in your code:

```python
from goblinfish.metrics.trackers import ProcessTracker
```

Create the timing-tracker instance:

```python
tracker = ProcessTracker()
```

Decorate your top-level/entry-point function:

```python
@tracker
def some_function():
    ...
```

Add any sub-process timers:

```python
@tracker
def some_function():
    ...

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

Decorate any child process functions with the instance's `.track` method:

```python
@tracker
def some_function():
    ...

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

@tracker.track
def some_other_function():
    ...
```

Set any explicit metrics needed:

```python
@tracker
def some_function():
    ...

    with tracker.timer('some_process_name'):
        try:
            some_other_function()
            # Do stuff here
            ...
        except Exception as error:
            # Count of errors to be aggregated
            tracker.set_metric('some_function_errors', 1)
            # Name of error; simple string values are OK too!
            tracker.set_metric(
                'some_function_error_name', error.__class__.__name__
            )
            # Do stuff here
            ...

@tracker.track
def some_other_function():
    ...
```

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

```json
{
    "some_function": 0.000,
    "some_other_function": 0.000,
    "some_process_name": 0.000
}
```

More detailed examples can be found in [the `examples` directory](https://bitbucket.org/stonefish-software-studio/goblinfish-metrics-trackers-package/src/main/examples/) in the repository.

### A top-level `ProcessTracker` instance is *required*

This package was designed around the idea of there being a top-level entry-point function and zero-to-many child functions. Applying a `@tracker.track` decorator to a function that isn't called by the entry-point function decorated with `@tracker` will yield unexpected result, or no results at all.

### Behavior in an `asyncio` context

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

```python
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](https://bitbucket.org/stonefish-software-studio/goblinfish-metrics-trackers-package/src/main/examples/) 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. Stripping that function in the example down to a bare minimum simulation, it would look like this:

```python
async def get_person_data():
    sleep_for = random.randrange(2_000, 3_000) / 1000
    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:

```json
{
    "get_person_data": 2215.262,
    "main": 8465.233
}
```

## 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. That said, if you have an idea that you want to propose as an addition, a bug that you want to call out, etc., please feel free to contact the maintainer(s) (see below).

## Who do I talk to?

The current maintainer(s) will always be listed in the `[maintainers]` section of [the `pyproject.toml` file](https://bitbucket.org/stonefish-software-studio/goblinfish-metrics-trackers-package/src/main/pyproject.toml) in the repository.

## Future plans (To-Dos) and BYOLF

While this package should work nicely for anything that can use a generic JSON log-message format, there are any number of products that are designed to read log-messages and ship them to some other service, usually with their own particular format requirements, in order to provide their own dashboards and alarms. If I have time in the future to start looking into those and writing [extras](https://stackoverflow.com/a/52475030) to accommodate, but I'm not confident that I'll have that time.

In the meantime, if there is a need for a specific log-message format, it's possible to BYOLF (**B**ring **Y**our **O**wn **L**og **F**ormat). Just write your own output function, and provide it as an argument to the `ProcessTracker` instance that is being created to track process items. What that would entail is:

- Writing a function that accepts a single `str` parameter.
- Deserializing that parameter from the JSON value that it will be passed.
- Creating the custom log-message output using whatever data is relevant.
- Writing that log-message in whatever manner is appropriate.

A *very* bare-bones example:

```python
def my_log_formatter(output: str) -> None:
    ...  # Handle the "output" log-line here as needed.

tracker = ProcessTracker(my_log_formatter)

# ...
```

Though this package was designed to issue log-messages in a reasonably standard output (`print` or some [`logging` package](https://docs.python.org/3.11/library/logging.html) functionality), there's no *functional* reason that it couldn't, for example, write data straight to some database, call some third-party API, or whatever else.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "goblinfish-metrics-trackers",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": "Brian Allbee <brian.allbee+goblinfish.trackers@gmail.com>",
    "keywords": "aws, cloudwatch, log, metrics, latency, process timing",
    "author": null,
    "author_email": "Brian Allbee <brian.allbee+goblinfish.trackers@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/4d/ad/4b221e5da98a4fff1062b8e53555baa7122c82901f4ff02ccb32f380f980/goblinfish_metrics_trackers-1.0.1.tar.gz",
    "platform": null,
    "description": "# The goblinfish.metrics.trackers Package\n\n> Provides context-manager classes to name, track and report elapsed-time and other, user-defined metrics for top-level process entry-points (like AWS Lambda Function handlers, which is what it was originally conceived for) and sub-processes within them.\n\n## Quick Start\n\nInstall in your project:\n\n```shell\n# Install with pip\npip install goblinfish-metrics-trackers\n```\n\n```shell\n# Install with pipenv\npipenv install goblinfish-metrics-trackers\n```\n\nImport in your code:\n\n```python\nfrom goblinfish.metrics.trackers import ProcessTracker\n```\n\nCreate the timing-tracker instance:\n\n```python\ntracker = ProcessTracker()\n```\n\nDecorate your top-level/entry-point function:\n\n```python\n@tracker\ndef some_function():\n    ...\n```\n\nAdd any sub-process timers:\n\n```python\n@tracker\ndef some_function():\n    ...\n\n    with tracker.timer('some_process_name'):\n        # Do stuff here\n        ...\n```\n\nDecorate any child process functions with the instance's `.track` method:\n\n```python\n@tracker\ndef some_function():\n    ...\n\n    with tracker.timer('some_process_name'):\n        some_other_function()\n        # Do stuff here\n        ...\n\n@tracker.track\ndef some_other_function():\n    ...\n```\n\nSet any explicit metrics needed:\n\n```python\n@tracker\ndef some_function():\n    ...\n\n    with tracker.timer('some_process_name'):\n        try:\n            some_other_function()\n            # Do stuff here\n            ...\n        except Exception as error:\n            # Count of errors to be aggregated\n            tracker.set_metric('some_function_errors', 1)\n            # Name of error; simple string values are OK too!\n            tracker.set_metric(\n                'some_function_error_name', error.__class__.__name__\n            )\n            # Do stuff here\n            ...\n\n@tracker.track\ndef some_other_function():\n    ...\n```\n\nWhen this code is executed, after the context created by the `@tracker` decorator is complete, it will `print` something that looks like this:\n\n```json\n{\n    \"some_function\": 0.000,\n    \"some_other_function\": 0.000,\n    \"some_process_name\": 0.000\n}\n```\n\nMore detailed examples can be found in [the `examples` directory](https://bitbucket.org/stonefish-software-studio/goblinfish-metrics-trackers-package/src/main/examples/) in the repository.\n\n### A top-level `ProcessTracker` instance is *required*\n\nThis package was designed around the idea of there being a top-level entry-point function and zero-to-many child functions. Applying a `@tracker.track` decorator to a function that isn't called by the entry-point function decorated with `@tracker` will yield unexpected result, or no results at all.\n\n### Behavior in an `asyncio` context\n\nThis version will *work* with processes running under `asyncio`, for example:\n\n```python\nwith tracker.timer('some_async_process'):\n    async.run(some_function())\n```\n\n\u2026**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.\n\nA more useful approach, shown in the `li-article-async-example.py` module in [the `examples` directory](https://bitbucket.org/stonefish-software-studio/goblinfish-metrics-trackers-package/src/main/examples/) 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. Stripping that function in the example down to a bare minimum simulation, it would look like this:\n\n```python\nasync def get_person_data():\n    sleep_for = random.randrange(2_000, 3_000) / 1000\n    with tracker.timer('get_person_data'):\n        await asyncio.sleep(sleep_for)\n    return {'person_data': ('Professor Plum', dict())}\n```\n\n\u2026which will contribute to the logged/printed output in a more meaningful fashion:\n\n```json\n{\n    \"get_person_data\": 2215.262,\n    \"main\": 8465.233\n}\n```\n\n## Contribution guidelines\n\nAt this point, contributions are not accepted \u2014 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. That said, if you have an idea that you want to propose as an addition, a bug that you want to call out, etc., please feel free to contact the maintainer(s) (see below).\n\n## Who do I talk to?\n\nThe current maintainer(s) will always be listed in the `[maintainers]` section of [the `pyproject.toml` file](https://bitbucket.org/stonefish-software-studio/goblinfish-metrics-trackers-package/src/main/pyproject.toml) in the repository.\n\n## Future plans (To-Dos) and BYOLF\n\nWhile this package should work nicely for anything that can use a generic JSON log-message format, there are any number of products that are designed to read log-messages and ship them to some other service, usually with their own particular format requirements, in order to provide their own dashboards and alarms. If I have time in the future to start looking into those and writing [extras](https://stackoverflow.com/a/52475030) to accommodate, but I'm not confident that I'll have that time.\n\nIn the meantime, if there is a need for a specific log-message format, it's possible to BYOLF (**B**ring **Y**our **O**wn **L**og **F**ormat). Just write your own output function, and provide it as an argument to the `ProcessTracker` instance that is being created to track process items. What that would entail is:\n\n- Writing a function that accepts a single `str` parameter.\n- Deserializing that parameter from the JSON value that it will be passed.\n- Creating the custom log-message output using whatever data is relevant.\n- Writing that log-message in whatever manner is appropriate.\n\nA *very* bare-bones example:\n\n```python\ndef my_log_formatter(output: str) -> None:\n    ...  # Handle the \"output\" log-line here as needed.\n\ntracker = ProcessTracker(my_log_formatter)\n\n# ...\n```\n\nThough this package was designed to issue log-messages in a reasonably standard output (`print` or some [`logging` package](https://docs.python.org/3.11/library/logging.html) functionality), there's no *functional* reason that it couldn't, for example, write data straight to some database, call some third-party API, or whatever else.\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2025 Brian D. Allbee\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.\n        ",
    "summary": "Context-managed metrics tracking and output, including but not limited to process/subprocess latencies.",
    "version": "1.0.1",
    "project_urls": {
        "LinkedIn Article": "https://www.linkedin.com/pulse/logging-strategies-observability-troubleshooting-brian-allbee-8h4hc",
        "Repository": "https://bitbucket.org/stonefish-software-studio/goblinfish-metrics-trackers-package"
    },
    "split_keywords": [
        "aws",
        " cloudwatch",
        " log",
        " metrics",
        " latency",
        " process timing"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1cd0c3e612dacbe9a8002ca1d42fbab0a9a3cda8814558051739b8555e8e31bc",
                "md5": "7af11acaf58dc62c2c346d6f4183380a",
                "sha256": "50aa6b10d9a51cc35855633d4208ab5bfa410e12012f7b3724e6f83008bc9587"
            },
            "downloads": -1,
            "filename": "goblinfish_metrics_trackers-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7af11acaf58dc62c2c346d6f4183380a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 8371,
            "upload_time": "2025-02-03T22:19:52",
            "upload_time_iso_8601": "2025-02-03T22:19:52.985608Z",
            "url": "https://files.pythonhosted.org/packages/1c/d0/c3e612dacbe9a8002ca1d42fbab0a9a3cda8814558051739b8555e8e31bc/goblinfish_metrics_trackers-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4dad4b221e5da98a4fff1062b8e53555baa7122c82901f4ff02ccb32f380f980",
                "md5": "8ecb7156c98b6046bc0b171ef9020135",
                "sha256": "899e8a76877a8f2937c80bb01243afd47ee8c2479399c3fe704a7d4a5ce7d29b"
            },
            "downloads": -1,
            "filename": "goblinfish_metrics_trackers-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "8ecb7156c98b6046bc0b171ef9020135",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 10626,
            "upload_time": "2025-02-03T22:19:54",
            "upload_time_iso_8601": "2025-02-03T22:19:54.042075Z",
            "url": "https://files.pythonhosted.org/packages/4d/ad/4b221e5da98a4fff1062b8e53555baa7122c82901f4ff02ccb32f380f980/goblinfish_metrics_trackers-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-03 22:19:54",
    "github": false,
    "gitlab": false,
    "bitbucket": true,
    "codeberg": false,
    "bitbucket_user": "stonefish-software-studio",
    "bitbucket_project": "goblinfish-metrics-trackers-package",
    "lcname": "goblinfish-metrics-trackers"
}
        
Elapsed time: 0.61090s