Skip to content

Instantly share code, notes, and snippets.

@lmammino
Last active June 18, 2021 08:24
Show Gist options
  • Save lmammino/25917a432462b4e728d22880a2c7eabc to your computer and use it in GitHub Desktop.
Save lmammino/25917a432462b4e728d22880a2c7eabc to your computer and use it in GitHub Desktop.
Gzipping payload for custom metrics to cloudwatch with boto3
import boto3
import gzip
cw_client = boto3.client('cloudwatch')
event_system = cw_client.meta.events
def gzip_request_body(request, **kwargs):
gzipped_body = gzip.compress(request.body)
request.headers['Content-Encoding'] = 'gzip'
request.data = gzipped_body
return request
event_system.register('before-sign.cloudwatch.PutMetricData', gzip_request_body)

In order to make sure the requests wasn't getting gzipped by default and, later on, that the custom event lister did actually gzip the request payload, i created a simple debug Node.js web server:

const { createServer } = require('http');

createServer((req, res) => {
  console.log('--------');
  console.log(JSON.stringify(req.headers, null, 2));
  req.pipe(process.stdout);
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.end(JSON.stringify({ok:true}));
})
.listen(8000);

Then in python:

import boto3
import gzip

endpoint = "http://localhost:8000/"

cw_client = boto3.client('cloudwatch', endpoint_url=endpoint, use_ssl=False)
event_system = cw_client.meta.events

def gzip_request_body(request, **kwargs):
    gzipped_body = gzip.compress(request.body)
    request.headers['Content-Encoding'] = 'gzip'
    request.headers['X-Test'] = 'FOOOBAAAAR' # to make sure also headers are modified
    request.data = gzipped_body
    return request

event_system.register('before-sign.cloudwatch.PutMetricData', gzip_request_body)


import random

cw_client.put_metric_data(
    MetricData = [
        {
            'MetricName': 'KPIs',
            'Dimensions': [
                {
                    'Name': 'PURCHASES_SERVICE',
                    'Value': 'CoolService'
                },
                {
                    'Name': 'APP_VERSION',
                    'Value': '1.0'
                },
            ],
            'Unit': 'None',
            'Value': random.randint(1, 500)
        },
    ],
    Namespace='CoolApp'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment