Skip to content

Commit dc7227d

Browse files
committed
Allow users to disable event serialization if metrics are empty
1 parent 3e5ccce commit dc7227d

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,22 @@ Config = get_config()
262262
Config.disable_metric_extraction = True
263263

264264
# environment
265-
AWS_EMF_DISABLE_METRIC_EXTRACTION = true
265+
AWS_EMF_DISABLE_METRIC_EXTRACTION=true
266+
```
267+
268+
**ONLY_LOG_EVENTS_WITH_METRICS**: When this property is set to `true`, LogEvents without any metrics
269+
set will be dropped.
270+
271+
Example:
272+
273+
```py
274+
# in process
275+
from aws_embedded_metrics.config import get_config
276+
Config = get_config()
277+
Config.only_log_events_with_metrics = True
278+
279+
# environment
280+
AWS_EMF_ONLY_LOG_EVENTS_WITH_METRICS=true
266281
```
267282

268283
## Examples

aws_embedded_metrics/config/configuration.py

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(
2727
namespace: str = None,
2828
disable_metric_extraction: bool = False,
2929
environment: Optional[str] = None,
30+
only_log_events_with_metrics: bool = False,
3031
):
3132
self.debug_logging_enabled = debug_logging_enabled
3233
self.service_name = service_name
@@ -38,3 +39,4 @@ def __init__(
3839
self.namespace = namespace
3940
self.disable_metric_extraction = disable_metric_extraction
4041
self.environment = environment
42+
self.only_log_events_with_metrics = only_log_events_with_metrics

aws_embedded_metrics/config/environment_configuration_provider.py

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
NAMESPACE = "NAMESPACE"
2727
DISABLE_METRIC_EXTRACTION = "DISABLE_METRIC_EXTRACTION"
2828
ENVIRONMENT_OVERRIDE = "ENVIRONMENT"
29+
ONLY_LOG_EVENTS_WITH_METRICS = "ONLY_LOG_EVENTS_WITH_METRICS"
2930

3031

3132
class EnvironmentConfigurationProvider:
@@ -45,6 +46,7 @@ def get_configuration(self) -> Configuration:
4546
self.__get_env_var(NAMESPACE),
4647
self.__get_bool_env_var(DISABLE_METRIC_EXTRACTION),
4748
self.__get_env_var(ENVIRONMENT_OVERRIDE),
49+
self.__get_bool_env_var(ONLY_LOG_EVENTS_WITH_METRICS),
4850
)
4951

5052
@staticmethod

aws_embedded_metrics/serializers/log_serializer.py

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def serialize(context: MetricsContext) -> List[str]:
2929
dimension_keys = []
3030
dimensions_properties: Dict[str, str] = {}
3131

32+
# allows users to skip emission entirely if no metrics have been
33+
# added to the logger
34+
if config.only_log_events_with_metrics and not context.metrics:
35+
return []
36+
3237
for dimension_set in context.get_dimensions():
3338
keys = list(dimension_set.keys())
3439
dimension_keys.append(keys[0:MAX_DIMENSIONS])

tests/config/test_config.py

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_can_get_config_from_environment(monkeypatch):
2525
namespace = fake.word()
2626
disable_metric_extraction = True
2727
environment_override = fake.word()
28+
only_log_events_with_metrics = True
2829

2930
monkeypatch.setenv("AWS_EMF_ENABLE_DEBUG_LOGGING", str(debug_enabled))
3031
monkeypatch.setenv("AWS_EMF_SERVICE_NAME", service_name)
@@ -36,6 +37,7 @@ def test_can_get_config_from_environment(monkeypatch):
3637
monkeypatch.setenv("AWS_EMF_NAMESPACE", namespace)
3738
monkeypatch.setenv("AWS_EMF_DISABLE_METRIC_EXTRACTION", str(disable_metric_extraction))
3839
monkeypatch.setenv("AWS_EMF_ENVIRONMENT", environment_override)
40+
monkeypatch.setenv("AWS_EMF_ONLY_LOG_EVENTS_WITH_METRICS", only_log_events_with_metrics)
3941

4042
# act
4143
result = get_config()
@@ -51,6 +53,7 @@ def test_can_get_config_from_environment(monkeypatch):
5153
assert result.namespace == namespace
5254
assert result.disable_metric_extraction == disable_metric_extraction
5355
assert result.environment == environment_override
56+
assert result.only_log_events_with_metrics == only_log_events_with_metrics
5457

5558

5659
def test_can_override_config(monkeypatch):
@@ -65,6 +68,7 @@ def test_can_override_config(monkeypatch):
6568
monkeypatch.setenv("AWS_EMF_NAMESPACE", fake.word())
6669
monkeypatch.setenv("AWS_EMF_DISABLE_METRIC_EXTRACTION", str(True))
6770
monkeypatch.setenv("AWS_EMF_ENVIRONMENT", fake.word())
71+
monkeypatch.setenv("AWS_EMF_ONLY_LOG_EVENTS_WITH_METRICS", str(True))
6872

6973
config = get_config()
7074

@@ -78,6 +82,7 @@ def test_can_override_config(monkeypatch):
7882
namespace = fake.word()
7983
disable_metric_extraction = False
8084
environment = fake.word()
85+
only_log_events_with_metrics = False
8186

8287
# act
8388
config.debug_logging_enabled = debug_enabled
@@ -90,6 +95,7 @@ def test_can_override_config(monkeypatch):
9095
config.namespace = namespace
9196
config.disable_metric_extraction = disable_metric_extraction
9297
config.environment = environment
98+
config.only_log_events_with_metrics = only_log_events_with_metrics
9399

94100
# assert
95101
assert config.debug_logging_enabled == debug_enabled
@@ -102,3 +108,4 @@ def test_can_override_config(monkeypatch):
102108
assert config.namespace == namespace
103109
assert config.disable_metric_extraction == disable_metric_extraction
104110
assert config.environment == environment
111+
assert config.only_log_events_with_metrics == only_log_events_with_metrics

tests/serializer/test_log_serializer.py

+29
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,35 @@ def test_serialize_metrics_with_aggregation_disabled():
280280
assert_json_equality(result_json, expected)
281281

282282

283+
def test_serialize_metrics_without_metrics_when_only_log_events_with_metrics_true():
284+
"""Test log records don't contain metadata when aggregation is disabled."""
285+
# arrange
286+
config = get_config()
287+
config.only_log_events_with_metrics = True
288+
289+
context = get_context()
290+
291+
# act
292+
result_json = serializer.serialize(context)
293+
294+
# assert
295+
assert len(result_json) == 0
296+
297+
298+
def test_serialize_metrics_without_metrics_when_only_log_events_with_metrics_false():
299+
"""Test log records don't contain metadata when aggregation is disabled."""
300+
# arrange
301+
config = get_config()
302+
config.only_log_events_with_metrics = False
303+
304+
context = get_context()
305+
306+
# act
307+
result_json = serializer.serialize(context)
308+
309+
# assert
310+
assert len(result_json) == 1
311+
283312
# Test utility method
284313

285314

0 commit comments

Comments
 (0)