@aws-lambda-powertools/metrics
Advanced tools
Comparing version
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MetricResolution = exports.MetricUnit = exports.Metrics = void 0; | ||
exports.Metrics = exports.MetricUnit = exports.MetricResolution = void 0; | ||
var constants_js_1 = require("./constants.js"); | ||
Object.defineProperty(exports, "MetricResolution", { enumerable: true, get: function () { return constants_js_1.MetricResolution; } }); | ||
Object.defineProperty(exports, "MetricUnit", { enumerable: true, get: function () { return constants_js_1.MetricUnit; } }); | ||
var Metrics_js_1 = require("./Metrics.js"); | ||
Object.defineProperty(exports, "Metrics", { enumerable: true, get: function () { return Metrics_js_1.Metrics; } }); | ||
var constants_js_1 = require("./constants.js"); | ||
Object.defineProperty(exports, "MetricUnit", { enumerable: true, get: function () { return constants_js_1.MetricUnit; } }); | ||
Object.defineProperty(exports, "MetricResolution", { enumerable: true, get: function () { return constants_js_1.MetricResolution; } }); |
@@ -0,3 +1,3 @@ | ||
export { MetricResolution, MetricUnit } from './constants.js'; | ||
export { Metrics } from './Metrics.js'; | ||
export { MetricUnit, MetricResolution } from './constants.js'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MetricResolution = exports.MetricUnit = exports.Metrics = void 0; | ||
exports.Metrics = exports.MetricUnit = exports.MetricResolution = void 0; | ||
var constants_js_1 = require("./constants.js"); | ||
Object.defineProperty(exports, "MetricResolution", { enumerable: true, get: function () { return constants_js_1.MetricResolution; } }); | ||
Object.defineProperty(exports, "MetricUnit", { enumerable: true, get: function () { return constants_js_1.MetricUnit; } }); | ||
var Metrics_js_1 = require("./Metrics.js"); | ||
Object.defineProperty(exports, "Metrics", { enumerable: true, get: function () { return Metrics_js_1.Metrics; } }); | ||
var constants_js_1 = require("./constants.js"); | ||
Object.defineProperty(exports, "MetricUnit", { enumerable: true, get: function () { return constants_js_1.MetricUnit; } }); | ||
Object.defineProperty(exports, "MetricResolution", { enumerable: true, get: function () { return constants_js_1.MetricResolution; } }); |
import { Utility } from '@aws-lambda-powertools/commons'; | ||
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types'; | ||
import type { Dimensions, EmfOutput, ExtraOptions, MetricResolution, MetricUnit, MetricsInterface, MetricsOptions } from './types/index.js'; | ||
import type { Dimensions, EmfOutput, ExtraOptions, MetricResolution, MetricsInterface, MetricsOptions, MetricUnit } from './types/index.js'; | ||
/** | ||
@@ -127,2 +127,7 @@ * The Metrics utility creates custom metrics asynchronously by logging metrics to standard output following {@link https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html | Amazon CloudWatch Embedded Metric Format (EMF)}. | ||
/** | ||
* Additional dimension sets for the current metrics context | ||
* @default [] | ||
*/ | ||
private dimensionSets; | ||
/** | ||
* Service for accessing environment variables | ||
@@ -129,0 +134,0 @@ */ |
@@ -130,2 +130,7 @@ "use strict"; | ||
/** | ||
* Additional dimension sets for the current metrics context | ||
* @default [] | ||
*/ | ||
dimensionSets = []; | ||
/** | ||
* Service for accessing environment variables | ||
@@ -222,5 +227,21 @@ */ | ||
addDimensions(dimensions) { | ||
for (const [name, value] of Object.entries(dimensions)) { | ||
this.addDimension(name, value); | ||
const newDimensionSet = {}; | ||
for (const [key, value] of Object.entries(dimensions)) { | ||
if (!value) { | ||
this.#logger.warn(`The dimension ${key} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`); | ||
continue; | ||
} | ||
if (Object.hasOwn(this.dimensions, key) || | ||
Object.hasOwn(this.defaultDimensions, key) || | ||
Object.hasOwn(newDimensionSet, key)) { | ||
this.#logger.warn(`Dimension "${key}" has already been added. The previous value will be overwritten.`); | ||
} | ||
newDimensionSet[key] = value; | ||
} | ||
const currentCount = this.getCurrentDimensionsCount(); | ||
const newSetCount = Object.keys(newDimensionSet).length; | ||
if (currentCount + newSetCount >= constants_js_1.MAX_DIMENSION_COUNT) { | ||
throw new RangeError(`The number of metric dimensions must be lower than ${constants_js_1.MAX_DIMENSION_COUNT}`); | ||
} | ||
this.dimensionSets.push(newDimensionSet); | ||
} | ||
@@ -394,2 +415,3 @@ /** | ||
this.dimensions = {}; | ||
this.dimensionSets = []; | ||
} | ||
@@ -602,15 +624,28 @@ /** | ||
}, {}); | ||
const dimensionNames = [ | ||
...new Set([ | ||
const dimensionNames = []; | ||
const allDimensionKeys = new Set([ | ||
...Object.keys(this.defaultDimensions), | ||
...Object.keys(this.dimensions), | ||
]); | ||
if (Object.keys(this.dimensions).length > 0) { | ||
dimensionNames.push([...allDimensionKeys]); | ||
} | ||
for (const dimensionSet of this.dimensionSets) { | ||
const dimensionSetKeys = new Set([ | ||
...Object.keys(this.defaultDimensions), | ||
...Object.keys(this.dimensions), | ||
]), | ||
]; | ||
...Object.keys(dimensionSet), | ||
]); | ||
dimensionNames.push([...dimensionSetKeys]); | ||
} | ||
if (dimensionNames.length === 0 && | ||
Object.keys(this.defaultDimensions).length > 0) { | ||
dimensionNames.push([...Object.keys(this.defaultDimensions)]); | ||
} | ||
return { | ||
_aws: { | ||
Timestamp: this.#timestamp ?? new Date().getTime(), | ||
Timestamp: this.#timestamp ?? Date.now(), | ||
CloudWatchMetrics: [ | ||
{ | ||
Namespace: this.namespace || constants_js_1.DEFAULT_NAMESPACE, | ||
Dimensions: [dimensionNames], | ||
Dimensions: dimensionNames, | ||
Metrics: metricDefinitions, | ||
@@ -622,2 +657,9 @@ }, | ||
...this.dimensions, | ||
// Merge all dimension sets efficiently by mutating the accumulator | ||
...this.dimensionSets.reduce((acc, dims) => { | ||
for (const [key, value] of Object.entries(dims)) { | ||
acc[key] = value; | ||
} | ||
return acc; | ||
}, {}), | ||
...metricValues, | ||
@@ -729,4 +771,6 @@ ...this.metadata, | ||
getCurrentDimensionsCount() { | ||
const dimensionSetsCount = this.dimensionSets.reduce((total, dimensionSet) => total + Object.keys(dimensionSet).length, 0); | ||
return (Object.keys(this.dimensions).length + | ||
Object.keys(this.defaultDimensions).length); | ||
Object.keys(this.defaultDimensions).length + | ||
dimensionSetsCount); | ||
} | ||
@@ -913,3 +957,3 @@ /** | ||
const timestampMs = isDate ? timestamp.getTime() : timestamp; | ||
const currentTime = new Date().getTime(); | ||
const currentTime = Date.now(); | ||
const minValidTimestamp = currentTime - constants_js_1.EMF_MAX_TIMESTAMP_PAST_AGE; | ||
@@ -916,0 +960,0 @@ const maxValidTimestamp = currentTime + constants_js_1.EMF_MAX_TIMESTAMP_FUTURE_AGE; |
@@ -1,3 +0,3 @@ | ||
export type { MetricsOptions, Dimensions, EmfOutput, ExtraOptions, StoredMetrics, StoredMetric, MetricDefinition, MetricResolution, MetricUnit, MetricsInterface, } from './Metrics.js'; | ||
export type { ConfigServiceInterface } from './ConfigServiceInterface.js'; | ||
export type { Dimensions, EmfOutput, ExtraOptions, MetricDefinition, MetricResolution, MetricsInterface, MetricsOptions, MetricUnit, StoredMetric, StoredMetrics, } from './Metrics.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -0,3 +1,3 @@ | ||
export { MetricResolution, MetricUnit } from './constants.js'; | ||
export { Metrics } from './Metrics.js'; | ||
export { MetricUnit, MetricResolution } from './constants.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -0,2 +1,2 @@ | ||
export { MetricResolution, MetricUnit } from './constants.js'; | ||
export { Metrics } from './Metrics.js'; | ||
export { MetricUnit, MetricResolution } from './constants.js'; |
import { Utility } from '@aws-lambda-powertools/commons'; | ||
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types'; | ||
import type { Dimensions, EmfOutput, ExtraOptions, MetricResolution, MetricUnit, MetricsInterface, MetricsOptions } from './types/index.js'; | ||
import type { Dimensions, EmfOutput, ExtraOptions, MetricResolution, MetricsInterface, MetricsOptions, MetricUnit } from './types/index.js'; | ||
/** | ||
@@ -127,2 +127,7 @@ * The Metrics utility creates custom metrics asynchronously by logging metrics to standard output following {@link https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html | Amazon CloudWatch Embedded Metric Format (EMF)}. | ||
/** | ||
* Additional dimension sets for the current metrics context | ||
* @default [] | ||
*/ | ||
private dimensionSets; | ||
/** | ||
* Service for accessing environment variables | ||
@@ -129,0 +134,0 @@ */ |
import { Console } from 'node:console'; | ||
import { Utility, isIntegerNumber } from '@aws-lambda-powertools/commons'; | ||
import { isIntegerNumber, Utility } from '@aws-lambda-powertools/commons'; | ||
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js'; | ||
import { COLD_START_METRIC, DEFAULT_NAMESPACE, EMF_MAX_TIMESTAMP_FUTURE_AGE, EMF_MAX_TIMESTAMP_PAST_AGE, MAX_DIMENSION_COUNT, MAX_METRICS_SIZE, MAX_METRIC_VALUES_SIZE, MetricResolution as MetricResolutions, MetricUnit as MetricUnits, } from './constants.js'; | ||
import { COLD_START_METRIC, DEFAULT_NAMESPACE, EMF_MAX_TIMESTAMP_FUTURE_AGE, EMF_MAX_TIMESTAMP_PAST_AGE, MAX_DIMENSION_COUNT, MAX_METRIC_VALUES_SIZE, MAX_METRICS_SIZE, MetricResolution as MetricResolutions, MetricUnit as MetricUnits, } from './constants.js'; | ||
/** | ||
@@ -127,2 +127,7 @@ * The Metrics utility creates custom metrics asynchronously by logging metrics to standard output following {@link https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html | Amazon CloudWatch Embedded Metric Format (EMF)}. | ||
/** | ||
* Additional dimension sets for the current metrics context | ||
* @default [] | ||
*/ | ||
dimensionSets = []; | ||
/** | ||
* Service for accessing environment variables | ||
@@ -219,5 +224,21 @@ */ | ||
addDimensions(dimensions) { | ||
for (const [name, value] of Object.entries(dimensions)) { | ||
this.addDimension(name, value); | ||
const newDimensionSet = {}; | ||
for (const [key, value] of Object.entries(dimensions)) { | ||
if (!value) { | ||
this.#logger.warn(`The dimension ${key} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`); | ||
continue; | ||
} | ||
if (Object.hasOwn(this.dimensions, key) || | ||
Object.hasOwn(this.defaultDimensions, key) || | ||
Object.hasOwn(newDimensionSet, key)) { | ||
this.#logger.warn(`Dimension "${key}" has already been added. The previous value will be overwritten.`); | ||
} | ||
newDimensionSet[key] = value; | ||
} | ||
const currentCount = this.getCurrentDimensionsCount(); | ||
const newSetCount = Object.keys(newDimensionSet).length; | ||
if (currentCount + newSetCount >= MAX_DIMENSION_COUNT) { | ||
throw new RangeError(`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`); | ||
} | ||
this.dimensionSets.push(newDimensionSet); | ||
} | ||
@@ -391,2 +412,3 @@ /** | ||
this.dimensions = {}; | ||
this.dimensionSets = []; | ||
} | ||
@@ -599,15 +621,28 @@ /** | ||
}, {}); | ||
const dimensionNames = [ | ||
...new Set([ | ||
const dimensionNames = []; | ||
const allDimensionKeys = new Set([ | ||
...Object.keys(this.defaultDimensions), | ||
...Object.keys(this.dimensions), | ||
]); | ||
if (Object.keys(this.dimensions).length > 0) { | ||
dimensionNames.push([...allDimensionKeys]); | ||
} | ||
for (const dimensionSet of this.dimensionSets) { | ||
const dimensionSetKeys = new Set([ | ||
...Object.keys(this.defaultDimensions), | ||
...Object.keys(this.dimensions), | ||
]), | ||
]; | ||
...Object.keys(dimensionSet), | ||
]); | ||
dimensionNames.push([...dimensionSetKeys]); | ||
} | ||
if (dimensionNames.length === 0 && | ||
Object.keys(this.defaultDimensions).length > 0) { | ||
dimensionNames.push([...Object.keys(this.defaultDimensions)]); | ||
} | ||
return { | ||
_aws: { | ||
Timestamp: this.#timestamp ?? new Date().getTime(), | ||
Timestamp: this.#timestamp ?? Date.now(), | ||
CloudWatchMetrics: [ | ||
{ | ||
Namespace: this.namespace || DEFAULT_NAMESPACE, | ||
Dimensions: [dimensionNames], | ||
Dimensions: dimensionNames, | ||
Metrics: metricDefinitions, | ||
@@ -619,2 +654,9 @@ }, | ||
...this.dimensions, | ||
// Merge all dimension sets efficiently by mutating the accumulator | ||
...this.dimensionSets.reduce((acc, dims) => { | ||
for (const [key, value] of Object.entries(dims)) { | ||
acc[key] = value; | ||
} | ||
return acc; | ||
}, {}), | ||
...metricValues, | ||
@@ -726,4 +768,6 @@ ...this.metadata, | ||
getCurrentDimensionsCount() { | ||
const dimensionSetsCount = this.dimensionSets.reduce((total, dimensionSet) => total + Object.keys(dimensionSet).length, 0); | ||
return (Object.keys(this.dimensions).length + | ||
Object.keys(this.defaultDimensions).length); | ||
Object.keys(this.defaultDimensions).length + | ||
dimensionSetsCount); | ||
} | ||
@@ -910,3 +954,3 @@ /** | ||
const timestampMs = isDate ? timestamp.getTime() : timestamp; | ||
const currentTime = new Date().getTime(); | ||
const currentTime = Date.now(); | ||
const minValidTimestamp = currentTime - EMF_MAX_TIMESTAMP_PAST_AGE; | ||
@@ -913,0 +957,0 @@ const maxValidTimestamp = currentTime + EMF_MAX_TIMESTAMP_FUTURE_AGE; |
@@ -1,3 +0,3 @@ | ||
export type { MetricsOptions, Dimensions, EmfOutput, ExtraOptions, StoredMetrics, StoredMetric, MetricDefinition, MetricResolution, MetricUnit, MetricsInterface, } from './Metrics.js'; | ||
export type { ConfigServiceInterface } from './ConfigServiceInterface.js'; | ||
export type { Dimensions, EmfOutput, ExtraOptions, MetricDefinition, MetricResolution, MetricsInterface, MetricsOptions, MetricUnit, StoredMetric, StoredMetrics, } from './Metrics.js'; | ||
//# sourceMappingURL=index.d.ts.map |
{ | ||
"name": "@aws-lambda-powertools/metrics", | ||
"version": "2.23.0", | ||
"version": "2.24.0", | ||
"description": "The metrics package for the Powertools for AWS Lambda (TypeScript) library", | ||
@@ -27,3 +27,3 @@ "author": { | ||
"dependencies": { | ||
"@aws-lambda-powertools/commons": "^2.23.0" | ||
"@aws-lambda-powertools/commons": "^2.24.0" | ||
}, | ||
@@ -30,0 +30,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
234323
2.02%5014
1.99%