Skip to content

Commit

Permalink
Adds support for awsvpc
Browse files Browse the repository at this point in the history
  • Loading branch information
fredsig committed Jul 25, 2019
1 parent 2c9a587 commit 04a1e3d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ ECS Service Discovery for Prometheus
## Info
This tool provides Prometheus service discovery for Docker containers running on AWS ECS. You can easily instrument your app using a Prometheus
client and enable discovery adding an ENV variable at the Service Task Definition. Your container will then be added
to the list of Prometheus targets to be scraped. Requires python2 and boto3. Works with Prometheus 2.x.
to the list of Prometheus targets to be scraped. Requires python2 and boto3. Works with Prometheus 2.x. It supports both bridge and awsvpc
network mode.

## Setup
``discoverecs.py`` should run alongside the Prometheus server. It generates targets using JSON file service discovery. It can
Expand Down Expand Up @@ -133,3 +134,5 @@ will be exposed and the instance label will always point to the job name.

PROMETHEUS_PORT can be used for tasks using classic ELB setup with multiple
port mappings.

If you are using awsvpc, you must set PROMETHEUS_PORT.
14 changes: 9 additions & 5 deletions discoverecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
and metrics are exposed at a service level. This way, no ec2/ecs labels
will be exposed and the instance label will always point to the job name.
PROMETHEUS_PORT can be used for tasks using classic ELB setup with multiple
port mappings.
PROMETHEUS_PORT must be set when using awsvpc network mode. It can also be used
for tasks using a classic ELB setup with multiple port mappings.
"""

def log(message):
Expand Down Expand Up @@ -133,7 +133,7 @@ def fetcher(fetch_task_arns):
for task in result['tasks']:
no_network_binding = []
for container in task['containers']:
if 'networkBindings' not in container or len(container['networkBindings']) == 0:
if ('networkBindings' not in container or len(container['networkBindings']) == 0) and len(container['networkInterfaces']) == 0:
no_network_binding.append(container['name'])
if no_network_binding:
arn = task['taskDefinitionArn']
Expand Down Expand Up @@ -284,19 +284,23 @@ def task_info_to_targets(task_info):
first_port = prom_port
else:
first_port = str(container['networkBindings'][0]['hostPort'])
if task_info.task_definition.get('networkMode') == 'awsvpc':
interface_ip = container['networkInterfaces'][0]['privateIpv4Address']
else:
interface_ip = task_info.ec2_instance['PrivateIpAddress']
if nolabels:
p_instance = ecs_task_name
ecs_task_id = ecs_task_version = ecs_container_id = ecs_cluster_name = ec2_instance_id = None
else:
p_instance = task_info.ec2_instance['PrivateIpAddress'] + ':' + first_port
p_instance = interface_ip + ':' + first_port
ecs_task_id=extract_name(task_info.task['taskArn'])
ecs_task_version=extract_task_version(task_info.task['taskDefinitionArn'])
ecs_container_id=extract_name(container['containerArn'])
ecs_cluster_name=extract_name(task_info.task['clusterArn'])
ec2_instance_id=task_info.container_instance['ec2InstanceId']

return [Target(
ip=task_info.ec2_instance['PrivateIpAddress'],
ip=interface_ip,
port=first_port,
metrics_path=metrics_path,
p_instance=p_instance,
Expand Down

0 comments on commit 04a1e3d

Please sign in to comment.