Skip to content

putDimension with same key is adding always new elements #91

Closed
@miguelcss

Description

@miguelcss

Hello,

I'm using the lib for a Java service on EC2, not serverless, so I create a single instance of the MetricsLogger that is created as @Bean in a Spring context, and then I have have it in the class that log metrics as:

@Component
public class MetricsHelper {

    private MetricsLogger metricsLogger;
    
    public void publishMetric(String operation) {
        metrics.putDimensions(DimensionSet.of("API", operation));
        metricsLogger.putMetric("Request", 1, Unit.COUNT);
        metricsLogger.flush();
    }

Then I use this helper as such

        metricsHelper.publishMetric("SomeOperation");

However, when checking the logGroup, as the logged metrics increase, I see an ever increasing list of "Dimensions", even if the value for the metric is just 1, the event includes a huge Dimensions list

A sample record:

{
    "_aws": {
        "Timestamp": 1637780784996,
        "CloudWatchMetrics": [
            {
                "Namespace": "someNameSpace",
                "Metrics": [
                    {
                        "Name": "Time",
                        "Unit": "Milliseconds"
                    }
                ],
                "Dimensions": [
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ],
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ],
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ],
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ],
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ],
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ],
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ],
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ],
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ],
                    [
                        "LogGroup",
                        "ServiceName",
                        "ServiceType",
                        "Api"
                    ]
                ]
            }
        ],
        "LogGroupName": "SomeService-metrics"
    },
    "Time": 327,
    "Api": "someOperation",
    "LogGroup": "SomeService-metrics",
    "ServiceName": "SomeService",
    "ServiceType": "someType"
}

Digging a bit in code, whenever calling putDimension, indeed a new element is added to a java List<DimensionSet>.
Code is here in MetricDirective.

Should this really be a list? if adding a new dimension that is already used, we could just update the value, this feels like it could be a Map and not a List to avoid this growing record size.

Or, is this the right usage? maybe when doing flush, this list should be cleaned? From the README.md I feel like the usage is correct, and user should not be worried about dimension internal structure (growing, or deleting/removing elements).

So maybe use a map would solve it, or integrate a clean in the flush

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions