Coder Social home page Coder Social logo

aws-prometheus-exporter's Introduction

AWS Prometheus Exporter

This Python module allows you to run AWS API calls through Boto3, and expose the results of those calls as Prometheus metrics. Metrics must be described in YAML. For example:

ec2_instance_ids:
  description: EC2 instance ids
  service: ec2
  paginator: describe_instances
  paginator_args:
    Filters:
      - Name: instance-state-name
        Values: [ "running" ]
  label_names:
    - instance_id
  search: |
    Reservations[].Instances[].{instance_id: InstanceId, value: `1`}

The above describes a Prometheus metric derived from calling the following in Boto3:

result = boto3.client("ec2") \
            .get_paginator("describe_instances") \
            .paginate({"Filters": [{"Name": "instance-state-name", "Values": ["running"]}]}) \
            .search("Reservations[].Instances[].{instance_id: InstanceId, value: `1`}")

You aren't restricted to calling paginators. By specifying method and method_args in place of paginator and paginator_args, you can call any service method. Pagination will be handled for you in the following fashion:

  service = boto3.client("ec2")
  next_token = ''
  result = []
  kwargs = {"Filters": [{"Name": "instance-state-name", "Values": ["running"]}]}
  while next_token is not None:
      response = boto3.client("ec2").describe_instances(**kwargs)
      next_token = response.get('NextToken', None)
      result += jmespath.search("Reservations[].Instances[].{instance_id: InstanceId, value: `1`}", response)
      kwargs["NextToken"] = next_token  

The dict values returned by the paginator or service method are then converted by this module into GaugeMetricFamily samples. Each dict must have the same keys as label_names, plus an additional value key; the corresponding values correspond to the labels and value of the created Gauge, respectively.

Note that pagninator_args may be a string. In that case, it will be eval-ed with access to datetime.datetime and datetime.timedelta. For example:

recent_emr_cluster_ids:
  description: Recent EMR cluster ids
  service: emr
  paginator: list_clusters
  paginator_args: |
    {
        "CreatedAfter": datetime.now() - timedelta(weeks=4)
    }
  label_names:
    - id
  search: |
    Clusters[].{id: Id, value: `1`}

Example Usage

The module can be run directly, as follows:

python -m aws_prometheus_exporter --metrics-file ./metrics.yaml --port 9000 --period-seconds 300

Running using Docker:

docker build -t aws_prometheus_exporter:latest  .
docker run -p9000:9000 -v $(pwd)/example.yaml:/mnt/metrics.yaml aws_prometheus_exporter

Alternatively, you can import the module into an existing application. See __main__.py for an example.

Links

aws-prometheus-exporter's People

Contributors

nmaquet avatar utdemir avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.