diff --git a/packages/commons/src/Utility.ts b/packages/commons/src/Utility.ts index cd2e6e0a4d..2932785e0d 100644 --- a/packages/commons/src/Utility.ts +++ b/packages/commons/src/Utility.ts @@ -52,23 +52,14 @@ */ export class Utility { protected coldStart = true; - private readonly defaultServiceName: string = 'service_undefined'; + protected readonly defaultServiceName: string = 'service_undefined'; /** * Get the cold start status of the current execution environment. * - * @example - * ```typescript - * import { Utility } from '@aws-lambda-powertools/commons'; - * - * const utility = new Utility(); - * utility.isColdStart(); // true - * utility.isColdStart(); // false - * ``` - * * The method also flips the cold start status to `false` after the first invocation. */ - public getColdStart(): boolean { + protected getColdStart(): boolean { if (this.coldStart) { this.coldStart = false; @@ -78,31 +69,6 @@ export class Utility { return false; } - /** - * Get the cold start status of the current execution environment. - * - * @example - * ```typescript - * import { Utility } from '@aws-lambda-powertools/commons'; - * - * const utility = new Utility(); - * utility.isColdStart(); // true - * utility.isColdStart(); // false - * ``` - * - * @see {@link getColdStart} - */ - public isColdStart(): boolean { - return this.getColdStart(); - } - - /** - * Get the default service name. - */ - protected getDefaultServiceName(): string { - return this.defaultServiceName; - } - /** * Validate that the service name provided is valid. * Used internally during initialization. diff --git a/packages/commons/tests/unit/Utility.test.ts b/packages/commons/tests/unit/Utility.test.ts index e655273fc6..16f88c3f38 100644 --- a/packages/commons/tests/unit/Utility.test.ts +++ b/packages/commons/tests/unit/Utility.test.ts @@ -1,157 +1,57 @@ -import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { describe, expect, it } from 'vitest'; import { Utility } from '../../src/index.js'; describe('Class: Utility', () => { - beforeEach(() => { - vi.clearAllMocks(); - vi.resetModules(); - }); + class TestUtility extends Utility { + public dummyMethod(): boolean { + return this.getColdStart(); + } + public isColdStart(): boolean { + return this.coldStart; + } + public getServiceName(): string { + return this.defaultServiceName; + } + public validateServiceName(serviceName: string): boolean { + return this.isValidServiceName(serviceName); + } + } - describe('Method: getDefaultServiceName', () => { - it('returns the default service name', () => { - class PowerTool extends Utility { - public dummyMethod(): string { - return this.getDefaultServiceName(); - } - } + it('returns the correct cold start value', () => { + // Prepare + const utility = new TestUtility(); - const powertool = new PowerTool(); - const result = powertool.dummyMethod(); - expect(result).toBe('service_undefined'); - }); + // Act & Assess + expect(utility.dummyMethod()).toBe(true); + expect(utility.dummyMethod()).toBe(false); + expect(utility.dummyMethod()).toBe(false); }); - describe('Method: getColdStart', () => { - it('it returns true the first time, then false afterwards, when called multiple times', () => { - // Prepare - const utility = new Utility(); - const getColdStartSpy = vi.spyOn(utility, 'getColdStart'); - - // Act - utility.getColdStart(); - utility.getColdStart(); - utility.getColdStart(); - utility.getColdStart(); - utility.getColdStart(); - - // Assess - expect(getColdStartSpy).toHaveBeenCalledTimes(5); - expect(getColdStartSpy.mock.results).toEqual([ - expect.objectContaining({ value: true }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - ]); - }); - - it('returns the correct values when subclassed', () => { - // Prepare - class PowerTool extends Utility { - public dummyMethod(): boolean { - return this.getColdStart(); - } - } - const powertool = new PowerTool(); - const dummyMethodSpy = vi.spyOn(powertool, 'dummyMethod'); - const getColdStartSpy = vi.spyOn(powertool, 'getColdStart'); + it('flips the cold start value', () => { + // Prepare + const utility = new TestUtility(); - // Act - powertool.dummyMethod(); - powertool.dummyMethod(); - powertool.dummyMethod(); - powertool.dummyMethod(); - powertool.dummyMethod(); + // Act + utility.dummyMethod(); - // Assess - expect(dummyMethodSpy).toHaveBeenCalledTimes(5); - expect(getColdStartSpy).toHaveBeenCalledTimes(5); - expect(dummyMethodSpy.mock.results).toEqual([ - expect.objectContaining({ value: true }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - ]); - }); + // Assess + expect(utility.isColdStart()).toBe(false); }); - describe('Method: isColdStart', () => { - it('returns true the first time, then false afterwards when called multiple times', () => { - // Prepare - const utility = new Utility(); - const isColdStartSpy = vi.spyOn(utility, 'isColdStart'); + it('returns the correct default service name', () => { + // Prepare + const utility = new TestUtility(); - // Act - utility.isColdStart(); - utility.isColdStart(); - utility.isColdStart(); - utility.isColdStart(); - utility.isColdStart(); - - // Assess - expect(isColdStartSpy).toHaveBeenCalledTimes(5); - expect(isColdStartSpy.mock.results).toEqual([ - expect.objectContaining({ value: true }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - ]); - }); - - it('returns the correct values when subclassed', () => { - // Prepare - class PowerTool extends Utility { - public dummyMethod(): boolean { - return this.isColdStart(); - } - } - const powertool = new PowerTool(); - const dummyMethodSpy = vi.spyOn(powertool, 'dummyMethod'); - const isColdStartSpy = vi.spyOn(powertool, 'isColdStart'); - - // Act - powertool.dummyMethod(); - powertool.dummyMethod(); - powertool.dummyMethod(); - powertool.dummyMethod(); - powertool.dummyMethod(); - - // Assess - expect(dummyMethodSpy).toHaveBeenCalledTimes(5); - expect(isColdStartSpy).toHaveBeenCalledTimes(5); - expect(dummyMethodSpy.mock.results).toEqual([ - expect.objectContaining({ value: true }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - expect.objectContaining({ value: false }), - ]); - }); + // Assess + expect(utility.getServiceName()).toBe('service_undefined'); }); - describe('Method: isValidServiceName', () => { - class PowerTool extends Utility { - public dummyMethod(name: string): boolean { - return this.isValidServiceName(name); - } - } - it('allows valid strings', () => { - const powertool = new PowerTool(); - const goodName = 'serverlessAirline'; - - const result = powertool.dummyMethod(goodName); - - expect(result).toBe(true); - }); - - it("doesn't allow empty strings", () => { - const tooShort = ''; - const powertool = new PowerTool(); - const result = powertool.dummyMethod(tooShort); + it('validates service name', () => { + // Prepare + const utility = new TestUtility(); - expect(result).toBe(false); - }); + // Act & Assess + expect(utility.validateServiceName('serverlessAirline')).toBe(true); + expect(utility.validateServiceName('')).toBe(false); }); }); diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 08843747fe..d83c26bc66 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -1259,7 +1259,7 @@ class Logger extends Utility implements LoggerInterface { serviceName || this.getCustomConfigService()?.getServiceName() || this.getEnvVarsService().getServiceName() || - this.getDefaultServiceName(), + this.defaultServiceName, }); persistentKeys && this.appendPersistentKeys(persistentKeys); } diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index f0ad0f7c84..93b2e35787 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -373,7 +373,7 @@ class Metrics extends Utility implements MetricsInterface { * ``` */ public captureColdStartMetric(): void { - if (!this.isColdStart()) return; + if (!this.getColdStart()) return; const singleMetric = this.singleMetric(); if (this.defaultDimensions.service) { @@ -975,7 +975,7 @@ class Metrics extends Utility implements MetricsInterface { ((service || this.getCustomConfigService()?.getServiceName() || this.getEnvVarsService().getServiceName()) as string) || - this.getDefaultServiceName(); + this.defaultServiceName; if (targetService.length > 0) { this.setDefaultDimensions({ service: targetService }); } diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index 359cf128a1..281241bdbc 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -914,7 +914,7 @@ class Tracer extends Utility implements TracerInterface { return; } - this.serviceName = this.getDefaultServiceName(); + this.serviceName = this.defaultServiceName; } /**