Skip to content

Commit a878b55

Browse files
ridhajaredcnance
andauthored
Allow CloudWatch namespace to be specifiable via environment variable (#28)
Allow CloudWatch namespace to be specifiable via environment variable Co-authored-by: Jared Nance <[email protected]>
1 parent 99aec8f commit a878b55

File tree

7 files changed

+57
-20
lines changed

7 files changed

+57
-20
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,25 @@ Config.log_stream_name = "LogStreamName";
231231
AWS_EMF_LOG_STREAM_NAME = LogStreamName;
232232
```
233233

234+
**NameSpace**: Overrides the CloudWatch [namespace](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html#Namespace). If not set, a default value of aws-embedded-metrics will be used.
235+
236+
Requirements:
237+
238+
- Name Length 1-512 characters
239+
- Name must be ASCII characters only
240+
241+
Example:
242+
243+
```py
244+
# in process
245+
from aws_embedded_metrics.config import get_config
246+
Config = get_config()
247+
Config.namespace = "MyApplication";
248+
249+
# environment
250+
AWS_EMF_NAMESPACE = MyApplication;
251+
```
252+
234253
## Examples
235254

236255
Check out the [examples](https://github.com/awslabs/aws-embedded-metrics-python/tree/master/examples) directory to get started.

aws_embedded_metrics/config/configuration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
log_stream_name: str,
2323
agent_endpoint: str,
2424
ec2_metadata_endpoint: str = None,
25+
namespace: str = None,
2526
):
2627
self.debug_logging_enabled = debug_logging_enabled
2728
self.service_name = service_name
@@ -30,3 +31,4 @@ def __init__(
3031
self.log_stream_name = log_stream_name
3132
self.agent_endpoint = agent_endpoint
3233
self.ec2_metadata_endpoint = ec2_metadata_endpoint
34+
self.namespace = namespace

aws_embedded_metrics/config/environment_configuration_provider.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
LOG_STREAM_NAME = "LOG_STREAM_NAME"
2424
AGENT_ENDPOINT = "AGENT_ENDPOINT"
2525
EC2_METADATA_ENDPOINT = "EC2_METADATA_ENDPOINT"
26+
NAMESPACE = "NAMESPACE"
2627

2728

2829
class EnvironmentConfigurationProvider:
@@ -39,6 +40,7 @@ def get_configuration(self) -> Configuration:
3940
self.__get_env_var(LOG_STREAM_NAME),
4041
self.__get_env_var(AGENT_ENDPOINT),
4142
self.__get_env_var(EC2_METADATA_ENDPOINT),
43+
self.__get_env_var(NAMESPACE),
4244
)
4345

4446
@staticmethod

aws_embedded_metrics/logger/metrics_context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414

1515
from aws_embedded_metrics import constants, utils
16+
from aws_embedded_metrics.config import get_config
1617
from aws_embedded_metrics.logger.metric import Metric
1718
from typing import List, Dict, Any
1819

@@ -30,7 +31,7 @@ def __init__(
3031
default_dimensions: Dict[str, str] = None,
3132
):
3233

33-
self.namespace: str = namespace or constants.DEFAULT_NAMESPACE
34+
self.namespace: str = namespace or get_config().namespace or constants.DEFAULT_NAMESPACE
3435
self.properties: Dict[str, Any] = properties or {}
3536
self.dimensions: List[Dict[str, str]] = dimensions or []
3637
self.default_dimensions: Dict[str, str] = default_dimensions or {}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="aws-embedded-metrics",
8-
version="1.0.1b3",
8+
version="1.0.1b4",
99
author="Amazon Web Services",
1010
author_email="[email protected]",
1111
description="AWS Embedded Metrics Package",

tests/config/test_config.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from aws_embedded_metrics import config
22
from faker import Faker
3-
import os
43
from importlib import reload
54

65

@@ -14,7 +13,7 @@ def get_config():
1413
return config.get_config()
1514

1615

17-
def test_can_get_config_from_environment():
16+
def test_can_get_config_from_environment(monkeypatch):
1817
# arrange
1918
debug_enabled = True
2019
service_name = fake.word()
@@ -23,14 +22,16 @@ def test_can_get_config_from_environment():
2322
log_stream = fake.word()
2423
agent_endpoint = fake.word()
2524
ec2_metadata_endpoint = fake.word()
25+
namespace = fake.word()
2626

27-
os.environ["AWS_EMF_ENABLE_DEBUG_LOGGING"] = str(debug_enabled)
28-
os.environ["AWS_EMF_SERVICE_NAME"] = service_name
29-
os.environ["AWS_EMF_SERVICE_TYPE"] = service_type
30-
os.environ["AWS_EMF_LOG_GROUP_NAME"] = log_group
31-
os.environ["AWS_EMF_LOG_STREAM_NAME"] = log_stream
32-
os.environ["AWS_EMF_AGENT_ENDPOINT"] = agent_endpoint
33-
os.environ["AWS_EMF_EC2_METADATA_ENDPOINT"] = ec2_metadata_endpoint
27+
monkeypatch.setenv("AWS_EMF_ENABLE_DEBUG_LOGGING", str(debug_enabled))
28+
monkeypatch.setenv("AWS_EMF_SERVICE_NAME", service_name)
29+
monkeypatch.setenv("AWS_EMF_SERVICE_TYPE", service_type)
30+
monkeypatch.setenv("AWS_EMF_LOG_GROUP_NAME", log_group)
31+
monkeypatch.setenv("AWS_EMF_LOG_STREAM_NAME", log_stream)
32+
monkeypatch.setenv("AWS_EMF_AGENT_ENDPOINT", agent_endpoint)
33+
monkeypatch.setenv("AWS_EMF_EC2_METADATA_ENDPOINT", ec2_metadata_endpoint)
34+
monkeypatch.setenv("AWS_EMF_NAMESPACE", namespace)
3435

3536
# act
3637
result = get_config()
@@ -43,17 +44,19 @@ def test_can_get_config_from_environment():
4344
assert result.log_stream_name == log_stream
4445
assert result.agent_endpoint == agent_endpoint
4546
assert result.ec2_metadata_endpoint == ec2_metadata_endpoint
47+
assert result.namespace == namespace
4648

4749

48-
def test_can_override_config():
50+
def test_can_override_config(monkeypatch):
4951
# arrange
50-
os.environ["AWS_EMF_ENABLE_DEBUG_LOGGING"] = str(True)
51-
os.environ["AWS_EMF_SERVICE_NAME"] = fake.word()
52-
os.environ["AWS_EMF_SERVICE_TYPE"] = fake.word()
53-
os.environ["AWS_EMF_LOG_GROUP_NAME"] = fake.word()
54-
os.environ["AWS_EMF_LOG_STREAM_NAME"] = fake.word()
55-
os.environ["AWS_EMF_AGENT_ENDPOINT"] = fake.word()
56-
os.environ["AWS_EMF_EC2_METADATA_ENDPOINT"] = fake.word()
52+
monkeypatch.setenv("AWS_EMF_ENABLE_DEBUG_LOGGING", str(True))
53+
monkeypatch.setenv("AWS_EMF_SERVICE_NAME", fake.word())
54+
monkeypatch.setenv("AWS_EMF_SERVICE_TYPE", fake.word())
55+
monkeypatch.setenv("AWS_EMF_LOG_GROUP_NAME", fake.word())
56+
monkeypatch.setenv("AWS_EMF_LOG_STREAM_NAME", fake.word())
57+
monkeypatch.setenv("AWS_EMF_AGENT_ENDPOINT", fake.word())
58+
monkeypatch.setenv("AWS_EMF_EC2_METADATA_ENDPOINT", fake.word())
59+
monkeypatch.setenv("AWS_EMF_NAMESPACE", fake.word())
5760

5861
config = get_config()
5962

@@ -64,6 +67,7 @@ def test_can_override_config():
6467
log_stream = fake.word()
6568
agent_endpoint = fake.word()
6669
ec2_metadata_endpoint = fake.word()
70+
namespace = fake.word()
6771

6872
# act
6973
config.debug_logging_enabled = debug_enabled
@@ -73,6 +77,7 @@ def test_can_override_config():
7377
config.log_stream_name = log_stream
7478
config.agent_endpoint = agent_endpoint
7579
config.ec2_metadata_endpoint = ec2_metadata_endpoint
80+
config.namespace = namespace
7681

7782
# assert
7883
assert config.debug_logging_enabled == debug_enabled
@@ -82,3 +87,4 @@ def test_can_override_config():
8287
assert config.log_stream_name == log_stream
8388
assert config.agent_endpoint == agent_endpoint
8489
assert config.ec2_metadata_endpoint == ec2_metadata_endpoint
90+
assert config.namespace == namespace

tests/logger/test_metrics_context.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from aws_embedded_metrics import config
12
from aws_embedded_metrics.logger.metrics_context import MetricsContext
23
from aws_embedded_metrics import utils
4+
from aws_embedded_metrics.constants import DEFAULT_NAMESPACE
35
from _pytest.monkeypatch import MonkeyPatch
46
import pytest
7+
from importlib import reload
58
from faker import Faker
69

710
fake = Faker()
@@ -16,12 +19,16 @@ def mock_time():
1619

1720

1821
def test_can_create_context_with_no_arguments(mock_time):
22+
# reload the configuration module since it is loaded on
23+
# startup and cached
24+
reload(config)
25+
1926
# arrange
2027
# act
2128
context = MetricsContext()
2229

2330
# assert
24-
assert context.namespace == "aws-embedded-metrics"
31+
assert context.namespace == DEFAULT_NAMESPACE
2532
assert context.meta == {"Timestamp": mock_time}
2633
assert context.properties == {}
2734
assert context.dimensions == []

0 commit comments

Comments
 (0)