Skip to content

Use param in context copy instead of added function #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ set_namespace("MyApplication")
- **flush**()

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.
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.
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.

Example:

Expand Down
15 changes: 3 additions & 12 deletions aws_embedded_metrics/logger/metrics_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ def get_dimensions(self) -> List[Dict]:
def __has_default_dimensions(self) -> bool:
return self.default_dimensions is not None and len(self.default_dimensions) > 0

def create_copy_with_context(self) -> "MetricsContext":
def create_copy_with_context(self, preserve_dimensions: bool = False) -> "MetricsContext":
"""
Creates a deep copy of the context excluding metrics.
Custom dimensions are NOT preserved by default unless preserve_dimensions parameter is set.
"""
new_properties: Dict = {}
new_properties.update(self.properties)
Expand All @@ -160,7 +161,7 @@ def create_copy_with_context(self) -> "MetricsContext":
#
# my_func()
# my_func()
new_dimensions: List[Dict] = []
new_dimensions: List[Dict] = [] if not preserve_dimensions else self.dimensions

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

def create_copy_with_context_with_dimensions(self) -> "MetricsContext":
"""
Creates a deep copy of the context excluding metrics.
Custom dimensions will be copied, this helps with the reuse of dimension sets.
"""
new_context = self.create_copy_with_context()
new_context.dimensions.extend(self.dimensions)

return new_context

@staticmethod
def empty() -> "MetricsContext":
return MetricsContext()
3 changes: 1 addition & 2 deletions aws_embedded_metrics/logger/metrics_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ async def flush(self) -> None:

# accept and reset the context
sink.accept(self.context)
self.context = self.context.create_copy_with_context() if not self.flush_preserve_dimensions \
else self.context.create_copy_with_context_with_dimensions()
self.context = self.context.create_copy_with_context(self.flush_preserve_dimensions)

def __configure_context_for_environment(self, env: Environment) -> None:
default_dimensions = {
Expand Down