Skip to content

Commit

Permalink
dev: lot fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nmix committed Apr 14, 2023
1 parent 3de68be commit 945856f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ target service environment variables:
## Run from source

```bash
curl -sSL https://install.python-poetry.org | python3 -
git clone https://github.com/nmix/gate-up.git
cd gate-up
pipenv shell
pipenv install
docker-compose up -d
```

Expand Down
31 changes: 10 additions & 21 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,29 @@

logging.basicConfig(level='INFO')

docker_client = docker.DockerClient(base_url='unix://tmp/docker.sock')

WORKING_DIR_LABEL = 'com.docker.compose.project.working_dir'
SCRAPE_LABEL = 'com.github.nmix.gate-up.scrape'

PUSHGATEWAY_URL = os.environ.get('PUSHGATEWAY_URL', 'http://pushgateway:9091')
PUSHGATEWAY_BASIC_AUTH_USERNAME = os.environ.get(
'PUSHGATEWAY_BASIC_AUTH_USERNAME', '')
PUSHGATEWAY_BASIC_AUTH_PASSWORD = os.environ.get(
'PUSHGATEWAY_BASIC_AUTH_PASSWORD', '')

SCRAPE_LABEL = os.environ.get('SCRAPE_LABEL', 'com.github.nmix.gate-up.scrape')
SCRAPE_INTERVAL = int(os.environ.get('SCRAPE_INTERVAL', 5))
if SCRAPE_INTERVAL < 1:
SCRAPE_INTERVAL = 1

DOCKER_BASE_URL = os.environ.get('DOCKER_BASE_URL', 'unix://tmp/docker.sock')
docker_client = docker.DockerClient(base_url=DOCKER_BASE_URL)

def log(msg):
logging.info(msg)
JOB_PREFIX = os.environ.get('JOB_PREFIX', 'env')


def project_containers():
'''
return list of compose project containers
'''
# --- get current container
hostname = open('/etc/hostname').readline()[:-1]
try:
app_container = docker_client.containers.get(hostname)
except docker.errors.NotFound:
raise RuntimeError('Not in docker?')
# --- filter project containers by working project
working_dir = app_container.labels[WORKING_DIR_LABEL]
labels = [f'{WORKING_DIR_LABEL}={working_dir}', SCRAPE_LABEL]
containers = docker_client.containers.list(filters={'label': labels})
containers = [c for c in containers if c != app_container]
containers = docker_client.containers.list(
filters={'label': [SCRAPE_LABEL]})
return containers


Expand Down Expand Up @@ -80,7 +68,8 @@ def __init__(self, url):

def collect(self):
response = requests.get(self.url)
return list(text_string_to_metric_families(response.text))
metrics = list(text_string_to_metric_families(response.text))
return metrics


def push_gateway_handler(url, method, timeout, headers, data):
Expand All @@ -103,13 +92,13 @@ def push_gateway_handler(url, method, timeout, headers, data):
# --- specify the metrics url
port, path = get_container_scrape_params(container)
url = urljoin(f'http://{container.name}:{port}', path)
log(f'scrape metrics from {url}')
logging.info(f'scrape metrics from {url}')
# ---
registry = CollectorRegistry()
registry.register(collector(url))
# --- push metrics to pushgateway
push_to_gateway(
PUSHGATEWAY_URL,
job=container.name,
job=f'{JOB_PREFIX}-{container.name}',
registry=registry,
handler=push_gateway_handler)
10 changes: 8 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ services:
- "--api.dashboard=true"
- "--api.insecure=true"
- "--providers.docker"
- "--providers.docker.constraints=Label(`com.github.nmix.project`, `gate-up`)"
- "--entrypoints.pushgateway.address=:9091"
ports:
- "8080:8080"
- "19091:9091"
- "9091:9091"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
restart: unless-stopped
labels:
- "com.github.nmix.project=gate-up"
# --- basic auth for pushgateway
- "traefik.http.routers.traefik.middlewares=auth"
# echo $(htpasswd -nb admin admin) | sed -e s/\\$/\\$\\$/g
- "traefik.http.middlewares.auth.basicauth.users=admin:$$apr1$$zhYMMcXR$$HIxTpJ.7X801hOXyrxkO3/"

Expand All @@ -27,7 +30,8 @@ services:
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./:/app
# environment:
environment:
JOB_PREFIX: prod
# # --- push metrics to pushgateway with auth
# PUSHGATEWAY_URL: http://pc-ip-address:19091
# PUSHGATEWAY_BASIC_AUTH_USERNAME: admin
Expand All @@ -50,6 +54,7 @@ services:
expose:
- 9100
labels:
- "com.github.nmix.project=gate-up"
- "com.github.nmix.gate-up.scrape"
environment:
SCRAPE_PORT: 9100
Expand All @@ -61,6 +66,7 @@ services:
expose:
- 9091
labels:
- "com.github.nmix.project=gate-up"
- "traefik.http.routers.pushgateway.entrypoints=pushgateway"
- "traefik.http.routers.pushgateway.rule=PathPrefix(`/`)"
- "traefik.http.routers.pushgateway.middlewares=auth"

0 comments on commit 945856f

Please sign in to comment.