Skip to content

Commit

Permalink
Merge pull request #15 from oliversalzburg/feat/cluster-selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Limess committed May 17, 2021
2 parents a9a9ba5 + 232ba68 commit 9721450
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ If `--tags-to-labels` is set, the given tags will be added to the service discov

If `--tags-to-labels "*"` is provided then _all_ non aws prefixed (`AWS:` or `aws:`) tags will be added.

### Selecting specific cluster ARNs

If you only want to monitor a specific subset of clusters in your ECS account, you can declare them, using the `--cluster-arns` argument. For example:

```
python discoverecs.py --directory /opt/prometheus-ecs --cluster-arns "arn:aws:ecs:eu-west-1:123456:cluster/staging" "arn:aws:ecs:eu-west-1:123456:cluster/production",
```

### Configuration yaml

The following Prometheus configuration should be used to support all available intervals:
Expand Down
36 changes: 27 additions & 9 deletions discoverecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ def valid(self):


class TaskInfoDiscoverer:
def __init__(self, fetch_tags=True):
def __init__(self, fetch_tags=True, cluster_arns=[]):
self.ec2_client = boto3.client("ec2")
self.ecs_client = boto3.client("ecs")
self.task_cache = FlipCache()
self.task_definition_cache = FlipCache()
self.container_instance_cache = FlipCache()
self.ec2_instance_cache = FlipCache()
self.fetch_tags = fetch_tags
self.cluster_arns = cluster_arns

def flip_caches(self):
self.task_cache.flip()
Expand Down Expand Up @@ -290,15 +291,25 @@ def print_cache_stats(self):
)
)

def list_clusters(self):
if len(self.cluster_arns) > 0:
return self.cluster_arns
cluster_arns = []
clusters_pages = self.ecs_client.get_paginator("list_clusters").paginate()
for clusters in clusters_pages:
for cluster_arn in clusters["clusterArns"]:
cluster_arns += [cluster_arn]

return cluster_arns

def get_infos(self):
self.flip_caches()
task_infos = []
fargate_task_infos = []
clusters_pages = self.ecs_client.get_paginator("list_clusters").paginate()
for clusters in clusters_pages:
for cluster_arn in clusters["clusterArns"]:
task_infos += self.get_infos_for_cluster(cluster_arn, "EC2")
fargate_task_infos += self.get_infos_for_cluster(cluster_arn, "FARGATE")
cluster_arns = self.list_clusters()
for cluster_arn in cluster_arns:
task_infos += self.get_infos_for_cluster(cluster_arn, "EC2")
fargate_task_infos += self.get_infos_for_cluster(cluster_arn, "FARGATE")
self.add_ec2_instances(task_infos)
task_infos += fargate_task_infos
self.print_cache_stats()
Expand Down Expand Up @@ -482,12 +493,12 @@ def task_info_to_targets(task_info):

class Main:
def __init__(
self, directory, interval, default_scrape_interval_prefix, tags_to_labels
self, directory, interval, default_scrape_interval_prefix, tags_to_labels, cluster_arns
):
self.directory = directory
self.interval = interval
self.default_scrape_interval_prefix = default_scrape_interval_prefix
self.discoverer = TaskInfoDiscoverer(fetch_tags=len(tags_to_labels) > 0)
self.discoverer = TaskInfoDiscoverer(fetch_tags=len(tags_to_labels) > 0, cluster_arns=cluster_arns)
self.tags_to_labels = tags_to_labels

def write_jobs(self, jobs):
Expand Down Expand Up @@ -557,7 +568,6 @@ def loop(self):
self.discover_tasks()
time.sleep(self.interval)


def main():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
Expand All @@ -582,6 +592,12 @@ def main():
default=[],
help="Task definition tags to convert to labels. Case sensitive.",
)
arg_parser.add_argument(
"--cluster-arns",
nargs="*",
default=[],
help="The ARNs of the ECS clusters that should be monitored."
)
args = arg_parser.parse_args()
log(
f"""
Expand All @@ -590,13 +606,15 @@ def main():
Refresh interval: "{str(args.interval)}s"
Default scrape interval prefix: "{args.default_scrape_interval_prefix}"
Tags to convert to labels: {args.tags_to_labels}
Clusters to query: {args.cluster_arns}
"""
)
Main(
directory=args.directory,
interval=args.interval,
default_scrape_interval_prefix=args.default_scrape_interval_prefix,
tags_to_labels=args.tags_to_labels,
cluster_arns=args.cluster_arns
).loop()


Expand Down

0 comments on commit 9721450

Please sign in to comment.