-
Notifications
You must be signed in to change notification settings - Fork 154
Docs: clarify addMetadata()
call order in single metrics docs
#3188
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
Comments
Hi @bml1g12, thanks for opening the issue. As far as I can tell (haven't tested yet), this happens only for single metrics, but not for regular metrics. The reason why, is that single metrics are published as soon as you add them, so by the time you add the metadata, that metric has already been emitted. You can see the implementation for With this in mind, if you want to append metadata and use single metric, you'll have to do it before adding the metric. The example you linked is for regular metrics and not single metrics |
Ok, I have just tested what I shared above and I can confirm that this is the case. import { expect, it, describe, vi } from 'vitest';
import { Metrics, MetricUnit } from '@aws-lambda-powertools/metrics';
vi.hoisted(() => {
process.env.POWERTOOLS_DEV = 'true';
});
describe('metrics', () => {
it('has metadata when using regular metrics', () => {
// Prepare
const metrics = new Metrics();
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
// Act
metrics.addMetric('myMetric', MetricUnit.Count, 1);
metrics.addMetadata('foo', 'bar');
metrics.publishStoredMetrics();
// Assess
expect(JSON.parse(logSpy.mock.calls[0][0])).toStrictEqual(
expect.objectContaining({
_aws: {
CloudWatchMetrics: [
{
Namespace: 'default_namespace',
Dimensions: [['service']],
Metrics: [{ Name: 'myMetric', Unit: 'Count' }],
},
],
Timestamp: expect.any(Number),
},
foo: 'bar',
myMetric: 1,
service: 'service_undefined',
})
);
});
it('does not have metadata when using single metrics', () => {
// Prepare
const metrics = new Metrics({ singleMetric: true });
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
// Act
metrics.addMetric('myMetric', MetricUnit.Count, 1);
metrics.addMetadata('foo', 'bar');
// Assess
expect(JSON.parse(logSpy.mock.calls[0][0])).toStrictEqual({
_aws: {
CloudWatchMetrics: [
{
Namespace: 'default_namespace',
Dimensions: [['service']],
Metrics: [{ Name: 'myMetric', Unit: 'Count' }],
},
],
Timestamp: expect.any(Number),
},
myMetric: 1,
service: 'service_undefined',
});
});
}); With this in mind, I don't think there's a way for us to make it work with single metrics since, as explained, by the time you call I am going however to improve the code sample for the single metric section to include an |
addMetadata()
call order sensitiveaddMetadata()
call order in single metrics docs
That makes perfect sense, thanks for clarifying in the docs I wasn't aware single metrics emit immediately (I thought they also would flush at end of runtime), that might be nice to clarify in docs too (apologies if it's there and I missed it) |
I have opened a PR to fix the issue and also spell out the fact that these metrics are emitted as soon as they are added, which was not explicitly stated in the previous version of that section (it was elsewhere). |
This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so. |
This is now released under v2.10.0 version! |
Expected Behavior
Works as expected however, doing instead the other way around:
Seems to result in no metadata being appended to the EMF cloudwatch log.
This is confusing because the documentation example shows addMetadata() being called after addMetadata https://docs.powertools.aws.dev/lambda/typescript/latest/core/metrics/#adding-metadata
Current Behavior
Does not add metadata to the cloudwatch log
Code snippet
Steps to Reproduce
Possible Solution
I'd suggest either updating docs to specify explicitly the order required, or making it work in either order
Powertools for AWS Lambda (TypeScript) version
2.8.0
AWS Lambda function runtime
20.x
Packaging format used
npm
Execution logs
No response
The text was updated successfully, but these errors were encountered: