Skip to content

Putting metrics fails when POWERTOOLS_METRICS_NAMESPACE is not set #1500

Closed

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

  1. scottgerring commented on Nov 7, 2023

    @scottgerring
    Contributor

    Hey @roamingthings , thanks for the report! The requirement for the variable is documented, but it does seem a bit odd to explicitly 1/ check if the default hasn't been overriden, and then 2/ override it with an empty string. Inverting the whole thing as you suggest so we override if we have the env var, or use the default otherwise, seems more sensible, and wouldn't be a breaking change.

    What do you think @jeromevdl - ?

  2. jeromevdl commented on Nov 7, 2023

    @jeromevdl
    Contributor

    First, I didn't really understand this code:

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

    Looks to me that we should do the contrary: if the env var is empty, set a default, as you suggest.

    But I'm not sure how the context namespace is actually set. I've found this which makes me think that "aws-embedded-metrics" is the default. So if it is the default, we try to replace it with the env var, otherwise, it means the user already changed the default somehow (how?) and we should use that.

    What is missing here is the check on SystemWrapper.getenv("POWERTOOLS_METRICS_NAMESPACE". If null or empty, we should come back to the default.

  3. moved this from Triage to Backlog in Powertools for AWS Lambda (Java)on Nov 8, 2023
  4. added a commit that references this issue on Nov 8, 2023
    c81e956
  5. moved this from Backlog to Coming soon in Powertools for AWS Lambda (Java)on Nov 8, 2023
  6. moved this from Coming soon to Working on it in Powertools for AWS Lambda (Java)on Nov 8, 2023
  7. moved this from Working on it to Coming soon in Powertools for AWS Lambda (Java)on Nov 8, 2023
  8. self-assigned this
    on Nov 8, 2023
  9. github-actions commented on Nov 16, 2023

    @github-actions
    Contributor

    This is now released under 1.18.0 version!

  10. added a commit that references this issue on Nov 29, 2023
    e6e2eb0
  11. added a commit that references this issue on Dec 5, 2023
    03024be
  12. added a commit that references this issue on Feb 22, 2024
    1813e5f
  13. added a commit that references this issue on Jul 30, 2024
    0f3e896
  14. moved this from Coming soon to Shipped in Powertools for AWS Lambda (Java)on Sep 2, 2024
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