Skip to main content

Easily monitor CPU and memory usage in AWS Batch jobs and report them to StatsD

Project description

AWS Resource Monitor

Small Python 3.5+ convenience library to easily monitor CPU and memory usage in AWS Batch jobs and report them to StatsD. Since it uses ECS Task Metadata V3 endpoints it is perfectly suitable to do so for other type of ECS tasks as well.

Setup

You can install this package by using pip:

pip install aws-resource-monitor

If you fancy pipenv use:

pipenv install aws-resource-monitor

To install from source, run:

python setup.py install

To install via requirements file, add the following:

-e git+https://github.com/luigisbox/aws-resource-monitor.git#egg=aws-resource-monitor

Usage

First off, you need to require the library:

from aws_resource_monitor import resource_monitoring

The resource_monitoring is a context manager accepting two positional arguments:

  • statsd_client is your own instance of StatsD client (see PythonStatsD library which will be used to report metrics.
  • metrics_prefix is a string which will prefix individual metrics.

Optionally, you can pass in keyword argument interval with timedelta object customizing polling period for new metrics (default is every five seconds).

with resource_monitoring(statsd_client, metrics_prefix):
	fn()

Behind the scenes, resource_monitoring context manager first checks for existence of ECS_CONTAINER_METADATA_URI environment variable. If it is not found, the monitor does nothing thus working well in development.

In production or when the variable exists, it creates a separate thread from your main application that reads task UUID and polls the "stats" endpoint in the metadata URI to complete and send as gauge the following metrics:

  • cpu.system
  • cpu.user
  • cpu.percent
  • memory.limit
  • memory.usage
  • memory.max_usage

A more complete example would be:

import statsd
from aws_resource_monitor import resource_monitoring

statsd = statsd.StatsClient('localhost', 8125)

with resource_monitoring(statsd, "example.metrics.", interval=timedelta(seconds=10)):
	fn()

For example, for the task UUID assigned by AWS "afc92259-3a0c-4e14-8e23-a49cc41e7947" this will report gauge "example.metrics.afc92259-3a0c-4e14-8e23-a49cc41e7947.memory.usage" and in ten seconds check and report again.

Contributing

  1. Check for open issues or open a new issue for a feature request or a bug.
  2. Fork the repository and make your changes to the master branch (or branch off of it).
  3. Send a pull request.

Development

Run the linter with:

make lint

The client library uses Black for code formatting. Code must be formatted with Black before PRs are submitted. Run the formatter with:

make fmt

Changelog

v1.0.0: 08/06/2020

Initial version.

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

aws-resource-monitor-1.0.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

aws_resource_monitor-1.0.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file aws-resource-monitor-1.0.0.tar.gz.

File metadata

  • Download URL: aws-resource-monitor-1.0.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.16

File hashes

Hashes for aws-resource-monitor-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d9a3a09dc15f4ed74a2cc48bb74f1a545e0a8416d3b77855550f93ba800088cf
MD5 824758767fdb247857563f1f7809ecb2
BLAKE2b-256 ce45273bd46289851b74220f32f2fd8256702e382ed2552942514788ca6817a6

See more details on using hashes here.

File details

Details for the file aws_resource_monitor-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: aws_resource_monitor-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.16

File hashes

Hashes for aws_resource_monitor-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da22b4d90d5216d990e14ebb021a7891df2f161fef6f220ade5ebbddf6c53f1d
MD5 19fbc1dcf20182d545d20581f88380c4
BLAKE2b-256 1c30d45717c9ac831c79a0231e4cf02d09a4c6953cbd377a4535f22d8e53321a

See more details on using hashes here.

Supported by

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