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.

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() 

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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 2Python 3

File details

Details for the file cloudwatch-metrics-buffer-0.1.0.tar.gz.

File metadata

  • Download URL: cloudwatch-metrics-buffer-0.1.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for cloudwatch-metrics-buffer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a4d0daa7bc33a48554408a4cf4fb45c331e63b6d7481d6a9ef5dd39a903eb75d
MD5 15ebfdccc83a87121f305b5b6b3ca9d5
BLAKE2b-256 84ac0a53a29d368180bd03a57c873805b58f1e534a0b1b1c695063cf5298a470

See more details on using hashes here.

File details

Details for the file cloudwatch_metrics_buffer-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: cloudwatch_metrics_buffer-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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

File hashes

Hashes for cloudwatch_metrics_buffer-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 df7fda3bca57d27a7c034034c57ccdd3d567a041df6c9e00044eb78cb599f6da
MD5 dbafa3db6ecac6ebb9ef2fa885643431
BLAKE2b-256 194d8375e33066621bb8493d8222c695951ead63739afd030ba44c72156b0afe

See more details on using hashes here.

Supported by

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