Skip to content

refactor(metrics): unit tests for Metrics are written to follow similar convention as Logger/Tracer #1414

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

Merged
merged 98 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
6bd7fb1
test: cleanup unit tests
arnabrahman Mar 26, 2023
37adcab
test: store metrics
arnabrahman Mar 26, 2023
655b820
test: create multiple metrics
arnabrahman Mar 26, 2023
ebe50f9
test: addMetric default resolution
arnabrahman Mar 26, 2023
1d62a18
test: addMetric will group values for same metric name
arnabrahman Mar 26, 2023
b44a983
test: addMetric throws error while adding same metric with different …
arnabrahman Mar 26, 2023
4986d6d
test: addMetric will publish metrics if stored metrics count has reac…
arnabrahman Mar 26, 2023
3ec7e74
test: addMetric will not publish if stored metrics count has not reac…
arnabrahman Mar 26, 2023
1e3845a
test: addDimension storing dimension
arnabrahman Mar 26, 2023
66b9a44
test: addDimension giving error if number of dimensions exceeds the m…
arnabrahman Mar 29, 2023
61b7da9
test: addDimensions should add multiple dimensions
arnabrahman Mar 29, 2023
78705ea
test: addDimensions should update existing dimension value if added a…
arnabrahman Mar 29, 2023
0f08806
test: addDimensions should throw error if maximum dimension count cro…
arnabrahman Mar 29, 2023
649dc5b
test: addMetadata should add metadata
arnabrahman Mar 29, 2023
99c447a
test: clearDimensions should clear dimensions
arnabrahman Mar 29, 2023
6541fba
test: clearMetadata should clear all metadata
arnabrahman Mar 29, 2023
d789b4e
test: clearMetrics should clear all stored metrics
arnabrahman Mar 29, 2023
f01b6ae
test: setDefaultDimensions should set default dimensions object
arnabrahman Mar 29, 2023
07dd43b
test: setDefaultDimensions should throw error if default dimensions r…
arnabrahman Mar 30, 2023
8b4c92b
test: clearDefaultDimensions should clear all default dimensions
arnabrahman Mar 30, 2023
3e63b19
test: singleMetric should return a single metric object
arnabrahman Mar 30, 2023
0d06a8d
test: throwOnEmptyMetrics will set the throwOnEmptyMetrics flag to true
arnabrahman Apr 1, 2023
30fb771
test: setFunctionName should set the function name
arnabrahman Apr 1, 2023
ce11bae
test: logMetrics should log metrics
arnabrahman Apr 2, 2023
4bde67d
test: logMetrics should capture cold start metrics
arnabrahman Apr 2, 2023
32bce7a
test: logMetrics should throw error if no metrics are added and throw…
arnabrahman Apr 2, 2023
20a9e53
test: logMetrics should set default dimensions if passed in the options
arnabrahman Apr 2, 2023
4081793
test: logMetrics should throw error if lambda handler throws any error
arnabrahman Apr 2, 2023
12a3a52
test: serializeMetrics should print warning, if no namespace provided…
arnabrahman Apr 2, 2023
94c0c1a
feat: metrics helper
arnabrahman Apr 3, 2023
94c8a39
test: createMetrics should return appropriate values for no construct…
arnabrahman Apr 3, 2023
1a9ed4c
test: createMetrics should return appropriate instance when construct…
arnabrahman Apr 3, 2023
4d5f987
Merge branch 'main' into 163-metrics-unit-tests
arnabrahman Apr 6, 2023
c1175b7
test: serializeMetricscreateMetrics should return right object compli…
arnabrahman Apr 7, 2023
eed92bc
Merge branch 'main' into 163-metrics-unit-tests
arnabrahman Apr 7, 2023
bed96a9
test: serializeMetricscreateMetrics should log service dimensions cor…
arnabrahman Apr 8, 2023
427637d
test: serializeMetricscreateMetrics should log other dimensions corre…
arnabrahman Apr 8, 2023
61a8c9e
refactor: clearDimensions should not clear default dimensions
arnabrahman Apr 8, 2023
91e5214
test: serializeMetrics should log metadata correctly
arnabrahman Apr 8, 2023
2c83ca9
test: serializeMetrics should throw error on empty metrics when throw…
arnabrahman Apr 8, 2023
83c5972
test: serializeMetrics should log namespaces properly
arnabrahman Apr 8, 2023
13f552f
test: serializeMetrics should log metric values properly
arnabrahman Apr 8, 2023
a870474
test: serializeMetrics should log properly
arnabrahman Apr 8, 2023
ca7a04b
test: serializeMetrics should show warning if no metrics are added
arnabrahman Apr 8, 2023
1a25d5c
test: serializeMetrics should should call serializeMetrics && log the…
arnabrahman Apr 8, 2023
01596d3
test: serializeMetrics should call clearMetrics function
arnabrahman Apr 9, 2023
8b2abee
test: serializeMetrics should call clearDimensions function
arnabrahman Apr 9, 2023
08af29a
test: serializeMetrics should call clearMetadata function
arnabrahman Apr 9, 2023
66012a5
test: serializeMetrics should call addMetric with correct parameters
arnabrahman Apr 9, 2023
d4f7265
test: serializeMetrics should call setDefaultDimensions with correct …
arnabrahman Apr 9, 2023
638ccf4
test: serializeMetrics should call addDimension depending on function…
arnabrahman Apr 9, 2023
28f9388
test: serializeMetrics should not call any function, if there is no c…
arnabrahman Apr 9, 2023
8d91555
refactor: extract reusable constants to a separate file
arnabrahman Apr 10, 2023
80cf6d3
refactor: import default namespace from constants
arnabrahman Apr 10, 2023
67c9cb8
refactor: use MAX_METRICS_SIZE from constants
arnabrahman Apr 10, 2023
327011a
refactor: use MAX_DIMENSION_COUNT from constants
arnabrahman Apr 10, 2023
f4553cc
refactor: use MAX_DIMENSION_COUNT in max dimension allowed tests
arnabrahman Apr 10, 2023
aea71c8
refactor: extract cold start metric in constants
arnabrahman Apr 10, 2023
46f60d0
refactor: use createMetrics helper function to create Metrics instance
arnabrahman Apr 10, 2023
151f119
refactor: set namespace for all the Metrics instance
arnabrahman Apr 10, 2023
dd1f790
style: formatting in test steps
arnabrahman Apr 10, 2023
77923b8
test: cleanup POWERTOOLS_METRICS_NAMESPACE env after test completion …
arnabrahman Apr 10, 2023
eedbc0f
test: error messages for various errors
arnabrahman Apr 10, 2023
12d12a3
docs: decorator function comments
arnabrahman Apr 13, 2023
f439dc1
refactor: format test metric name
arnabrahman Apr 15, 2023
efa136e
style: rearrange tests by name
arnabrahman Apr 16, 2023
995235c
fix: default namespace test for serializeMetrics function
arnabrahman Apr 16, 2023
8f7302b
fix: remove redundant deletion of process env
arnabrahman Apr 16, 2023
62ad51a
test: addDimension should update existing dimension value if same dim…
arnabrahman Apr 16, 2023
8dd3ed9
style: rephrase test description for addDimensions
arnabrahman Apr 16, 2023
940c585
style: rephrase test description for addMetadata
arnabrahman Apr 16, 2023
c376a44
style: rephrase test description for addMetric
arnabrahman Apr 16, 2023
3c6ae18
test: addMetric should publish metrics on every call if singleMetric …
arnabrahman Apr 16, 2023
a2e4065
style: rephrase test description for captureColdStartMetric
arnabrahman Apr 16, 2023
5de965f
test: clearDefaultDimensions should only clear default dimensions
arnabrahman Apr 16, 2023
ad81018
style: rephrase test description of clearDimensions
arnabrahman Apr 16, 2023
880fcc2
style: rephrase test description of clearMetrics
arnabrahman Apr 16, 2023
d68e55f
refactor: common functionality inside beforeEach for logMetrics test
arnabrahman Apr 16, 2023
c749956
refactor: publish metrics test for logMetrics
arnabrahman Apr 17, 2023
8148b77
refactor: capture cold start test for logMetrics
arnabrahman Apr 17, 2023
5aa15db
refactor: call throwOnEmptyMetrics test for logMetrics
arnabrahman Apr 17, 2023
dbd8a47
refactor: set default dimensions test for logMetrics
arnabrahman Apr 17, 2023
35b1727
refactor: separate decorator lambda function generation for logMetric…
arnabrahman Apr 17, 2023
23c474b
refactor: publishStoredMetrics tests
arnabrahman Apr 18, 2023
562f8d5
improv: test return values for serializeMetrics.
arnabrahman Apr 18, 2023
9ea8d04
refactor: tests for setDefaultDimensions
arnabrahman Apr 18, 2023
4026512
Merge branch 'main' into 163-metrics-unit-tests
arnabrahman Apr 20, 2023
a402e15
style: spacing in Metrics class
arnabrahman Apr 20, 2023
80e4c3b
refactor: function export for metricsUtils
arnabrahman Apr 20, 2023
e71c1fe
refactor: LooseObject interface in metrics tests
arnabrahman Apr 20, 2023
4a9dbc3
refactor: range error tests for max dimension count
arnabrahman Apr 20, 2023
3ef1a7b
refactor: test range error for max dimension in addDimension
arnabrahman Apr 20, 2023
73a69cb
refactor: range error tests for max metric size
arnabrahman Apr 20, 2023
ffd14d2
refactor: test namespace value from constant
arnabrahman Apr 20, 2023
e6d11e2
style: redundant curly braces
arnabrahman Apr 20, 2023
af2a6b8
refactor: replace createMetrics helper with new Metrics(...).
arnabrahman Apr 21, 2023
a063546
test: constructor method of Metrics class.
arnabrahman Apr 21, 2023
4037c3c
refactor: remove createMetrics helper function related codes
arnabrahman Apr 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Callback, Context, Handler } from 'aws-lambda';
import { Utility } from '@aws-lambda-powertools/commons';
import { MetricsInterface } from '.';
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
import { MAX_DIMENSION_COUNT, MAX_METRICS_SIZE, DEFAULT_NAMESPACE, COLD_START_METRIC } from './constants';
import {
MetricsOptions,
Dimensions,
Expand All @@ -16,10 +17,6 @@ import {
StoredMetric,
} from './types';

const MAX_METRICS_SIZE = 100;
const MAX_DIMENSION_COUNT = 29;
const DEFAULT_NAMESPACE = 'default_namespace';

/**
* ## Intro
* Metrics creates custom metrics asynchronously by logging metrics to standard output following Amazon CloudWatch Embedded Metric Format (EMF).
Expand Down Expand Up @@ -228,7 +225,7 @@ class Metrics extends Utility implements MetricsInterface {
if (this.functionName != null) {
singleMetric.addDimension('function_name', this.functionName);
}
singleMetric.addMetric('ColdStart', MetricUnits.Count, 1);
singleMetric.addMetric(COLD_START_METRIC, MetricUnits.Count, 1);
}

public clearDefaultDimensions(): void {
Expand Down
4 changes: 4 additions & 0 deletions packages/metrics/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const COLD_START_METRIC = 'ColdStart';
export const DEFAULT_NAMESPACE = 'default_namespace';
export const MAX_METRICS_SIZE = 100;
export const MAX_DIMENSION_COUNT = 29;
29 changes: 28 additions & 1 deletion packages/metrics/tests/helpers/metricsUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CloudWatch } from 'aws-sdk';
import promiseRetry from 'promise-retry';
import { Metrics } from '../../src';
import { ExtraOptions, MetricUnits } from '../../src/types';
import { Context, Handler } from 'aws-lambda';
import { LambdaInterface } from '@aws-lambda-powertools/commons';

const getMetrics = async (cloudWatchClient: CloudWatch, namespace: string, metric: string, expectedMetrics: number): Promise<CloudWatch.ListMetricsOutput> => {
const retryOptions = { retries: 20, minTimeout: 5_000, maxTimeout: 10_000, factor: 1.25 };
Expand All @@ -21,4 +25,27 @@ const getMetrics = async (cloudWatchClient: CloudWatch, namespace: string, metri
}, retryOptions);
};

export { getMetrics };
const setupDecoratorLambdaHandler = (metrics: Metrics, options: ExtraOptions = {}): Handler => {

class LambdaFunction implements LambdaInterface {
@metrics.logMetrics(options)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public async handler<TEvent>(_event: TEvent, _context: Context): Promise<string> {
metrics.addMetric('decorator-lambda-test-metric', MetricUnits.Count, 1);

return 'Lambda invoked!';
}
}

const handlerClass = new LambdaFunction();
const handler = handlerClass.handler.bind(handlerClass);

return handler;
};

export {
getMetrics,
setupDecoratorLambdaHandler
};

Loading