def test_collect_metric_using_service_method():
    mocks = create_session_mocks_using_service_method("describe_instances", [
        {
            "Reservations": [{"Instances": [{"InstanceId": "instance_id_1"}, {"InstanceId": "instance_id_2"}, ]}],
            "NextToken": "123abc"
        },
        {
            "Reservations": [{"Instances": [{"InstanceId": "instance_id_3"}]}],
            "NextToken": None
        },
    ])
    metrics = parse_aws_metrics(SINGLE_METRIC_YAML_WITH_PAGINATOR_WITH_SERVICE_METHOD)
    collector = AwsMetricsCollector(metrics, mocks.session)
    collector.update()
    gauge_family = list(collector.collect())[0]
    mocks.session.client.assert_called_once_with("ec2")
    mocks.service.describe_instances.assert_has_calls([
        call(Filters=[{
            "Name": "instance-state-name",
            "Values": ["Running"],
        }]),
        call(Filters=[{
            "Name": "instance-state-name",
            "Values": ["Running"],
        }], NextToken="123abc")
    ])
    assert gauge_family.samples == [
        Sample("ec2_instance_ids", {"id": "instance_id_1"}, 1),
        Sample("ec2_instance_ids", {"id": "instance_id_2"}, 1),
        Sample("ec2_instance_ids", {"id": "instance_id_3"}, 1)
    ]
def main(args):
    port = int(args.port)
    with open(args.metrics_file_path) as metrics_file:
        metrics_yaml = metrics_file.read()
    metrics = parse_aws_metrics(metrics_yaml)
    collector = AwsMetricsCollector(metrics, boto3.Session())
    REGISTRY.register(collector)
    start_http_server(port)
    print("Serving at port: %s" % port)
    while True:
        try:
            collector.update()
            time.sleep(args.period_seconds)
        except KeyboardInterrupt:
            print("Caught SIGTERM - stopping...")
            break
    print("Done.")
Beispiel #3
0
def main(args):
    port = int(args.port)
    with open(args.metrics_file_path) as metrics_file:
        metrics_yaml = metrics_file.read()
    metrics = parse_aws_metrics(metrics_yaml)
    collector = AwsMetricsCollector(metrics, boto3.client("sts"), args.assume_role_arn, "prometheusAssumeRole", args.duration_seconds)
    REGISTRY.register(collector)
    start_http_server(port)
    print("Serving at port: %s" % port)
    while True:
        try:
            print("Starting the collection again : ", datetime.datetime.now())
            collector.update()
            time.sleep(args.period_seconds)
        except KeyboardInterrupt:
            print("Caught SIGTERM - stopping...")
            break
    print("Done.")
def test_collect_metric_with_extra_labels():
    mocks = create_session_mocks_using_paginator([
        {"id": "instance_id_1", "value": 1},
        {"id": "instance_id_2", "value": 1},
        {"id": "instance_id_3", "value": 1}
    ])
    metrics = parse_aws_metrics(SINGLE_METRIC_YAML_WITH_PAGINATOR)
    collector = AwsMetricsCollector(metrics, mocks.session, ["region_name", "env"], ["us-east-1", "dev"])
    collector.update()
    gauge_family = list(collector.collect())[0]
    mocks.session.client.assert_called_once_with("ec2")
    mocks.service.get_paginator.assert_called_once_with("describe_instances")
    mocks.paginator.paginate.assert_called_once_with(Filters=[{
        "Name": "instance-state-name",
        "Values": ["Running"]
    }])
    mocks.paginate_response_iterator.search.assert_called_once_with(metrics[0].search)
    assert gauge_family.samples == [
        Sample("ec2_instance_ids", {"region_name": "us-east-1", "env": "dev", "id": "instance_id_1"}, 1),
        Sample("ec2_instance_ids", {"region_name": "us-east-1", "env": "dev", "id": "instance_id_2"}, 1),
        Sample("ec2_instance_ids", {"region_name": "us-east-1", "env": "dev", "id": "instance_id_3"}, 1)
    ]