Skip to content

Commit

Permalink
Merge pull request #21 from ClarkSource/CLOUD-5317
Browse files Browse the repository at this point in the history
CLOUD-5317 Add SES metrics
  • Loading branch information
an2nb2 authored Feb 22, 2023
2 parents f9e4b68 + c4925df commit ead92b1
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ repos:
hooks:
- id: check-hooks-apply
- id: check-useless-excludes
- repo: git://github.com/pre-commit/pre-commit-hooks
- repo: git@github.com:pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
- id: check-merge-conflict
- repo: git://github.com/Lucas-C/pre-commit-hooks.git
- repo: git@github.com:Lucas-C/pre-commit-hooks
rev: v1.1.7
hooks:
- id: insert-license
Expand Down
67 changes: 67 additions & 0 deletions aws_exporter/aws/ses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
# ISC License
#
# Copyright 2019 FL Fintech E GmbH
#
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import boto3
import logging

from prometheus_client.core import GaugeMetricFamily

from aws_exporter.aws import COMMON_LABELS
from aws_exporter.aws.sts import get_account_id
from aws_exporter.util import strget


SES = boto3.client("ses")
LOGGER = logging.getLogger(__name__)
SES_LABELS = COMMON_LABELS


class AWSSESMetricsCollector:
def _metrics_containers(self):
return {
'aws_ses_send_quota_max': GaugeMetricFamily(
'aws_ses_send_quota_max',
'The maximum number of emails the user is allowed to send in a 24-hour interval.',
labels=SES_LABELS
),
'aws_ses_send_quota_rate_max': GaugeMetricFamily(
'aws_ses_send_quota_rate_max',
'The maximum number of emails that Amazon SES can accept from the user\'s account per second.',
labels=SES_LABELS
),
'aws_ses_send_quota_used': GaugeMetricFamily(
'aws_ses_send_quota_used',
'The number of emails sent during the previous 24 hours.',
labels=SES_LABELS
),
}

def describe(self):
return self._metrics_containers().values()

def collect(self):
metrics = self._metrics_containers()
labels = [get_account_id()]
mapping = {
'aws_ses_send_quota_max': 'Max24HourSend',
'aws_ses_send_quota_rate_max': 'MaxSendRate',
'aws_ses_send_quota_used': 'SentLast24Hours',
}

LOGGER.debug('querying sending quotas')

response = SES.get_send_quota()

for metric_key, response_key in mapping.items():
metrics[metric_key].add_metric(labels, strget(response, response_key))
yield metrics[metric_key]

LOGGER.debug('finished querying sending quotas')

# -*- coding: utf-8 -*-
17 changes: 12 additions & 5 deletions aws_exporter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging

from prometheus_client import start_http_server

from aws_exporter.util import strtobool
from aws_exporter.metrics import MetricsCollector

LOGGER = logging.getLogger(__name__)
Expand All @@ -22,25 +22,32 @@ def main():
delay = int(os.environ.get('AWS_EXPORTER_POLL_DELAY', 30))
port = int(os.environ.get('AWS_EXPORTER_PORT', 8000))
log_level = getattr(logging, os.environ.get('AWS_EXPORTER_LOG_LEVEL', 'info').upper())
metrics_config = {
'enable_ec2': strtobool(os.environ.get('ENABLE_EC2_METRICS', 'True')),
'enable_sns': strtobool(os.environ.get('ENABLE_SNS_METRICS', 'True')),
'enable_ses': strtobool(os.environ.get('ENABLE_SES_METRICS', 'True')),
'enable_backup': strtobool(os.environ.get('ENABLE_BACKUP_METRICS','True')),
'ec2': dict(ami_owners=['self']),
}


logging.basicConfig(level=log_level)
logging.getLogger('botocore').setLevel(logging.WARN)
logging.getLogger('urllib3').setLevel(logging.WARN)

ami_owners = ['self']
additional_ami_owners = os.environ.get('AWS_EXPORTER_EC2_AMI_OWNERS')

if additional_ami_owners is not None:
ami_owners.extend(additional_ami_owners.split(','))
metrics_config['ec2']['ami_owners'].extend(additional_ami_owners.split(','))

LOGGER.debug('getting amis from owners: %s', ami_owners)
LOGGER.debug('getting amis from owners: %s', metrics_config['ec2']['ami_owners'])

try:
LOGGER.info('listening on port %d', port)

start_http_server(port)

metrics = MetricsCollector(ec2_config=dict(ami_owners=ami_owners))
metrics = MetricsCollector(metrics_config=metrics_config)
metrics.run_loop(delay)
except KeyboardInterrupt:
exit(137)
Expand Down
17 changes: 13 additions & 4 deletions aws_exporter/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@
from aws_exporter.aws.ec2 import AWSEC2MetricsCollector
from aws_exporter.aws.backup import AWSBackupMetricsCollector
from aws_exporter.aws.sns import AWSSNSMetricsCollector
from aws_exporter.aws.ses import AWSSESMetricsCollector

LOGGER = logging.getLogger(__name__)


class MetricsCollector:
def __init__(self, ec2_config = None):
REGISTRY.register(AWSEC2MetricsCollector(config = ec2_config))
REGISTRY.register(AWSSNSMetricsCollector())
REGISTRY.register(AWSBackupMetricsCollector())
def __init__(self, metrics_config):
if metrics_config['enable_ec2']:
REGISTRY.register(AWSEC2MetricsCollector(config = metrics_config['ec2']))

if metrics_config['enable_sns']:
REGISTRY.register(AWSSNSMetricsCollector())

if metrics_config['enable_backup']:
REGISTRY.register(AWSBackupMetricsCollector())

if metrics_config['enable_ses']:
REGISTRY.register(AWSSESMetricsCollector())

def run_loop(self, polling_interval_seconds = 30):
while True:
Expand Down
3 changes: 3 additions & 0 deletions aws_exporter/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ def function_wrapper(*args, **kwargs):

def strget(dict_obj, value, default=None):
return str(dict_obj.get(value, default))

def strtobool(string):
return string.lower() in ('true', '1', 't')

0 comments on commit ead92b1

Please sign in to comment.