Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,34 @@ def exporter_instance(exporter_cfg_defaults, find_free_port):
yield exporter


@pytest.fixture()
def exporter_instance_auth(find_free_port, celery_config, log_level):
cfg = {
"host": "0.0.0.0",
"port": find_free_port(),
"broker_url": celery_config["broker_url"],
"broker_transport_option": ["visibility_timeout=7200"],
"broker_ssl_option": [],
"retry_interval": 5,
"log_level": log_level,
"accept_content": None,
"worker_timeout": 1,
"purge_offline_worker_metrics": 10,
"initial_queues": ["queue_from_command_line"],
"http_username": "angus",
"http_password": "secret",
}
exporter = Exporter(
worker_timeout_seconds=cfg["worker_timeout"],
purge_offline_worker_metrics_seconds=cfg["purge_offline_worker_metrics"],
initial_queues=cfg["initial_queues"],
http_username=cfg["http_username"],
http_password=cfg["http_password"],
)
setattr(exporter, "cfg", cfg)
yield exporter


@pytest.fixture()
def threaded_exporter(exporter_instance):
thread = threading.Thread(
Expand Down Expand Up @@ -157,6 +185,17 @@ def threaded_exporter_static_labels(exporter_instance_static_labels):
yield exporter_instance_static_labels


@pytest.fixture()
def threaded_exporter_auth(exporter_instance_auth):
thread = threading.Thread(
target=exporter_instance_auth.run,
args=(exporter_instance_auth.cfg,),
daemon=True,
)
thread.start()
yield exporter_instance_auth


@pytest.fixture()
def hostname():
return socket.gethostname()
440 changes: 191 additions & 249 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pytest-celery = "^0.0.0"
pylint = "^3.3.1"
certifi = "^2024.8.30"
idna = "^3.7"
flask-httpauth-stubs = "^0.1.6"

[build-system]
requires = ["poetry-core>=1.0.0a5"]
Expand Down
10 changes: 10 additions & 0 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def _eq_sign_separated_argument_to_dict(_ctx, _param, value):
help="Prefix all metrics with a string. "
"This option replaces the 'celery_*' part with a custom prefix. ",
)
@click.option(
"--http-username", default=None, help="Basic auth username for /metrics endpoint."
)
@click.option(
"--http-password", default=None, help="Basic auth password for /metrics endpoint."
)
Comment on lines +130 to +135

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add an envvar to these ? I want to avoid passwords in commands

Suggested change
@click.option(
"--http-username", default=None, help="Basic auth username for /metrics endpoint."
)
@click.option(
"--http-password", default=None, help="Basic auth password for /metrics endpoint."
)
@click.option(
"--http-username",
default=None,
help="Basic auth username for /metrics endpoint.",
envvar="CELERY_EXPORTER_HTTP_USERNAME"
)
@click.option(
"--http-password",
default=None,
help="Basic auth password for /metrics endpoint.",
envvar="CELERY_EXPORTER_HTTP_PASSWORD"
)

@click.option(
"--default-queue-name",
default="celery",
Expand Down Expand Up @@ -157,6 +163,8 @@ def cli( # pylint: disable=too-many-arguments,too-many-positional-arguments,too
generic_hostname_task_sent_metric,
queues,
metric_prefix,
http_username,
http_password,
default_queue_name,
static_label,
): # pylint: disable=unused-argument
Expand All @@ -169,6 +177,8 @@ def cli( # pylint: disable=too-many-arguments,too-many-positional-arguments,too
generic_hostname_task_sent_metric,
queues,
metric_prefix,
http_username,
http_password,
default_queue_name,
static_label,
).run(ctx.params)
6 changes: 6 additions & 0 deletions src/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def __init__(
generic_hostname_task_sent_metric=False,
initial_queues=None,
metric_prefix="celery_",
http_username=None,
http_password=None,
default_queue_name="celery",
static_label=None,
):
Expand Down Expand Up @@ -146,6 +148,8 @@ def __init__(
["queue_name", *self.static_label_keys],
registry=self.registry,
)
self.http_username = http_username
self.http_password = http_password

def scrape(self):
if (
Expand Down Expand Up @@ -409,6 +413,8 @@ def run(self, click_params):
click_params["host"],
click_params["port"],
self.scrape,
self.http_username,
self.http_password,
)
while True:
try:
Expand Down
26 changes: 24 additions & 2 deletions src/http_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from threading import Thread

import kombu.exceptions
from flask import Blueprint, Flask, current_app, request
from flask import Blueprint, Flask, current_app, request, abort
from flask_httpauth import HTTPBasicAuth
from loguru import logger
from prometheus_client.exposition import choose_encoder
from waitress import serve

blueprint = Blueprint("celery_exporter", __name__)
auth = HTTPBasicAuth()


@blueprint.route("/")
Expand All @@ -28,6 +30,7 @@ def index():


@blueprint.route("/metrics")
@auth.login_required(optional=False)
def metrics():
current_app.config["metrics_puller"]()
encoder, content_type = choose_encoder(request.headers.get("accept"))
Expand All @@ -36,6 +39,7 @@ def metrics():


@blueprint.route("/health")
@auth.login_required(optional=False)
def health():
conn = current_app.config["celery_connection"]
uri = conn.as_uri()
Expand All @@ -51,11 +55,29 @@ def health():
return f"Connected to the broker {conn.as_uri()}"


def start_http_server(registry, celery_connection, host, port, metrics_puller):
# pylint: disable=too-many-positional-arguments,too-many-arguments
def start_http_server(
registry,
celery_connection,
host,
port,
metrics_puller,
http_username=None,
http_password=None,
):
app = Flask(__name__)
app.config["registry"] = registry
app.config["celery_connection"] = celery_connection
app.config["metrics_puller"] = metrics_puller

@auth.verify_password
def verify_password(username, password):
if not http_username or not http_password:
return "anonymous"
if http_username == username and http_password == password:
return "authenticated"
abort(401)

app.register_blueprint(blueprint)
Thread(
target=serve,
Expand Down
21 changes: 21 additions & 0 deletions src/test_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ def test_health(threaded_exporter):
res.raise_for_status()


@pytest.mark.celery()
def test_health_auth_missing(threaded_exporter_auth):
time.sleep(1)
res = requests.get(
f"http://localhost:{threaded_exporter_auth.cfg['port']}/health", timeout=3
)
assert res.status_code == 401


@pytest.mark.celery()
def test_health_auth_present(threaded_exporter_auth):
time.sleep(1)
username = threaded_exporter_auth.cfg["http_username"]
password = threaded_exporter_auth.cfg["http_password"]
res = requests.get(
f"http://{username}:{password}@localhost:{threaded_exporter_auth.cfg['port']}/health",
timeout=3,
)
res.raise_for_status()


def test_index(threaded_exporter):
time.sleep(1)
res = requests.get(f"http://localhost:{threaded_exporter.cfg['port']}", timeout=3)
Expand Down