diff --git a/judoscale/core/config.py b/judoscale/core/config.py index 7a21584..e7119dc 100644 --- a/judoscale/core/config.py +++ b/judoscale/core/config.py @@ -38,6 +38,8 @@ def initialize(cls, env: Mapping = os.environ): return cls.for_heroku(env) elif env.get("RENDER_INSTANCE_ID"): return cls.for_render(env) + elif env.get("ECS_CONTAINER_METADATA_URI"): + return cls.for_ecs(env) else: return cls(None, "", env) @@ -55,6 +57,13 @@ def for_render(cls, env: Mapping): api_base_url = f"https://adapter.judoscale.com/api/{service_id}" return cls(runtime_container, api_base_url, env) + @classmethod + def for_ecs(cls, env: Mapping): + instance = env["ECS_CONTAINER_METADATA_URI"].split("/")[-1] + runtime_container = RuntimeContainer(instance) + api_base_url = env.get("JUDOSCALE_URL") + return cls(runtime_container, api_base_url, env) + @property def is_enabled(self) -> bool: return bool(self["API_BASE_URL"]) diff --git a/tests/test_config.py b/tests/test_config.py index f85cd31..2fb747e 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -27,6 +27,20 @@ def test_on_render(self): assert config["LOG_LEVEL"] == "WARN" assert config["API_BASE_URL"] == "https://adapter.judoscale.com/api/srv-123" + def test_on_ecs(self): + fake_env = { + "ECS_CONTAINER_METADATA_URI": "http://169.254.170.2/v3/a8880ee042bc4db3ba878dce65b769b6-2750272591", # noqa + "JUDOSCALE_URL": "https://adapter.judoscale.com/api/srv-123", + "LOG_LEVEL": "WARN", + } + config = Config.for_ecs(fake_env) + + assert ( + config["RUNTIME_CONTAINER"] == "a8880ee042bc4db3ba878dce65b769b6-2750272591" + ) + assert config["LOG_LEVEL"] == "WARN" + assert config["API_BASE_URL"] == "https://adapter.judoscale.com/api/srv-123" + def test_is_enabled(self): config = Config(None, "", {}) assert not config.is_enabled