Skip to main content

Asynchronous (and synchronous) Python client for AWS CloudWatch metrics

Project description


Python CloudWatch metrics Client library utilising asyncio (along with sync variant)

This is yet another Python CloudWatch client. While there are alternatives, they are either not supporting asyncio or not actively maintained.

The client collects whatever metrics you feed it with and reports to CloudWatch regularly, though you can use it for ad-hoc reporting as well

NOTE: aioboto3 is not in the requirements, in case you only need sync version. If running under Python 3.6, you'll need async_generator>=1.10 as well


import asyncio
from cloudwatch_metrics_client.aiocloudwatch import CloudWatchAsyncMetrics, CloudWatchAsyncMetricReporter

async def setup():

  reporter = CloudWatchAsyncMetricReporter(report_interval=REPORT INTERVAL)        # seconds
  CloudWatchAsyncMetrics.with_namespace('<YOUR NAMESPACE').with_reporter(reporter)
  # .with_client(aioboto3_cloudwatch_client) is you're not happy with default client

async def process_request(request):

  # processing logic...
  # request_type = ... 
  # request_specific_data = 

  # Set details on monitored call
    .with_monitored_dimension('TypeOfRequest', request_type)\
    .with_monitored_dimension('SpecificAttribute', request_specific_data)

  # Put particular metric
  await CloudWatchAsyncMetrics.put_metric(
        Dimensions={'Kind': 'metric-kind', 'Sort': 'metric-sort'},

  # Record statistical value
  await CloudWatchAsyncMetrics.put_statistic(
        Dimensions={'Kind': 'metric-kind', 'Sort': 'metric-sort'},

  # Elapsed time will be recorded  

Elapsed time of each call of process_request coroutine will be recorded in internal data structure and aggregated statistics will be regularly sent to CloudWatch. Individual metrics could be collected with put_metric call - they too would be stored for a while and then sent to CloudWatch. put_metric keeps each unique value and sends list of them along with number of occurrencies of each value, put_statistics only sends aggregated data over a bunch of metrics - Sum, SampleCount, Min and Max (See for details)

Reporter has async flush method for flushing metric data still not sent to CloudWatch. If regular reporting is not needed, just don't call run coro and instead call flush when it is time to report metrics.

Sync version works in the very same way, it utilises separate thread for reporting.

from cloudwatch_metrics_client.cloudwatch import CloudWatchSyncMetrics, CloudWatchSyncMetricReporter

def setup():

  reporter = CloudWatchSyncMetricReporter(report_interval=REPORT INTERVAL)        # seconds
  CloudWatchSyncMetrics.with_namespace('<YOUR NAMESPACE').with_reporter(reporter)

def process_request(request):

# Same here, but with CloudWatchSync*

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for cloudwatch-metrics-client, version 0.0.6
Filename, size File type Python version Upload date Hashes
Filename, size cloudwatch_metrics_client-0.0.6-py3-none-any.whl (8.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size cloudwatch-metrics-client-0.0.6.tar.gz (6.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page