Skip to content

single_metric() to inherit default dimensions #1859

Closed
@prudnikov

Description

@prudnikov

Use case

At the root level I define default metric which is environment name in my case.

metrics = Metrics()
metrics.set_default_dimensions(Environment=ENVIRONMENT)

This is all fine and Environment gets propagated to all metrics. However, when single_metric() is used this default dimension is ignored. This is what I do to overcome that.

metrics = Metrics()

with single_metric(
    name="Metric1", unit=MetricUnit.Milliseconds, value=elapsed_ms
) as this_metric:
    this_metric.add_dimension(name="TableName", value=self.table_name)


    for name, value in metrics.default_dimensions.items():
        this_metric.add_dimension(name=name, value=value)

or it should be possible with this_metric.default_dimensions = metrics.default_dimensions, I did not tried this.

This way all the default dimensions are propagated with a single metric.

Solution/User Experience

When creating a SingleMetric with single_metric context manager default dimensions should be copied to a SingleMetric instance.

I can imagine the situation when you don't want to propagate default dimensions to the SingleMetric.

To keep existing usage untouched I suppose the default behavior should be to not propagate default dimensions. So the solution can be the additional keyword argument inherit_default_dimensions: bool = False

with single_metric("Name", MetricUnit.Count, 1, inherit_default_dimensions=True) as this_metric:
    this_metric.add_dimension(name="Dim1", value="Val")

Alternative solutions

No response

Acknowledgment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions