Skip to content

Putting metrics fails when POWERTOOLS_METRICS_NAMESPACE is not set #1500

Description

@roamingthings

Putting metrics will fail with an software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException when environment POWERTOOLS_METRICS_NAMESPACE is not set.

What were you trying to accomplish?

After updating cloudwatchlogs-aws-embedded-metrics to 4.1.1 I started to get InvalidNamespaceException when I try to put a metric for example using

MetricsUtils.withSingleMetric("MyMetric, 1.0, Unit.COUNT, metric ->
...
});

Expected Behavior

Possible behaviors I would expect are:

  • metric is put with a default namespace
  • no exception is thrown (maybe some internal logging). I would not expect a call to a metrics helper to fail my application with an unchecked exception

Current Behavior

software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException is thrown which is an unchecked expception.

The reason is that MetricsUtils sets the namespace to an empty string when the environment variable is not set:

    private static String defaultNameSpace() {
        MetricsContext context = MetricsLoggerHelper.metricsContext();
        return "aws-embedded-metrics".equals(context.getNamespace()) ?
                SystemWrapper.getenv("POWERTOOLS_METRICS_NAMESPACE") : context.getNamespace();
    }

Possible Solution

  • Keep the original default namespace (aws-embedded-metrics) if POWERTOOLS_METRICS_NAMESPACE is not set
  • Choose another "default" namespace if POWERTOOLS_METRICS_NAMESPACE is not set

Steps to Reproduce (for bugs)

  1. Call MetricsUtils.withSingleMetric() whithout providing a namespace in POWERTOOLS_METRICS_NAMESPACE

Environment

  • Powertools for AWS Lambda (Java) version used: 1.17.0
  • Packaging format (Layers, Maven/Gradle): Gradle
  • AWS Lambda function runtime: Java 17 (actually discovered in a unit test)
  • Debugging logs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions