Skip to content

Commit a80d047

Browse files
author
Mark Kuhn
authored
use param in context copy instead of added functn (#92)
1 parent 18a7562 commit a80d047

File tree

3 files changed

+5
-15
lines changed

3 files changed

+5
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ set_namespace("MyApplication")
170170
- **flush**()
171171

172172
Flushes the current MetricsContext to the configured sink and resets all properties and metric values. The namespace and default dimensions will be preserved across flushes.
173-
Custom dimensions are **not** preserved by default, but this behavior can be changed by invoking `logger.flush_preserve_dimensions = True`, so that custom dimensions would be preserved after each flushing thereafter.
173+
Custom dimensions are **not** preserved by default, but this behavior can be changed by setting `logger.flush_preserve_dimensions = True`, so that custom dimensions would be preserved after each flushing thereafter.
174174

175175
Example:
176176

aws_embedded_metrics/logger/metrics_context.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ def get_dimensions(self) -> List[Dict]:
143143
def __has_default_dimensions(self) -> bool:
144144
return self.default_dimensions is not None and len(self.default_dimensions) > 0
145145

146-
def create_copy_with_context(self) -> "MetricsContext":
146+
def create_copy_with_context(self, preserve_dimensions: bool = False) -> "MetricsContext":
147147
"""
148148
Creates a deep copy of the context excluding metrics.
149+
Custom dimensions are NOT preserved by default unless preserve_dimensions parameter is set.
149150
"""
150151
new_properties: Dict = {}
151152
new_properties.update(self.properties)
@@ -160,7 +161,7 @@ def create_copy_with_context(self) -> "MetricsContext":
160161
#
161162
# my_func()
162163
# my_func()
163-
new_dimensions: List[Dict] = []
164+
new_dimensions: List[Dict] = [] if not preserve_dimensions else self.dimensions
164165

165166
new_default_dimensions: Dict = {}
166167
new_default_dimensions.update(self.default_dimensions)
@@ -169,16 +170,6 @@ def create_copy_with_context(self) -> "MetricsContext":
169170
self.namespace, new_properties, new_dimensions, new_default_dimensions
170171
)
171172

172-
def create_copy_with_context_with_dimensions(self) -> "MetricsContext":
173-
"""
174-
Creates a deep copy of the context excluding metrics.
175-
Custom dimensions will be copied, this helps with the reuse of dimension sets.
176-
"""
177-
new_context = self.create_copy_with_context()
178-
new_context.dimensions.extend(self.dimensions)
179-
180-
return new_context
181-
182173
@staticmethod
183174
def empty() -> "MetricsContext":
184175
return MetricsContext()

aws_embedded_metrics/logger/metrics_logger.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ async def flush(self) -> None:
4444

4545
# accept and reset the context
4646
sink.accept(self.context)
47-
self.context = self.context.create_copy_with_context() if not self.flush_preserve_dimensions \
48-
else self.context.create_copy_with_context_with_dimensions()
47+
self.context = self.context.create_copy_with_context(self.flush_preserve_dimensions)
4948

5049
def __configure_context_for_environment(self, env: Environment) -> None:
5150
default_dimensions = {

0 commit comments

Comments
 (0)