Skip to content

Commit 79e5aae

Browse files
committed
chore(metrics): lint changes
1 parent 8fbe2dd commit 79e5aae

File tree

5 files changed

+72
-70
lines changed

5 files changed

+72
-70
lines changed

packages/metrics/src/Metrics.ts

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,13 @@ class Metrics implements MetricsInterface {
149149
if (this.isSingleMetric) this.purgeStoredMetrics();
150150
}
151151

152-
public clearDefaultDimensions(): void {
153-
this.defaultDimensions = {};
154-
}
155-
156-
public clearDimensions(): void {
157-
this.dimensions = {};
158-
}
159-
160-
public clearMetadata(): void {
161-
this.metadata = {};
162-
}
163-
164-
public clearMetrics(): void {
165-
this.storedMetrics = {};
166-
}
167-
168152
/**
169-
* Throw an Error if the metrics buffer is empty.
153+
* Create a singleMetric to capture cold start.
154+
* If it's a cold start invocation, this feature will:
155+
* * Create a separate EMF blob solely containing a metric named ColdStart
156+
* * Add function_name and service dimensions
157+
*
158+
* This has the advantage of keeping cold start metric separate from your application metrics, where you might have unrelated dimensions.
170159
*
171160
* @example
172161
*
@@ -177,13 +166,38 @@ class Metrics implements MetricsInterface {
177166
* const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
178167
*
179168
* export const handler = async (event: any, context: Context) => {
180-
* metrics.raiseOnEmptyMetrics();
181-
* metrics.purgeStoredMetrics(); // will throw since no metrics added.
169+
* metrics.captureColdStartMetric();
182170
* }
183171
* ```
184172
*/
185-
public raiseOnEmptyMetrics(): void {
186-
this.shouldRaiseOnEmptyMetrics = true;
173+
public captureColdStartMetric(): void {
174+
if (!this.isColdStart) return;
175+
this.isColdStart = false;
176+
const singleMetric = this.singleMetric();
177+
178+
if (this.dimensions.service) {
179+
singleMetric.addDimension('service', this.dimensions.service);
180+
}
181+
if (this.functionName != null) {
182+
singleMetric.addDimension('function_name', this.functionName);
183+
}
184+
singleMetric.addMetric('ColdStart', MetricUnits.Count, 1);
185+
}
186+
187+
public clearDefaultDimensions(): void {
188+
this.defaultDimensions = {};
189+
}
190+
191+
public clearDimensions(): void {
192+
this.dimensions = {};
193+
}
194+
195+
public clearMetadata(): void {
196+
this.metadata = {};
197+
}
198+
199+
public clearMetrics(): void {
200+
this.storedMetrics = {};
187201
}
188202

189203
/**
@@ -228,7 +242,7 @@ class Metrics implements MetricsInterface {
228242
if (captureColdStartMetric) this.captureColdStartMetric();
229243
try {
230244
const result = originalMethod?.apply(this, [ event, context, callback ]);
231-
245+
232246
return result;
233247
} finally {
234248
this.purgeStoredMetrics();
@@ -259,6 +273,27 @@ class Metrics implements MetricsInterface {
259273
this.storedMetrics = {};
260274
}
261275

276+
/**
277+
* Throw an Error if the metrics buffer is empty.
278+
*
279+
* @example
280+
*
281+
* ```typescript
282+
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
283+
* import { Context } from 'aws-lambda';
284+
*
285+
* const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
286+
*
287+
* export const handler = async (event: any, context: Context) => {
288+
* metrics.raiseOnEmptyMetrics();
289+
* metrics.purgeStoredMetrics(); // will throw since no metrics added.
290+
* }
291+
* ```
292+
*/
293+
public raiseOnEmptyMetrics(): void {
294+
this.shouldRaiseOnEmptyMetrics = true;
295+
}
296+
262297
/**
263298
* Function to create the right object compliant with Cloudwatch EMF (Event Metric Format).
264299
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html for more details
@@ -315,6 +350,10 @@ class Metrics implements MetricsInterface {
315350
this.defaultDimensions = targetDimensions;
316351
}
317352

353+
public setFunctionName(value: string): void {
354+
this.functionName = value;
355+
}
356+
318357
/**
319358
* CloudWatch EMF uses the same dimensions across all your metrics. Use singleMetric if you have a metric that should have different dimensions.
320359
*
@@ -339,45 +378,6 @@ class Metrics implements MetricsInterface {
339378
});
340379
}
341380

342-
/**
343-
* Create a singleMetric to capture cold start.
344-
* If it's a cold start invocation, this feature will:
345-
* * Create a separate EMF blob solely containing a metric named ColdStart
346-
* * Add function_name and service dimensions
347-
*
348-
* This has the advantage of keeping cold start metric separate from your application metrics, where you might have unrelated dimensions.
349-
*
350-
* @example
351-
*
352-
* ```typescript
353-
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
354-
* import { Context } from 'aws-lambda';
355-
*
356-
* const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
357-
*
358-
* export const handler = async (event: any, context: Context) => {
359-
* metrics.captureColdStartMetric();
360-
* }
361-
* ```
362-
*/
363-
public captureColdStartMetric(): void {
364-
if (!this.isColdStart) return;
365-
this.isColdStart = false;
366-
const singleMetric = this.singleMetric();
367-
368-
if (this.dimensions.service) {
369-
singleMetric.addDimension('service', this.dimensions.service);
370-
}
371-
if (this.functionName != null) {
372-
singleMetric.addDimension('function_name', this.functionName);
373-
}
374-
singleMetric.addMetric('ColdStart', MetricUnits.Count, 1);
375-
}
376-
377-
public setFunctionName(value: string): void {
378-
this.functionName = value;
379-
}
380-
381381
private getCurrentDimensionsCount(): number {
382382
return Object.keys(this.dimensions).length + Object.keys(this.defaultDimensions).length;
383383
}

packages/metrics/src/config/ConfigService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { ConfigServiceInterface } from '.';
22

33
abstract class ConfigService implements ConfigServiceInterface {
44

5-
abstract get(name: string): string;
6-
abstract getNamespace(): string;
7-
abstract getService(): string;
5+
public abstract get(name: string): string;
6+
public abstract getNamespace(): string;
7+
public abstract getService(): string;
88

99
}
1010

packages/metrics/src/config/ConfigServiceInterface.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
interface ConfigServiceInterface {
22

3-
get?(name: string): string;
4-
getNamespace(): string;
5-
getService(): string;
3+
get?(name: string): string
4+
getNamespace(): string
5+
getService(): string
66

77
}
88

packages/metrics/src/types/Metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type EmfOutput = {
2020
CloudWatchMetrics: {
2121
Namespace: string
2222
Dimensions: [string[]]
23-
Metrics: { Name: string; Unit: MetricUnit }[];
23+
Metrics: { Name: string; Unit: MetricUnit }[]
2424
}[]
2525
}
2626
};

packages/metrics/tests/unit/Metrics.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ContextExamples as dummyContext, LambdaInterface } from '@aws-lambda-powertools/commons';
22
import { Context, Callback } from 'aws-lambda';
3+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
4+
// @ts-ignore
35
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
46
import { Metrics, MetricUnits } from '../../src/';
57
import { populateEnvironmentVariables } from '../helpers';
@@ -227,7 +229,7 @@ describe('Class: Metrics', () => {
227229
test('Cold start metric should only be written out once and flushed automatically', async () => {
228230
const metrics = new Metrics({ namespace: 'test' });
229231

230-
const handler = async (event: any, context: Context) => {
232+
const handler = async (_event, _context): Promise<void> => {
231233
// Should generate only one log
232234
metrics.captureColdStartMetric();
233235
};
@@ -361,7 +363,7 @@ describe('Class: Metrics', () => {
361363
expect.assertions(1);
362364

363365
const metrics = new Metrics({ namespace: 'test' });
364-
const handler = async (event: any, context: Context) => {
366+
const handler = async (_event, _context): Promise<void> => {
365367
metrics.raiseOnEmptyMetrics();
366368
// Logic goes here
367369
metrics.purgeStoredMetrics();

0 commit comments

Comments
 (0)