Skip to main content

cloudwatch-metrics-buffer Python 3

This is a wrapper library for publishing metrics to AWS CloudWatch.

While playing around with lambdas and serveless frameworks, I needed a library to help me with publishing metrics to CloudWatch in easy and elegant way. I wanted to avoid boilerplate code that polutes all my methods. Ideally, I would decorate my function to measure execution time or count. After unsuccessful search, I implemented my own.

This wrapper will buffer metrics first, then send them in batches. It supports timeit and count decorators for metricating functions in elegant way. Though publishing metrics is batched, there is no guaranty ClouWatch will swallow everything. If you send > 150 TPS (can happen easily if you scale out with Lambdas), you might get throttled. Read CloudWatch documentation for limits and pricing consideration.

Installation

pip install cloudwatch-metrics-buffer

Usage

Post metrics explicitly

from cwmetrics import CloudWatchMetricsBuffer

cw = CloudWatchMetricsBuffer('Some Namespace')

# buffer single metrics value to buffer
cw.put_value('total_calls', 5) # metric without units of value 5
cw.put_value('latency', 11.25, unit='Milliseconds') # metric with unit specified
cw.put_value('latency', 11.25, dimensions={'HTTP Method': 'GET'}, unit='Milliseconds') # same latency with specified dimension 
cw.put_value('home-page', 1, timestamp=datetime.datetime(2019, 10, 10, 14, 0, 0)) # metric on exact time

# buffer statistic value; use this if you are gathering your statistics along the way in your app
cw.put_statistic('metric', sample_count=50, sum=10000, minimum=0, maximum=500)

# send all to Cloudwatch
cw.send()

Post metrics using decorators

from cwmetrics import CloudWatchMetricsBuffer

cw = CloudWatchMetricsBuffer('Some Namespace')

# send value of for metric for each execution
@cw.count('count_metric1')
def func():
   ...

func()

You can also decorate function multiple times. Publishing to CloudWatch is executed after outer decorator finishes:

# measure execution time in milliseconds and count request
@cw.timeit('api')
@cw.timeit('api', dimensions={'HTTP Method': 'GET'})
@cw.count('requests', dimensions={'HTTP Method': 'GET'})
def process_api_request():
   ...

# CW will receive 3 metric values
process_api_request() 

This will also work:

@cw.timeit('api')
@cw.timeit('api', dimensions={'HTTP Method': 'GET'})
@cw.count('requests', dimensions={'HTTP Method': 'GET'})
@cw.count('requests')
def process_api_request():
    ...
    authenticate()
    ...

@cw.timeit('auth')
@cw.count('auth requests')
def authenticate():
    ...

# all metrics (total of 6) are buffered and sent after method was executed
process_api_request() 

Metadata

Release files for cloudwatch-metrics-buffer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cloudwatch-metrics-buffer 0.1.0
File Size Uploaded
cloudwatch-metrics-buffer-0.1.0.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cloudwatch-metrics-buffer 0.1.0
File Interpreter ABI Platform
cloudwatch_metrics_buffer-0.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 10.5 kB

Release files / cloudwatch-metrics-buffer-0.1.0.tar.gz

Download URL cloudwatch-metrics-buffer-0.1.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a4d0daa7bc33a48554408a4cf4fb45c331e63b6d7481d6a9ef5dd39a903eb75d
BLAKE2b-256 checksum
How to use checksums
84ac0a53a29d368180bd03a57c873805b58f1e534a0b1b1c695063cf5298a470
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.2

Release files / cloudwatch_metrics_buffer-0.1.0-py2.py3-none-any.whl

Download URL cloudwatch_metrics_buffer-0.1.0-py2.py3-none-any.whl
Size 5.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
df7fda3bca57d27a7c034034c57ccdd3d567a041df6c9e00044eb78cb599f6da
BLAKE2b-256 checksum
How to use checksums
194d8375e33066621bb8493d8222c695951ead63739afd030ba44c72156b0afe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page