From 3a617bf19bdb3c211ef694f3fc510f8f60c79bc9 Mon Sep 17 00:00:00 2001 From: Niek Palm Date: Tue, 1 Jul 2025 22:14:04 +0200 Subject: [PATCH 1/9] fix: add lambda version to context logging --- .../functions/ami-housekeeper/version.json | 3 + lambdas/functions/control-plane/version.json | 3 + .../functions/gh-agent-syncer/version.json | 3 + .../termination-watcher/version.json | 3 + lambdas/functions/webhook/version.json | 3 + .../aws-powertools-util/src/logger/index.ts | 13 +++++ .../src/logger/logger.test.ts | 55 ++++++++++++++++++- 7 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 lambdas/functions/ami-housekeeper/version.json create mode 100644 lambdas/functions/control-plane/version.json create mode 100644 lambdas/functions/gh-agent-syncer/version.json create mode 100644 lambdas/functions/termination-watcher/version.json create mode 100644 lambdas/functions/webhook/version.json diff --git a/lambdas/functions/ami-housekeeper/version.json b/lambdas/functions/ami-housekeeper/version.json new file mode 100644 index 0000000000..b58d4d69fb --- /dev/null +++ b/lambdas/functions/ami-housekeeper/version.json @@ -0,0 +1,3 @@ +{ + "version": "main" +} diff --git a/lambdas/functions/control-plane/version.json b/lambdas/functions/control-plane/version.json new file mode 100644 index 0000000000..b58d4d69fb --- /dev/null +++ b/lambdas/functions/control-plane/version.json @@ -0,0 +1,3 @@ +{ + "version": "main" +} diff --git a/lambdas/functions/gh-agent-syncer/version.json b/lambdas/functions/gh-agent-syncer/version.json new file mode 100644 index 0000000000..b58d4d69fb --- /dev/null +++ b/lambdas/functions/gh-agent-syncer/version.json @@ -0,0 +1,3 @@ +{ + "version": "main" +} diff --git a/lambdas/functions/termination-watcher/version.json b/lambdas/functions/termination-watcher/version.json new file mode 100644 index 0000000000..b58d4d69fb --- /dev/null +++ b/lambdas/functions/termination-watcher/version.json @@ -0,0 +1,3 @@ +{ + "version": "main" +} diff --git a/lambdas/functions/webhook/version.json b/lambdas/functions/webhook/version.json new file mode 100644 index 0000000000..b58d4d69fb --- /dev/null +++ b/lambdas/functions/webhook/version.json @@ -0,0 +1,3 @@ +{ + "version": "main" +} diff --git a/lambdas/libs/aws-powertools-util/src/logger/index.ts b/lambdas/libs/aws-powertools-util/src/logger/index.ts index 195b552a74..392bb4021d 100644 --- a/lambdas/libs/aws-powertools-util/src/logger/index.ts +++ b/lambdas/libs/aws-powertools-util/src/logger/index.ts @@ -1,5 +1,7 @@ import { Logger } from '@aws-lambda-powertools/logger'; import { Context } from 'aws-lambda'; +import * as fs from 'fs'; +import * as path from 'path'; const childLoggers: Logger[] = []; @@ -8,10 +10,20 @@ const defaultValues = { environment: process.env.ENVIRONMENT || 'N/A', }; +function getReleaseVersion(): string { + const versionFilePath = path.resolve(__dirname, 'version.json'); + if (fs.existsSync(versionFilePath)) { + const versionData = fs.readFileSync(versionFilePath, 'utf-8'); + return JSON.parse(versionData).version || 'unknown'; + } + return 'unknown'; +} + function setContext(context: Context, module?: string) { logger.addPersistentLogAttributes({ 'aws-request-id': context.awsRequestId, 'function-name': context.functionName, + version: getReleaseVersion(), module: module, }); @@ -20,6 +32,7 @@ function setContext(context: Context, module?: string) { childLogger.addPersistentLogAttributes({ 'aws-request-id': context.awsRequestId, 'function-name': context.functionName, + version: getReleaseVersion(), }); }); } diff --git a/lambdas/libs/aws-powertools-util/src/logger/logger.test.ts b/lambdas/libs/aws-powertools-util/src/logger/logger.test.ts index bcdc44f677..2040531d4f 100644 --- a/lambdas/libs/aws-powertools-util/src/logger/logger.test.ts +++ b/lambdas/libs/aws-powertools-util/src/logger/logger.test.ts @@ -1,7 +1,8 @@ import { Context } from 'aws-lambda'; +import * as fs from 'fs'; +import * as path from 'path'; -import { logger, setContext } from '../'; -import { describe, test, expect, beforeEach, vi } from 'vitest'; +import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest'; beforeEach(() => { vi.clearAllMocks(); @@ -29,6 +30,31 @@ const context: Context = { }, }; +const versionFilePath = path.resolve(__dirname, 'version.json'); + +let logger: typeof import('../').logger; +let setContext: typeof import('../').setContext; + +beforeEach(async () => { + // Clear the module cache and reload the logger module + vi.resetModules(); + const loggerModule = await import('../'); + logger = loggerModule.logger; + setContext = loggerModule.setContext; + + // Ensure a clean state before each test + if (fs.existsSync(versionFilePath)) { + fs.unlinkSync(versionFilePath); + } +}); + +afterEach(() => { + // Clean up after each test + if (fs.existsSync(versionFilePath)) { + fs.unlinkSync(versionFilePath); + } +}); + describe('A root logger.', () => { test('Should log set context.', async () => { setContext(context, 'unit-test'); @@ -42,3 +68,28 @@ describe('A root logger.', () => { ); }); }); + +describe('Logger version handling', () => { + test('Should not fail if version.json does not exist', () => { + setContext(context, 'unit-test'); + + expect(logger.getPersistentLogAttributes()).toEqual( + expect.objectContaining({ + version: 'unknown', + }), + ); + }); + + test('Should log version if version.json exists', () => { + // Create a mock version.json file + fs.writeFileSync(versionFilePath, JSON.stringify({ version: '1.0.0' })); + + setContext(context, 'unit-test'); + + expect(logger.getPersistentLogAttributes()).toEqual( + expect.objectContaining({ + version: '1.0.0', + }), + ); + }); +}); From 72b95a55d05c2ac90e297296eb9406360212118b Mon Sep 17 00:00:00 2001 From: Niek Palm Date: Tue, 1 Jul 2025 22:49:31 +0200 Subject: [PATCH 2/9] release tests --- .release-please-manifest.json | 7 ++++ .../aws-powertools-util/src/logger/index.ts | 8 ++--- .../src/logger/logger.test.ts | 32 ++++++++++++++++--- release-please-config.json | 12 +++++++ 4 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 .release-please-manifest.json create mode 100644 release-please-config.json diff --git a/.release-please-manifest.json b/.release-please-manifest.json new file mode 100644 index 0000000000..8d280d2e3c --- /dev/null +++ b/.release-please-manifest.json @@ -0,0 +1,7 @@ +{ + "packages/ami-housekeeper": "1.0.0", + "packages/control-plane": "1.0.0", + "packages/gh-agent-syncer": "1.0.0", + "packages/termination-watcher": "1.0.0", + "packages/webhook": "1.0.0" +} diff --git a/lambdas/libs/aws-powertools-util/src/logger/index.ts b/lambdas/libs/aws-powertools-util/src/logger/index.ts index 392bb4021d..0ba2aff416 100644 --- a/lambdas/libs/aws-powertools-util/src/logger/index.ts +++ b/lambdas/libs/aws-powertools-util/src/logger/index.ts @@ -11,10 +11,10 @@ const defaultValues = { }; function getReleaseVersion(): string { - const versionFilePath = path.resolve(__dirname, 'version.json'); - if (fs.existsSync(versionFilePath)) { - const versionData = fs.readFileSync(versionFilePath, 'utf-8'); - return JSON.parse(versionData).version || 'unknown'; + const packageFilePath = path.resolve(__dirname, 'package.json'); + if (fs.existsSync(packageFilePath)) { + const packageData = fs.readFileSync(packageFilePath, 'utf-8'); + return JSON.parse(packageData).version || 'unknown'; } return 'unknown'; } diff --git a/lambdas/libs/aws-powertools-util/src/logger/logger.test.ts b/lambdas/libs/aws-powertools-util/src/logger/logger.test.ts index 2040531d4f..704bdbe89e 100644 --- a/lambdas/libs/aws-powertools-util/src/logger/logger.test.ts +++ b/lambdas/libs/aws-powertools-util/src/logger/logger.test.ts @@ -70,7 +70,15 @@ describe('A root logger.', () => { }); describe('Logger version handling', () => { - test('Should not fail if version.json does not exist', () => { + test('Should not fail if package.json does not exist', () => { + const packageFilePath = path.resolve(__dirname, 'package.json'); + + // Temporarily rename package.json to simulate its absence + const tempFilePath = `${packageFilePath}.bak`; + if (fs.existsSync(packageFilePath)) { + fs.renameSync(packageFilePath, tempFilePath); + } + setContext(context, 'unit-test'); expect(logger.getPersistentLogAttributes()).toEqual( @@ -78,11 +86,20 @@ describe('Logger version handling', () => { version: 'unknown', }), ); + + // Restore package.json + if (fs.existsSync(tempFilePath)) { + fs.renameSync(tempFilePath, packageFilePath); + } }); - test('Should log version if version.json exists', () => { - // Create a mock version.json file - fs.writeFileSync(versionFilePath, JSON.stringify({ version: '1.0.0' })); + test('Should log version from package.json', () => { + const packageFilePath = path.resolve(__dirname, 'package.json'); + + // Create a mock package.json file + const originalPackageData = fs.existsSync(packageFilePath) ? fs.readFileSync(packageFilePath, 'utf-8') : null; + const mockPackageData = JSON.stringify({ version: '1.0.0' }); + fs.writeFileSync(packageFilePath, mockPackageData); setContext(context, 'unit-test'); @@ -91,5 +108,12 @@ describe('Logger version handling', () => { version: '1.0.0', }), ); + + // Restore the original package.json file + if (originalPackageData) { + fs.writeFileSync(packageFilePath, originalPackageData); + } else { + fs.unlinkSync(packageFilePath); + } }); }); diff --git a/release-please-config.json b/release-please-config.json new file mode 100644 index 0000000000..a3e910b8b4 --- /dev/null +++ b/release-please-config.json @@ -0,0 +1,12 @@ +{ + "packages": { + // `.` is a special case for handling to root of the repository + "lambdas": { + // overrides release-type for node + "release-type": "node", + // exclude commits from that path from processing + "exclude-paths": ["lambdas/libs"], + + } + } +} From 1c156184aa991d1081d509e92b78b660a58efce0 Mon Sep 17 00:00:00 2001 From: Niek Palm Date: Tue, 1 Jul 2025 22:51:51 +0200 Subject: [PATCH 3/9] release tests --- release-please-config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release-please-config.json b/release-please-config.json index a3e910b8b4..43e3d1670a 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -5,7 +5,7 @@ // overrides release-type for node "release-type": "node", // exclude commits from that path from processing - "exclude-paths": ["lambdas/libs"], + "exclude-paths": ["lambdas/libs"] } } From 01e2e2380cc4bc1536621f8929bf862a097e008f Mon Sep 17 00:00:00 2001 From: Niek Palm Date: Thu, 3 Jul 2025 22:17:12 +0200 Subject: [PATCH 4/9] fix: Add lambda version to log context --- lambdas/functions/ami-housekeeper/version.json | 3 --- lambdas/functions/control-plane/version.json | 3 --- lambdas/functions/termination-watcher/version.json | 3 --- lambdas/functions/webhook/version.json | 3 --- 4 files changed, 12 deletions(-) delete mode 100644 lambdas/functions/ami-housekeeper/version.json delete mode 100644 lambdas/functions/control-plane/version.json delete mode 100644 lambdas/functions/termination-watcher/version.json delete mode 100644 lambdas/functions/webhook/version.json diff --git a/lambdas/functions/ami-housekeeper/version.json b/lambdas/functions/ami-housekeeper/version.json deleted file mode 100644 index b58d4d69fb..0000000000 --- a/lambdas/functions/ami-housekeeper/version.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "version": "main" -} diff --git a/lambdas/functions/control-plane/version.json b/lambdas/functions/control-plane/version.json deleted file mode 100644 index b58d4d69fb..0000000000 --- a/lambdas/functions/control-plane/version.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "version": "main" -} diff --git a/lambdas/functions/termination-watcher/version.json b/lambdas/functions/termination-watcher/version.json deleted file mode 100644 index b58d4d69fb..0000000000 --- a/lambdas/functions/termination-watcher/version.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "version": "main" -} diff --git a/lambdas/functions/webhook/version.json b/lambdas/functions/webhook/version.json deleted file mode 100644 index b58d4d69fb..0000000000 --- a/lambdas/functions/webhook/version.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "version": "main" -} From b4cd1d16569f3ddb5a4656ee8caa0163893105e6 Mon Sep 17 00:00:00 2001 From: Niek Palm Date: Thu, 3 Jul 2025 22:17:46 +0200 Subject: [PATCH 5/9] fix: Add lambda version to log context --- lambdas/functions/gh-agent-syncer/version.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 lambdas/functions/gh-agent-syncer/version.json diff --git a/lambdas/functions/gh-agent-syncer/version.json b/lambdas/functions/gh-agent-syncer/version.json deleted file mode 100644 index b58d4d69fb..0000000000 --- a/lambdas/functions/gh-agent-syncer/version.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "version": "main" -} From 2f8f3bf6121019b7e2bfbaacd173f5e244cd9723 Mon Sep 17 00:00:00 2001 From: Niek Palm Date: Thu, 3 Jul 2025 22:19:41 +0200 Subject: [PATCH 6/9] fix: Add lambda version to log context --- .release-please-manifest.json | 6 +----- release-please-config.json | 36 ++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 8d280d2e3c..2f0a9c430f 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,7 +1,3 @@ { - "packages/ami-housekeeper": "1.0.0", - "packages/control-plane": "1.0.0", - "packages/gh-agent-syncer": "1.0.0", - "packages/termination-watcher": "1.0.0", - "packages/webhook": "1.0.0" + ".": "6.6.0" } diff --git a/release-please-config.json b/release-please-config.json index 43e3d1670a..7c55680405 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -1,12 +1,34 @@ { + "release-type": "simple", "packages": { - // `.` is a special case for handling to root of the repository - "lambdas": { - // overrides release-type for node - "release-type": "node", - // exclude commits from that path from processing - "exclude-paths": ["lambdas/libs"] - + ".": { + "extra-files": [ + { + "type": "json", + "path": "lambdas/functions/ami-housekeeper/package.json", + "jsonpath": "$.version" + }, + { + "type": "json", + "path": "lambdas/functions/control-plane/package.json", + "jsonpath": "$.version" + }, + { + "type": "json", + "path": "lambdas/functions/gh-agent-syncer/package.json", + "jsonpath": "$.version" + }, + { + "type": "json", + "path": "lambdas/functions/termination-watcher/package.json", + "jsonpath": "$.version" + }, + { + "type": "json", + "path": "lambdas/functions/webhook/package.json", + "jsonpath": "$.version" + } + ] } } } From 406abc099cabcf70f7c6441c158b6f10c23ac1f7 Mon Sep 17 00:00:00 2001 From: Niek Palm Date: Thu, 3 Jul 2025 22:22:16 +0200 Subject: [PATCH 7/9] fix: Add lambda version to log context --- .../libs/aws-powertools-util/src/logger/index.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lambdas/libs/aws-powertools-util/src/logger/index.ts b/lambdas/libs/aws-powertools-util/src/logger/index.ts index 0ba2aff416..49059b4e20 100644 --- a/lambdas/libs/aws-powertools-util/src/logger/index.ts +++ b/lambdas/libs/aws-powertools-util/src/logger/index.ts @@ -2,21 +2,26 @@ import { Logger } from '@aws-lambda-powertools/logger'; import { Context } from 'aws-lambda'; import * as fs from 'fs'; import * as path from 'path'; +import { fileURLToPath } from 'url'; const childLoggers: Logger[] = []; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + const defaultValues = { region: process.env.AWS_REGION, environment: process.env.ENVIRONMENT || 'N/A', }; function getReleaseVersion(): string { - const packageFilePath = path.resolve(__dirname, 'package.json'); - if (fs.existsSync(packageFilePath)) { - const packageData = fs.readFileSync(packageFilePath, 'utf-8'); - return JSON.parse(packageData).version || 'unknown'; + let version = 'unknown'; + try { + const packageFilePath = path.resolve(__dirname, 'package.json'); + version = JSON.parse(fs.readFileSync(packageFilePath, 'utf-8')).version || 'unknown'; + } catch (error) { + logger.debug(`Failed to read package.json for version: ${error.message}`); } - return 'unknown'; + return version; } function setContext(context: Context, module?: string) { From ab3c8ff8bbbe691263b6205ffafd8e28e9ebd69c Mon Sep 17 00:00:00 2001 From: Niek Palm Date: Thu, 3 Jul 2025 22:27:41 +0200 Subject: [PATCH 8/9] fix: Add lambda version to log context --- lambdas/libs/aws-powertools-util/src/logger/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambdas/libs/aws-powertools-util/src/logger/index.ts b/lambdas/libs/aws-powertools-util/src/logger/index.ts index 49059b4e20..f3fdbe3c6e 100644 --- a/lambdas/libs/aws-powertools-util/src/logger/index.ts +++ b/lambdas/libs/aws-powertools-util/src/logger/index.ts @@ -19,7 +19,7 @@ function getReleaseVersion(): string { const packageFilePath = path.resolve(__dirname, 'package.json'); version = JSON.parse(fs.readFileSync(packageFilePath, 'utf-8')).version || 'unknown'; } catch (error) { - logger.debug(`Failed to read package.json for version: ${error.message}`); + logger.debug(`Failed to read package.json for version: ${(error as Error)?.message ?? 'Unknown error'}`); } return version; } From cb1e684108efc37c51a8da8707459d07b421959f Mon Sep 17 00:00:00 2001 From: Niek Palm Date: Thu, 3 Jul 2025 22:39:25 +0200 Subject: [PATCH 9/9] feat: Add lambda version to log context --- .github/workflows/release.yml | 3 ++- release-please-config.json => .release-please-config.json | 0 2 files changed, 2 insertions(+), 1 deletion(-) rename release-please-config.json => .release-please-config.json (100%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d70f0a60a7..895410453a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -40,7 +40,8 @@ jobs: uses: googleapis/release-please-action@a02a34c4d625f9be7cb89156071d8567266a2445 # v4.2.0 with: target-branch: ${{ steps.branch.outputs.name }} - release-type: terraform-module + manifest-file: .release-please-manifest.json + config-file: .release-please-config.json token: ${{ steps.token.outputs.token }} - name: Attest if: ${{ steps.release.outputs.releases_created == 'true' }} diff --git a/release-please-config.json b/.release-please-config.json similarity index 100% rename from release-please-config.json rename to .release-please-config.json