Skip to content

Commit

Permalink
Merge pull request #6 from stromnet/master
Browse files Browse the repository at this point in the history
Add support for random hostPort network bindings
  • Loading branch information
fredsig authored Mar 7, 2020
2 parents df5f29f + 3bb5838 commit 5738e22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ will be exposed and the instance label will always point to the job name.

All network modes are supported (bridge, host and awsvpc).

If PROMETHEUS_PORT is not set, the script will pick the first port from the container
If PROMETHEUS_PORT and PROMETHEUS_CONTAINER_PORT are not set, the script will pick the first port from the container
definition (in awsvpc and host network mode) or the container host network bindings
in bridge mode. On Fargate, if PROMETHEUS_PORT is not set, it will default to port 80.

If PROMETHEUS_CONTAINER_PORT is set, it will look at the container host network bindings, and find the entry with a matching containerPort. It will then use the hostPort found there as target port.
This is useful when the container port is known, but the hostPort is randomly picked by ECS (by setting hostPort to 0 in the task definition).

If your container uses multiple ports, it's recommended to specify PROMETHEUS_PORT explicitly.
4 changes: 4 additions & 0 deletions discoverecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ def task_info_to_targets(task_info):
metrics_path = get_environment_var(container_definition['environment'], 'PROMETHEUS_ENDPOINT')
nolabels = get_environment_var(container_definition['environment'], 'PROMETHEUS_NOLABELS')
prom_port = get_environment_var(container_definition['environment'], 'PROMETHEUS_PORT')
prom_container_port = get_environment_var(container_definition['environment'], 'PROMETHEUS_CONTAINER_PORT')
if nolabels != 'true': nolabels = None
containers = filter(lambda c:c['name'] == container_definition['name'], task_info.task['containers'])
if prometheus:
Expand All @@ -298,6 +299,9 @@ def task_info_to_targets(task_info):
first_port = str(container_definition['portMappings'][0]['hostPort'])
else:
first_port = '80'
elif prom_container_port:
binding_by_container_port = filter(lambda c:str(c['containerPort']) == prom_container_port, container['networkBindings'])
first_port = str(binding_by_container_port[0]['hostPort'])
else:
first_port = str(container['networkBindings'][0]['hostPort'])

Expand Down

0 comments on commit 5738e22

Please sign in to comment.