Skip to content

Commit

Permalink
Enable overriding environment detection (#60)
Browse files Browse the repository at this point in the history
Sometimes it would be great to override the default selected environment (e.g. to force usage of the Lambda environment outside of Lambda), which this change enables.

The code is based on #26.
  • Loading branch information
02strich authored Jan 5, 2021
1 parent 8878b3b commit 5aa2d87
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
4 changes: 4 additions & 0 deletions aws_embedded_metrics/config/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional


class Configuration:
def __init__(
Expand All @@ -24,6 +26,7 @@ def __init__(
ec2_metadata_endpoint: str = None,
namespace: str = None,
disable_metric_extraction: bool = False,
environment: Optional[str] = None,
):
self.debug_logging_enabled = debug_logging_enabled
self.service_name = service_name
Expand All @@ -34,3 +37,4 @@ def __init__(
self.ec2_metadata_endpoint = ec2_metadata_endpoint
self.namespace = namespace
self.disable_metric_extraction = disable_metric_extraction
self.environment = environment
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
EC2_METADATA_ENDPOINT = "EC2_METADATA_ENDPOINT"
NAMESPACE = "NAMESPACE"
DISABLE_METRIC_EXTRACTION = "DISABLE_METRIC_EXTRACTION"
ENVIRONMENT_OVERRIDE = "ENVIRONMENT"


class EnvironmentConfigurationProvider:
Expand All @@ -43,6 +44,7 @@ def get_configuration(self) -> Configuration:
self.__get_env_var(EC2_METADATA_ENDPOINT),
self.__get_env_var(NAMESPACE),
self.__get_bool_env_var(DISABLE_METRIC_EXTRACTION),
self.__get_env_var(ENVIRONMENT_OVERRIDE),
)

@staticmethod
Expand Down
22 changes: 20 additions & 2 deletions aws_embedded_metrics/environment/environment_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# limitations under the License.

import logging
from aws_embedded_metrics import config
from aws_embedded_metrics.environment import Environment
from aws_embedded_metrics.environment.default_environment import DefaultEnvironment
from aws_embedded_metrics.environment.lambda_environment import LambdaEnvironment
Expand All @@ -20,7 +21,11 @@

log = logging.getLogger(__name__)

environments = [LambdaEnvironment(), EC2Environment()]
lambda_environment = LambdaEnvironment()
ec2_environment = EC2Environment()
default_environment = DefaultEnvironment()
environments = [lambda_environment, ec2_environment]
Config = config.get_config()


class EnvironmentCache:
Expand All @@ -32,6 +37,19 @@ async def resolve_environment() -> Environment:
log.debug("Environment resolved from cache.")
return EnvironmentCache.environment

if Config.environment:
lower_configured_enviroment = Config.environment.lower()
if lower_configured_enviroment == "lambda":
EnvironmentCache.environment = lambda_environment
elif lower_configured_enviroment == "ec2":
EnvironmentCache.environment = ec2_environment
elif lower_configured_enviroment == "default":
EnvironmentCache.environment = default_environment
else:
log.info("Failed to understand environment override: %s", Config.environment)
if EnvironmentCache.environment is not None:
return EnvironmentCache.environment

for env_under_test in environments:
is_environment = False
try:
Expand All @@ -49,5 +67,5 @@ async def resolve_environment() -> Environment:
return env_under_test

log.info("No environment was detected. Using default.")
EnvironmentCache.environment = DefaultEnvironment()
EnvironmentCache.environment = default_environment
return EnvironmentCache.environment
7 changes: 7 additions & 0 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_can_get_config_from_environment(monkeypatch):
ec2_metadata_endpoint = fake.word()
namespace = fake.word()
disable_metric_extraction = True
environment_override = fake.word()

monkeypatch.setenv("AWS_EMF_ENABLE_DEBUG_LOGGING", str(debug_enabled))
monkeypatch.setenv("AWS_EMF_SERVICE_NAME", service_name)
Expand All @@ -34,6 +35,7 @@ def test_can_get_config_from_environment(monkeypatch):
monkeypatch.setenv("AWS_EMF_EC2_METADATA_ENDPOINT", ec2_metadata_endpoint)
monkeypatch.setenv("AWS_EMF_NAMESPACE", namespace)
monkeypatch.setenv("AWS_EMF_DISABLE_METRIC_EXTRACTION", str(disable_metric_extraction))
monkeypatch.setenv("AWS_EMF_ENVIRONMENT", environment_override)

# act
result = get_config()
Expand All @@ -48,6 +50,7 @@ def test_can_get_config_from_environment(monkeypatch):
assert result.ec2_metadata_endpoint == ec2_metadata_endpoint
assert result.namespace == namespace
assert result.disable_metric_extraction == disable_metric_extraction
assert result.environment == environment_override


def test_can_override_config(monkeypatch):
Expand All @@ -61,6 +64,7 @@ def test_can_override_config(monkeypatch):
monkeypatch.setenv("AWS_EMF_EC2_METADATA_ENDPOINT", fake.word())
monkeypatch.setenv("AWS_EMF_NAMESPACE", fake.word())
monkeypatch.setenv("AWS_EMF_DISABLE_METRIC_EXTRACTION", str(True))
monkeypatch.setenv("AWS_EMF_ENVIRONMENT", fake.word())

config = get_config()

Expand All @@ -73,6 +77,7 @@ def test_can_override_config(monkeypatch):
ec2_metadata_endpoint = fake.word()
namespace = fake.word()
disable_metric_extraction = False
environment = fake.word()

# act
config.debug_logging_enabled = debug_enabled
Expand All @@ -84,6 +89,7 @@ def test_can_override_config(monkeypatch):
config.ec2_metadata_endpoint = ec2_metadata_endpoint
config.namespace = namespace
config.disable_metric_extraction = disable_metric_extraction
config.environment = environment

# assert
assert config.debug_logging_enabled == debug_enabled
Expand All @@ -95,3 +101,4 @@ def test_can_override_config(monkeypatch):
assert config.ec2_metadata_endpoint == ec2_metadata_endpoint
assert config.namespace == namespace
assert config.disable_metric_extraction == disable_metric_extraction
assert config.environment == environment
31 changes: 29 additions & 2 deletions tests/environment/test_environment_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import pytest
from importlib import reload

from aws_embedded_metrics.config import get_config
from aws_embedded_metrics import config
from aws_embedded_metrics.environment.lambda_environment import LambdaEnvironment
from aws_embedded_metrics.environment.default_environment import DefaultEnvironment

from aws_embedded_metrics.environment import ec2_environment
from aws_embedded_metrics.environment import environment_detector

fake = Faker()
Config = get_config()


@pytest.fixture
Expand Down Expand Up @@ -60,3 +59,31 @@ async def test_resolve_environment_returns_default_envionment(before):

# assert
assert isinstance(result, DefaultEnvironment)


@pytest.mark.asyncio
async def test_resolve_environment_returns_override_ec2(before, monkeypatch):
# arrange
monkeypatch.setenv("AWS_EMF_ENVIRONMENT", "ec2")
reload(config)
reload(environment_detector)

# act
result = await environment_detector.resolve_environment()

# assert
assert isinstance(result, ec2_environment.EC2Environment)


@pytest.mark.asyncio
async def test_resolve_environment_returns_override_lambda(before, monkeypatch):
# arrange
monkeypatch.setenv("AWS_EMF_ENVIRONMENT", "lambda")
reload(config)
reload(environment_detector)

# act
result = await environment_detector.resolve_environment()

# assert
assert isinstance(result, LambdaEnvironment)

0 comments on commit 5aa2d87

Please sign in to comment.