Skip to content

Commit

Permalink
Merge pull request #4 from mintel/net-host
Browse files Browse the repository at this point in the history
Support host networking mode
  • Loading branch information
fredsig authored Aug 22, 2019
2 parents 04a1e3d + 9dc0e54 commit ed8c5e3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +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. It supports both bridge and awsvpc
network mode.
to the list of Prometheus targets to be scraped. Requires python2 and boto3. Works with Prometheus 2.x. It supports bridge, host, and awsvpc
network modes.

## Setup
``discoverecs.py`` should run alongside the Prometheus server. It generates targets using JSON file service discovery. It can
Expand Down
7 changes: 6 additions & 1 deletion discoverecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ def fetcher(fetch_task_arns):
arn = task['taskDefinitionArn']
no_cache = None
task_definition = self.task_definition_cache.get(arn, fetcher_task_definition)
is_host_network_mode = task_definition.get('networkMode') == 'host'
for container_definition in task_definition['containerDefinitions']:
prometheus = get_environment_var(container_definition['environment'], 'PROMETHEUS')
if container_definition['name'] in no_network_binding and prometheus:
prometheus_port = get_environment_var(container_definition['environment'], 'PROMETHEUS_PORT')
port_mappings = container_definition.get('portMappings')
if container_definition['name'] in no_network_binding and prometheus and not (is_host_network_mode and (prometheus_port or port_mappings)):
log(task['group'] + ':' + container_definition['name'] + ' does not have a networkBinding. Skipping for next run.')
no_cache = True
if not no_cache:
Expand Down Expand Up @@ -282,6 +285,8 @@ def task_info_to_targets(task_info):
ecs_task_name=extract_name(task_info.task['taskDefinitionArn'])
if prom_port:
first_port = prom_port
elif task_info.task_definition.get('networkMode') == 'host':
first_port = str(container_definition['portMappings'][0]['hostPort'])
else:
first_port = str(container['networkBindings'][0]['hostPort'])
if task_info.task_definition.get('networkMode') == 'awsvpc':
Expand Down

0 comments on commit ed8c5e3

Please sign in to comment.