Skip to main content

Asynchronous (and synchronous) Python client for AWS CloudWatch metrics

Project description

cloudwatch-metrics-client

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.

https://github.com/awslabs/cloudwatch-fluent-metrics https://github.com/peterdemin/awsme

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

Usage:

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)
  await reporter.run()


@CloudWatchAsyncMetrics.monitored_task
async def process_request(request):

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

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

  # Put particular metric
  await CloudWatchAsyncMetrics.put_metric(
        MetricName='particular-metric-name',
        Unit='Count',
        Dimensions={'Kind': 'metric-kind', 'Sort': 'metric-sort'},
        Value=value
  )

  # Record statistical value
  await CloudWatchAsyncMetrics.put_statistic(
        MetricName='particular-metric-name',
        Unit='Count',
        Dimensions={'Kind': 'metric-kind', 'Sort': 'metric-sort'},
        Value=value

  # 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 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.put_metric_data 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)
  reporter.run()

@CloudWatchSyncMetrics.monitored_task  
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.

Source Distribution

cloudwatch-metrics-client-0.0.1.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudwatch_metrics_client-0.0.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file cloudwatch-metrics-client-0.0.1.tar.gz.

File metadata

  • Download URL: cloudwatch-metrics-client-0.0.1.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for cloudwatch-metrics-client-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9530e0bae41583fbe4f0f3d8314422dc8e1e684736932e84c3667f4e5f6ccfd1
MD5 625b1cb1e352df0f0f4062484ecc7fc6
BLAKE2b-256 8f5fc6e61283f5ed2efacf9231e784a97b54be50a7359e6ed1b76cf250100b73

See more details on using hashes here.

File details

Details for the file cloudwatch_metrics_client-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: cloudwatch_metrics_client-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for cloudwatch_metrics_client-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 472a783937f7c5edc80bcf208686bd5ccc3152a3b85f12b475a25a738855890a
MD5 b0a53740a8bf8862dbda6b925a3c2fc1
BLAKE2b-256 96dd1c127e7ab55242499ef65fe9504c790ef71c4531be12f0845f2986c5add5

See more details on using hashes here.

Supported by

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