def publish_cloudwatch_metric()

in services/jenkins-run-statistics/aws_utils.py [0:0]


def publish_cloudwatch_metric(cloudwatch, metric_namespace, metric_name, value, unix_timestamp, dimensions, unit='Milliseconds'):
    # CloudWatch does not allow submission older than 2 weeks.
    if time.time() - unix_timestamp >= CLOUDWATCH_MAXIMUM_LOOKBACK_TIMEFRAME_SECONDS:
        logging.info('Skipping submission of CloudWatch metric that was older than 2 weeks.')
        return

    try:
        cloudwatch.put_metric_data(
            MetricData=[
                {
                    'MetricName': metric_name,
                    'Dimensions': [{'Name': name, 'Value': value} for name, value in dimensions.items()],
                    'Unit': unit,
                    'Value': value,
                    'Timestamp': datetime.utcfromtimestamp(unix_timestamp)
                }
            ],
            Namespace=metric_namespace
        )
    except ClientError as e:
        if e.response['Error']['Code'] == 'InvalidParameterValue':
            logging.info('Skipping submission of CloudWatch metric that was older than 2 weeks.')
            logging.exception('Exception:')
        else:
            raise