Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
sclg-yelp committed Aug 29, 2023
1 parent 765d1c1 commit 987af47
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
29 changes: 18 additions & 11 deletions paasta_tools/setup_prometheus_adapter_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
from paasta_tools.kubernetes_tools import sanitise_kubernetes_name
from paasta_tools.kubernetes_tools import V1Pod
from paasta_tools.long_running_service_tools import AutoscalingParamsDict
from paasta_tools.long_running_service_tools import (
DEFAULT_ACTIVE_REQUESTS_AUTOSCALING_MOVING_AVERAGE_WINDOW,
)
from paasta_tools.long_running_service_tools import (
DEFAULT_CPU_AUTOSCALING_MOVING_AVERAGE_WINDOW,
)
Expand All @@ -52,9 +55,6 @@
from paasta_tools.long_running_service_tools import (
DEFAULT_UWSGI_AUTOSCALING_MOVING_AVERAGE_WINDOW,
)
from paasta_tools.long_running_service_tools import (
DEFAULT_ACTIVE_REQUESTS_AUTOSCALING_MOVING_AVERAGE_WINDOW,
)
from paasta_tools.paasta_service_config_loader import PaastaServiceConfigLoader
from paasta_tools.utils import DEFAULT_SOA_DIR
from paasta_tools.utils import get_services_for_cluster
Expand Down Expand Up @@ -244,18 +244,21 @@ def should_create_active_requests_scaling_rule(


def create_instance_active_requests_scaling_rule(
service: str,
instance: str,
autoscaling_config: AutoscalingParamsDict,
paasta_cluster: str,
namespace: str = "paasta",
service: str,
instance: str,
autoscaling_config: AutoscalingParamsDict,
paasta_cluster: str,
namespace: str = "paasta",
) -> PrometheusAdapterRule:
"""
Creates a Prometheus adapter rule config for a given service instance.
"""
desired_active_requests_per_replica = autoscaling_config["desired_active_requests_per_replica"]
desired_active_requests_per_replica = autoscaling_config[
"desired_active_requests_per_replica"
]
moving_average_window = autoscaling_config.get(
"moving_average_window_seconds", DEFAULT_ACTIVE_REQUESTS_AUTOSCALING_MOVING_AVERAGE_WINDOW
"moving_average_window_seconds",
DEFAULT_ACTIVE_REQUESTS_AUTOSCALING_MOVING_AVERAGE_WINDOW,
)
deployment_name = get_kubernetes_app_name(service=service, instance=instance)

Expand Down Expand Up @@ -340,6 +343,7 @@ def create_instance_active_requests_scaling_rule(
"metricsQuery": _minify_promql(metrics_query),
}


def create_instance_uwsgi_scaling_rule(
service: str,
instance: str,
Expand Down Expand Up @@ -889,7 +893,10 @@ def get_rules_for_service_instance(
rules: List[PrometheusAdapterRule] = []

for should_create_scaling_rule, create_instance_scaling_rule in (
(should_create_active_requests_scaling_rule, create_instance_active_requests_scaling_rule),
(
should_create_active_requests_scaling_rule,
create_instance_active_requests_scaling_rule,
),
(should_create_uwsgi_scaling_rule, create_instance_uwsgi_scaling_rule),
(should_create_piscina_scaling_rule, create_instance_piscina_scaling_rule),
(should_create_cpu_scaling_rule, create_instance_cpu_scaling_rule),
Expand Down
26 changes: 15 additions & 11 deletions tests/test_setup_prometheus_adapter_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from paasta_tools.long_running_service_tools import AutoscalingParamsDict
from paasta_tools.setup_prometheus_adapter_config import _minify_promql
from paasta_tools.setup_prometheus_adapter_config import (
create_instance_active_requests_scaling_rule,
)
from paasta_tools.setup_prometheus_adapter_config import (
create_instance_arbitrary_promql_scaling_rule,
)
Expand All @@ -15,20 +18,17 @@
from paasta_tools.setup_prometheus_adapter_config import (
create_instance_uwsgi_scaling_rule,
)
from paasta_tools.setup_prometheus_adapter_config import get_rules_for_service_instance
from paasta_tools.setup_prometheus_adapter_config import (
create_instance_active_requests_scaling_rule,
should_create_active_requests_scaling_rule,
)
from paasta_tools.setup_prometheus_adapter_config import get_rules_for_service_instance
from paasta_tools.setup_prometheus_adapter_config import should_create_cpu_scaling_rule
from paasta_tools.setup_prometheus_adapter_config import (
should_create_gunicorn_scaling_rule,
)
from paasta_tools.setup_prometheus_adapter_config import (
should_create_uwsgi_scaling_rule,
)
from paasta_tools.setup_prometheus_adapter_config import (
should_create_active_requests_scaling_rule,
)
from paasta_tools.utils import SystemPaastaConfig

MOCK_SYSTEM_PAASTA_CONFIG = SystemPaastaConfig(
Expand Down Expand Up @@ -67,7 +67,7 @@
],
)
def test_should_create_active_requests_scaling_rule(
autoscaling_config: AutoscalingParamsDict, expected: bool
autoscaling_config: AutoscalingParamsDict, expected: bool
) -> None:
should_create, reason = should_create_active_requests_scaling_rule(
autoscaling_config=autoscaling_config
Expand All @@ -91,9 +91,9 @@ def test_create_instance_active_requests_scaling_rule() -> None:
}

with mock.patch(
"paasta_tools.setup_prometheus_adapter_config.load_system_paasta_config",
autospec=True,
return_value=MOCK_SYSTEM_PAASTA_CONFIG,
"paasta_tools.setup_prometheus_adapter_config.load_system_paasta_config",
autospec=True,
return_value=MOCK_SYSTEM_PAASTA_CONFIG,
):
rule = create_instance_active_requests_scaling_rule(
service=service_name,
Expand All @@ -110,10 +110,14 @@ def test_create_instance_active_requests_scaling_rule() -> None:
assert instance_name in rule["seriesQuery"]
assert paasta_cluster in rule["seriesQuery"]
# these two numbers are distinctive and unlikely to be used as constants
assert str(autoscaling_config["desired_active_requests_per_replica"]) in rule["metricsQuery"]
assert (
str(autoscaling_config["moving_average_window_seconds"]) in rule["metricsQuery"]
str(autoscaling_config["desired_active_requests_per_replica"])
in rule["metricsQuery"]
)
assert (
str(autoscaling_config["moving_average_window_seconds"]) in rule["metricsQuery"]
)


@pytest.mark.parametrize(
"autoscaling_config,expected",
Expand Down

0 comments on commit 987af47

Please sign in to comment.