Skip to content

improv(metrics): consistent duplicate dimension handling #4235

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

uttam282005
Copy link
Contributor

Summary

This PR restores and improves the warning mechanism for overwritten default dimensions in the Metrics utility:

Changes

  • Restores the warning logic that was unintentionally removed in a prior revert.
  • Fixes a bug where calling captureColdStartMetric() would emit duplicate warnings for the service dimension.
  • Refines warning behavior to only trigger when the overwritten service dimension is different from the internal default (i.e., truly user-defined).

Additional Fix

Previously, passing defaultDimensions with a service key in the constructor triggered unnecessary warnings, beacause service was set internally as a default dimension via setService():

new Metrics({
  defaultDimensions: {
    service: 'auth-service', // <-- warning was wrongly emitted 
  },
});

Now, this behavior is handled correctly. No warning is emitted if the service in defaultDimensions is the same as the internal default service name.


Final Fix Summary

  • Avoids redundant call to setDefaultDimensions() inside captureColdStartMetric(), which previously caused a second warning.
  • Prevents unnecessary or duplicate warnings when instantiating Metrics with service value in defaultDimensions.

Issue number: #4134


Note

Instantiating the object like this will still emit a warning as user is trying to overwrite previously passed serviceName, even if they are equal. This behaviour is consistent with other methods like addDimensions and addDimension.

const metrics = new Metrics({
  serviceName: 'some-service',
  defaultDimensions: {
    service: 'some-service', // ⚠️ Warning: service already added
  },
});

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@boring-cyborg boring-cyborg bot added metrics This item relates to the Metrics Utility tests PRs that add or change tests labels Jul 30, 2025
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Jul 30, 2025
@uttam282005 uttam282005 changed the title Fix/service dimension cleanup fix(metrics): suppress duplicate warnings on service dimension and improve defaultDimensions handling Jul 30, 2025
@dreamorosi dreamorosi changed the title fix(metrics): suppress duplicate warnings on service dimension and improve defaultDimensions handling improv(metrics): consistent duplicate dimension handling Jul 30, 2025
if (Object.hasOwn(this.defaultDimensions, key)) {
const currentValue = this.defaultDimensions[key];
const suppressOverwriteWarning =
key === 'service' && currentValue === this.defaultServiceName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uttam282005 I tried to run the e2e tests against the PR, and one of the tests is failing. It seems like there's still one warning that gets logged if we use captureColdStartMetric.
I'm not sure, but I think it has to do with this.defaultServiceName. We're doing this in setService to get the service, do we need something similar?

service ||
this.getCustomConfigService()?.getServiceName() ||
this.#envConfig.serviceName ||
this.defaultServiceName;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the warning occurs because the service dimension is initially set via setService(), this value can come from a user-provided serviceName, an environment variable, or a custom config service. Later, when setDefaultDimensions() is called (and includes a service key), we get a warning indicating the service dimension is being overwritten.

In many cases, this warning is correct, it's a user attempting to override a previously set user-defined service value, which we want to surface. However, the problem arises when setDefaultDimensions() is called internally (e.g., within captureColdStartMetric() or during initialization like singleMetric() ). In these cases, the overwrite is intentional and should not trigger a warning.

To address this, the cleanest solution I can think of is introducing a private boolean flag that marks such internal calls. This flag would let us suppress the warning when setDefaultDimensions() is invoked by internal logic rather than user input, preserving the intent of the warning without polluting logs in expected flows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause is the call to single metric which is emitting the same behaviour that I have written in the note section of the PR.

Copy link

@@ -1058,8 +1090,6 @@ class Metrics extends Utility implements MetricsInterface {
functionName,
} = options;

this.setEnvConfig();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were these two removed and extracted in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setEnvConfig() and setConsole() calls from setOptions() were moved to the constructor to ensure logger is initialized before any method (eg. setDefaultDimensions) uses it.

@uttam282005
Copy link
Contributor Author

@dreamorosi @sdangol I’d like to get your thoughts on how best to solve this.

@sdangol
Copy link
Contributor

sdangol commented Jul 31, 2025

@uttam282005 I think having the flag to maintain the internal calls would probably add some complexity to this. Let me think this through for some more time, discuss with the team and get back to you

@uttam282005
Copy link
Contributor Author

private static supressWarnings = false;

private static supressSetDefaultDimensionsWarnings<T>(fn: () => T): T {
  Metrics.supressWarnings = true;
  try {
    return fn();
  } finally {
    Metrics.supressWarnings = false;
  }
}

How is this approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metrics This item relates to the Metrics Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintenance: Add warning for overwriting dimensions in setDefaultDimensions method
3 participants