Skip to main content

Library for posting metrics to AWS CloudWatch

Project description

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.


pip install cloudwatch-metrics-buffer


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

Post metrics using decorators

from cwmetrics import CloudWatchMetricsBuffer

cw = CloudWatchMetricsBuffer('Some Namespace')

# send value of for metric for each execution
def 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', dimensions={'HTTP Method': 'GET'})
@cw.count('requests', dimensions={'HTTP Method': 'GET'})
def process_api_request():

# CW will receive 3 metric values

This will also work:

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

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

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

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

cloudwatch-metrics-buffer-0.1.0.tar.gz (4.7 kB view hashes)

Uploaded Source

Built Distribution

cloudwatch_metrics_buffer-0.1.0-py2.py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 2 Python 3

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